From 40c8e0640ff35ad268886dd160e1f4a1ebccb3ee Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sun, 15 Sep 2019 12:17:33 +0300 Subject: [PATCH] Use points for map --- src/map.cr | 12 +++++++++++- src/village.cr | 14 ++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/map.cr b/src/map.cr index a3bd2ac..47a44b5 100644 --- a/src/map.cr +++ b/src/map.cr @@ -33,6 +33,12 @@ class WoodTile < Tile end end +class WoodMillTile < Tile + def letter : Char + 'm' + end +end + struct Point def initialize(@x : Int32, @y : Int32) end @@ -59,10 +65,14 @@ class Map @data[key(Point.new(2, 2))] = WoodTile.new(100) end - def get(x : Int32, y : Int32) : Tile + def get(point : Point) : Tile @data[key(Point.new(x, y))] end + def set(point : Point, tile : Tile) + @data[key(point)] = tile + end + def print (0...4).each do |x| (0...4).each do |y| diff --git a/src/village.cr b/src/village.cr index 68cd616..11388c7 100644 --- a/src/village.cr +++ b/src/village.cr @@ -1,4 +1,5 @@ require "./queue" +require "./map" class Resources def initialize @@ -19,14 +20,18 @@ abstract class Command abstract def run(ts : Int32, world : World) end -class BuildMillCommand < Command +class BuildWoodMillCommand < Command + def initialize(@point : Point) + end + def supports?(world : World) : Bool return true end def run(ts : Int32, world : World) - puts "build mill" + printf "build mill at [%d,%d]\n", @point.x, @point.y c = GetWoodCommand.new + world.map.set(@point, WoodMillTile.new) world.push(ts + 5, c) end end @@ -89,7 +94,7 @@ class World if !command.supports?(self) return false end - printf "push command %d\n", ts + printf "push command %s, %d\n", typeof(command), ts @queue.push(ts, command) true end @@ -109,6 +114,7 @@ end w = World.new w.map.print -w.push(0, BuildMillCommand.new) +w.push(0, BuildWoodMillCommand.new(Point.new(0, 0))) w.push(1, BuildForesterHouseCommand.new) w.run(20) +w.map.print