Add tile types

This commit is contained in:
Anton Vakhrushev 2019-09-17 17:51:38 +03:00
parent 9c8fdb89ac
commit 3ee59a1dc9
3 changed files with 55 additions and 16 deletions

View File

@ -1,3 +1,5 @@
require "colorize"
abstract class Command abstract class Command
abstract def start(world : World) : Int32 abstract def start(world : World) : Int32
abstract def finish(world : World) abstract def finish(world : World)
@ -33,23 +35,21 @@ class GetWoodCommand < Command
def start(world : World) : Int32 def start(world : World) : Int32
wood_tile = nearest_wood(world) wood_tile = nearest_wood(world)
if wood_tile stock_tile = nearest_stock(world)
calc_time(wood_tile.as(Tile)) if wood_tile && stock_tile
calc_time(wood_tile.as(Tile), stock_tile.as(Tile))
else else
printf " << no wood tile\n" puts " << no wood or stock tile".colorize(:red)
REST_TIME REST_TIME
end end
end end
private def calc_time(wood_tile : Tile) private def calc_time(wood_tile : Tile, stock_tile : Tile)
wood_point = wood_tile.point wood_dist = @point.distance(wood_tile.point)
dist = @point.distance(wood_point) stock_dist = @point.distance(stock_tile.point)
@wood = wood_tile.withdraw(BASE_WOOD) @wood = wood_tile.withdraw(BASE_WOOD)
printf " << start cut down wood at [%d,%d] -> %d -> %d -> [%d,%d]\n", printf " << wood %d, %d, %d\n", BASE_TIME, 2 * wood_dist, 2 * stock_dist
@point.x, @point.y, BASE_TIME + 2 * wood_dist + 2 * stock_dist
dist, @wood,
wood_point.x, wood_point.y
BASE_TIME + 2 * dist
end end
def finish(world : World) def finish(world : World)
@ -60,7 +60,13 @@ class GetWoodCommand < Command
private def nearest_wood(world : World) private def nearest_wood(world : World)
world.map.nearest_tile @point do |tile| world.map.nearest_tile @point do |tile|
tile.letter == 'f' && tile.cur > 0 tile.supports(TileType::Wood) && tile.cur > 0
end
end
private def nearest_stock(world : World)
world.map.nearest_tile @point do |tile|
tile.supports(TileType::Stock)
end end
end end
end end
@ -126,7 +132,7 @@ class GrowWoodCommand < Command
private def nearest_wood(world : World) private def nearest_wood(world : World)
world.map.nearest_tile @point do |tile| world.map.nearest_tile @point do |tile|
tile.letter == 'f' && tile.cur < tile.cap tile.supports(TileType::Wood) && tile.cur < tile.cap
end end
end end
end end

View File

@ -13,6 +13,11 @@ struct Point
end end
end end
enum TileType
Stock
Wood
end
abstract class Tile abstract class Tile
property cap : Int32 = 0 property cap : Int32 = 0
property cur : Int32 = 0 property cur : Int32 = 0
@ -24,6 +29,9 @@ abstract class Tile
getter cap getter cap
getter cur getter cur
abstract def letter : Char
abstract def supports(t : TileType) : Bool
def withdraw(value) def withdraw(value)
if value >= @cur if value >= @cur
wd = @cur wd = @cur
@ -39,14 +47,26 @@ abstract class Tile
charged = @cur + value charged = @cur + value
@cur = charged <= @cap ? charged : @cap @cur = charged <= @cap ? charged : @cap
end end
abstract def letter : Char
end end
class StoneTile < Tile class StoneTile < Tile
def letter : Char def letter : Char
'.' '.'
end end
def supports(t : TileType) : Bool
false
end
end
class MainBaseTile < Tile
def letter : Char
'M'
end
def supports(t : TileType) : Bool
t == TileType::Stock
end
end end
class WoodTile < Tile class WoodTile < Tile
@ -58,18 +78,30 @@ class WoodTile < Tile
def letter : Char def letter : Char
'f' 'f'
end end
def supports(t : TileType) : Bool
t == TileType::Wood
end
end end
class WoodMillTile < Tile class WoodMillTile < Tile
def letter : Char def letter : Char
'm' 'm'
end end
def supports(t : TileType) : Bool
t == TileType::Wood
end
end end
class ForesterHouseTile < Tile class ForesterHouseTile < Tile
def letter : Char def letter : Char
'h' 'h'
end end
def supports(t : TileType) : Bool
false
end
end end
class Map class Map
@ -82,6 +114,7 @@ class Map
self.set(StoneTile.new(Point.new(x, y))) self.set(StoneTile.new(Point.new(x, y)))
end end
end end
self.set(MainBaseTile.new(Point.new(0, 0)))
self.set(WoodTile.new(Point.new(1, 1), 100)) self.set(WoodTile.new(Point.new(1, 1), 100))
self.set(WoodTile.new(Point.new(3, 1), 200)) self.set(WoodTile.new(Point.new(3, 1), 200))
self.set(WoodTile.new(Point.new(2, 2), 100)) self.set(WoodTile.new(Point.new(2, 2), 100))

View File

@ -62,6 +62,6 @@ w.map.print
w.push(BuildWoodMillCommand.new(Point.new(2, 3))) w.push(BuildWoodMillCommand.new(Point.new(2, 3)))
w.push(BuildForesterHouseCommand.new(Point.new(1, 2))) w.push(BuildForesterHouseCommand.new(Point.new(1, 2)))
w.push(BuildForesterHouseCommand.new(Point.new(3, 2))) w.push(BuildForesterHouseCommand.new(Point.new(3, 2)))
w.run(120) w.run(60)
w.map.print w.map.print
printf "Wood: %d\n", w.resources.wood printf "Wood: %d\n", w.resources.wood