Fix terraform logic

This commit is contained in:
Anton Vakhrushev 2019-10-08 22:14:01 +03:00
parent 47d6857dd7
commit 4b61881bfb
3 changed files with 20 additions and 5 deletions

View File

@ -7,7 +7,7 @@ world = Game::World.new(ts)
router = CLI::CommandRouter.new router = CLI::CommandRouter.new
router.add "harv {x} {y}", "Build harvester at x,y" do |p| router.add "harv {x} {y}", "Build harvester at x,y (+80cr/10s)" do |p|
x = p["x"].to_i32 x = p["x"].to_i32
y = p["y"].to_i32 y = p["y"].to_i32
point = Game::Point.new(x, y) point = Game::Point.new(x, y)
@ -15,14 +15,14 @@ router.add "harv {x} {y}", "Build harvester at x,y" do |p|
printf "Build harvester at %d %d\n", x, y printf "Build harvester at %d %d\n", x, y
end end
router.add "rest {x} {y}", "Build restorer at x,y (100 cr)" do |p| router.add "rest {x} {y}", "Build restorer at x,y (100 cr, +30cr/15s)" do |p|
x = p["x"].to_i32 x = p["x"].to_i32
y = p["y"].to_i32 y = p["y"].to_i32
point = Game::Point.new(x, y) point = Game::Point.new(x, y)
world.push(Game::BuildCrystalRestorerCommand.new(point)) world.push(Game::BuildCrystalRestorerCommand.new(point))
end end
router.add "terr {x} {y}", "Build terraformator at x,y (300 cr)" do |p| router.add "terr {x} {y}", "Build terraformator at x,y (300 cr, +5terr/-50cr/60s)" do |p|
x = p["x"].to_i32 x = p["x"].to_i32
y = p["y"].to_i32 y = p["y"].to_i32
point = Game::Point.new(x, y) point = Game::Point.new(x, y)

View File

@ -170,13 +170,22 @@ module Game
class TerraformCommand < Command class TerraformCommand < Command
PRODUCTION_TIME = 60 PRODUCTION_TIME = 60
REST_TIME = 20
PRODUCTION_VALUE = 5 PRODUCTION_VALUE = 5
CRYSTAL_REQUIRED = 50
def initialize(@point : Point) def initialize(@point : Point)
@can_terr = false
end end
def start(world : World) : Int32 def start(world : World) : Int32
PRODUCTION_TIME if world.resources.has(ResourceType::Crystal, CRYSTAL_REQUIRED)
world.resources.dec(ResourceType::Crystal, CRYSTAL_REQUIRED)
@can_terr = true
PRODUCTION_TIME
else
REST_TIME
end
end end
def desc : String def desc : String
@ -184,7 +193,9 @@ module Game
end end
def finish(world : World) def finish(world : World)
world.resources.inc(ResourceType::Terraformation, PRODUCTION_VALUE) if @can_terr
world.resources.inc(ResourceType::Terraformation, PRODUCTION_VALUE)
end
world.push(TerraformCommand.new(@point)) world.push(TerraformCommand.new(@point))
end end
end end

View File

@ -17,6 +17,10 @@ class Game::Resources
@values[t] @values[t]
end end
def has(t : ResourceType, value : Int32) : Bool
@values[t] >= value
end
def inc(t : ResourceType, value : Int32) def inc(t : ResourceType, value : Int32)
new_value = @values[t] + value new_value = @values[t] + value
if new_value < 0 if new_value < 0