Add tile types
This commit is contained in:
parent
9c8fdb89ac
commit
3ee59a1dc9
@ -1,3 +1,5 @@
|
||||
require "colorize"
|
||||
|
||||
abstract class Command
|
||||
abstract def start(world : World) : Int32
|
||||
abstract def finish(world : World)
|
||||
@ -33,23 +35,21 @@ class GetWoodCommand < Command
|
||||
|
||||
def start(world : World) : Int32
|
||||
wood_tile = nearest_wood(world)
|
||||
if wood_tile
|
||||
calc_time(wood_tile.as(Tile))
|
||||
stock_tile = nearest_stock(world)
|
||||
if wood_tile && stock_tile
|
||||
calc_time(wood_tile.as(Tile), stock_tile.as(Tile))
|
||||
else
|
||||
printf " << no wood tile\n"
|
||||
puts " << no wood or stock tile".colorize(:red)
|
||||
REST_TIME
|
||||
end
|
||||
end
|
||||
|
||||
private def calc_time(wood_tile : Tile)
|
||||
wood_point = wood_tile.point
|
||||
dist = @point.distance(wood_point)
|
||||
private def calc_time(wood_tile : Tile, stock_tile : Tile)
|
||||
wood_dist = @point.distance(wood_tile.point)
|
||||
stock_dist = @point.distance(stock_tile.point)
|
||||
@wood = wood_tile.withdraw(BASE_WOOD)
|
||||
printf " << start cut down wood at [%d,%d] -> %d -> %d -> [%d,%d]\n",
|
||||
@point.x, @point.y,
|
||||
dist, @wood,
|
||||
wood_point.x, wood_point.y
|
||||
BASE_TIME + 2 * dist
|
||||
printf " << wood %d, %d, %d\n", BASE_TIME, 2 * wood_dist, 2 * stock_dist
|
||||
BASE_TIME + 2 * wood_dist + 2 * stock_dist
|
||||
end
|
||||
|
||||
def finish(world : World)
|
||||
@ -60,7 +60,13 @@ class GetWoodCommand < Command
|
||||
|
||||
private def nearest_wood(world : World)
|
||||
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
|
||||
@ -126,7 +132,7 @@ class GrowWoodCommand < Command
|
||||
|
||||
private def nearest_wood(world : World)
|
||||
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
|
||||
|
37
src/map.cr
37
src/map.cr
@ -13,6 +13,11 @@ struct Point
|
||||
end
|
||||
end
|
||||
|
||||
enum TileType
|
||||
Stock
|
||||
Wood
|
||||
end
|
||||
|
||||
abstract class Tile
|
||||
property cap : Int32 = 0
|
||||
property cur : Int32 = 0
|
||||
@ -24,6 +29,9 @@ abstract class Tile
|
||||
getter cap
|
||||
getter cur
|
||||
|
||||
abstract def letter : Char
|
||||
abstract def supports(t : TileType) : Bool
|
||||
|
||||
def withdraw(value)
|
||||
if value >= @cur
|
||||
wd = @cur
|
||||
@ -39,14 +47,26 @@ abstract class Tile
|
||||
charged = @cur + value
|
||||
@cur = charged <= @cap ? charged : @cap
|
||||
end
|
||||
|
||||
abstract def letter : Char
|
||||
end
|
||||
|
||||
class StoneTile < Tile
|
||||
def letter : Char
|
||||
'.'
|
||||
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
|
||||
|
||||
class WoodTile < Tile
|
||||
@ -58,18 +78,30 @@ class WoodTile < Tile
|
||||
def letter : Char
|
||||
'f'
|
||||
end
|
||||
|
||||
def supports(t : TileType) : Bool
|
||||
t == TileType::Wood
|
||||
end
|
||||
end
|
||||
|
||||
class WoodMillTile < Tile
|
||||
def letter : Char
|
||||
'm'
|
||||
end
|
||||
|
||||
def supports(t : TileType) : Bool
|
||||
t == TileType::Wood
|
||||
end
|
||||
end
|
||||
|
||||
class ForesterHouseTile < Tile
|
||||
def letter : Char
|
||||
'h'
|
||||
end
|
||||
|
||||
def supports(t : TileType) : Bool
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
class Map
|
||||
@ -82,6 +114,7 @@ class Map
|
||||
self.set(StoneTile.new(Point.new(x, y)))
|
||||
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(3, 1), 200))
|
||||
self.set(WoodTile.new(Point.new(2, 2), 100))
|
||||
|
@ -62,6 +62,6 @@ w.map.print
|
||||
w.push(BuildWoodMillCommand.new(Point.new(2, 3)))
|
||||
w.push(BuildForesterHouseCommand.new(Point.new(1, 2)))
|
||||
w.push(BuildForesterHouseCommand.new(Point.new(3, 2)))
|
||||
w.run(120)
|
||||
w.run(60)
|
||||
w.map.print
|
||||
printf "Wood: %d\n", w.resources.wood
|
||||
|
Loading…
Reference in New Issue
Block a user