CabinGame/Luxe/blocks/adventures.wren

131 lines
3.3 KiB
Text
Raw Normal View History

2020-09-19 14:06:46 +00:00
import "math/observable" for Observable
import "luxe: world" for Entity, Values, Tags
2020-09-19 20:38:55 +00:00
import "globals" for Globals
2020-10-23 13:25:53 +00:00
import "blocks/resources" for Resources
2020-10-31 14:13:01 +00:00
import "math/stringUtil" for StringUtil
import "blocks/human/human" for Human
import "luxe: containers" for Lists
import "blocks/narrative/diary/diary" for Diary
import "blocks/narrative/progress/progress" for Progress
2020-09-19 14:06:46 +00:00
class AdventureManager{
planning : Observable {_planning}
max_distance : Num {_max_distance}
2020-09-19 14:06:46 +00:00
construct new(){
_planning = Observable.new()
_in_progress = []
_archive = []
_max_distance = 10
2020-10-31 14:13:01 +00:00
_game = Globals["Game"]
2020-09-19 14:06:46 +00:00
}
plan_new(){
Log.print("Start planning new Adventure")
2020-09-19 20:38:55 +00:00
var adventure = Adventure.new()
2020-10-31 14:13:01 +00:00
adventure.distance = 2
2020-09-19 14:06:46 +00:00
_planning.value = adventure
return adventure
}
2020-10-31 14:13:01 +00:00
dispatch(){
var adventure = _planning.value
if(!adventure){
Log.print("Tried to dispatch adventure, but none is planned")
2020-10-31 14:13:01 +00:00
return
}
adventure.setup()
adventure.adventurers.each{|human|
human.active = false
if(_game.focus.value == human) _game.focus.value = null
2020-10-31 14:13:01 +00:00
}
adventure.resources.list().each{|resource|
_game.resources.remove(resource["base_name"], resource["amount"])
}
Log.print("Dispatching adventure \"%(adventure)\"")
2020-10-31 14:13:01 +00:00
_in_progress.add(adventure)
_planning.value = null
}
arrive(adventure: Adventure){
Lists.remove(_in_progress, adventure)
//bring resources back into main storage
adventure.resources.list().each{|resource|
_game.resources.add(resource["base_name"], resource["amount"])
}
adventure.adventurers.each{|adventurer|
adventurer.active = true
}
Log.print("Adventure arrived \"%(adventure)\"")
}
2020-10-31 14:13:01 +00:00
tick(){
//check some stuff that needs to be checked from time to time
//once per second/minute/whatever should be enough
_in_progress.each{|adventure : Adventure|
adventure.tick(this)
}
2020-10-31 14:13:01 +00:00
}
2020-09-19 14:06:46 +00:00
new_or_current(){
if(_planning.value) return _planning.value
return plan_new()
}
discard(){
_planning.value = null
}
2020-10-04 16:35:12 +00:00
set_max_distance(dist){
_max_distance = dist
planning.emit()
}
2020-09-19 20:38:55 +00:00
}
class Adventure{
adventurers{_adventurers}
resources{_resources}
data{_data}
2020-09-25 08:42:41 +00:00
direction{_direction}
direction=(dir){_direction = dir}
distance{_distance}
distance=(dist){_distance = dist}
2020-09-19 20:38:55 +00:00
2020-10-31 14:13:01 +00:00
toString{
return "%(adventurers.map{|human| human.name}.toList)"+
2020-10-31 14:13:01 +00:00
" travelling %(distance)km towards %(StringUtil.direction(direction))"+
" with %(resources.list().map{|resource| "%(resource["amount"]) %(resource["name"])"}.toList)"
}
2020-09-19 20:38:55 +00:00
construct new(){
_adventurers = []
2020-10-23 13:25:53 +00:00
_resources = Resources.new()
2020-09-19 20:38:55 +00:00
_data = {}
2020-09-25 08:42:41 +00:00
_direction = 0
_distance = 0
2020-09-19 20:38:55 +00:00
}
2020-10-31 14:13:01 +00:00
setup(){
_resources.removeEmpty()
}
2020-10-31 14:13:01 +00:00
tick(manager : AdventureManager){
//generate what happens on the day
var dayData = Progress.progress(this)
//make everyone (try to) write a diary entry about it
adventurers.each{|adventurer|
Diary.write(adventurer, dayData)
}
if(dayData["arrive"]) manager.arrive(this)
2020-10-31 14:13:01 +00:00
}
2020-09-19 20:38:55 +00:00
add_adventurer(adventurer){
_adventurers.add(adventurer)
}
add_resource(resource, amount){
_resources.add(resource, amount)
2020-09-19 20:38:55 +00:00
}
2020-09-19 14:06:46 +00:00
}