Tile with points
This commit is contained in:
parent
101c4179df
commit
05801dd983
47
src/map.cr
47
src/map.cr
@ -1,4 +1,24 @@
|
||||
struct Point
|
||||
def initialize(@x : Int32, @y : Int32)
|
||||
end
|
||||
|
||||
def x
|
||||
@x
|
||||
end
|
||||
|
||||
def y
|
||||
@y
|
||||
end
|
||||
end
|
||||
|
||||
abstract class Tile
|
||||
def initialize(@point : Point)
|
||||
end
|
||||
|
||||
def point
|
||||
@point
|
||||
end
|
||||
|
||||
abstract def letter : Char
|
||||
end
|
||||
|
||||
@ -9,7 +29,7 @@ class GrassTile < Tile
|
||||
end
|
||||
|
||||
class WoodTile < Tile
|
||||
def initialize(cap : Int32)
|
||||
def initialize(@point : Point, cap : Int32)
|
||||
@cap = cap
|
||||
@cur = cap
|
||||
end
|
||||
@ -45,36 +65,27 @@ class ForesterHouseTile < Tile
|
||||
end
|
||||
end
|
||||
|
||||
struct Point
|
||||
def initialize(@x : Int32, @y : Int32)
|
||||
end
|
||||
|
||||
def x
|
||||
@x
|
||||
end
|
||||
|
||||
def y
|
||||
@y
|
||||
end
|
||||
end
|
||||
|
||||
class Map
|
||||
def initialize
|
||||
@data = {} of String => Tile
|
||||
(0...4).each do |x|
|
||||
(0...4).each do |y|
|
||||
@data[key(Point.new(x, y))] = GrassTile.new
|
||||
self.set(GrassTile.new(Point.new(x, y)))
|
||||
end
|
||||
end
|
||||
@data[key(Point.new(1, 1))] = WoodTile.new(100)
|
||||
@data[key(Point.new(3, 1))] = WoodTile.new(200)
|
||||
@data[key(Point.new(2, 2))] = WoodTile.new(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(2, 2), 100))
|
||||
end
|
||||
|
||||
def get(point : Point) : Tile
|
||||
@data[key(Point.new(x, y))]
|
||||
end
|
||||
|
||||
def set(tile : Tile)
|
||||
@data[key(tile.point)] = tile
|
||||
end
|
||||
|
||||
def set(point : Point, tile : Tile)
|
||||
@data[key(point)] = tile
|
||||
end
|
||||
|
@ -30,7 +30,7 @@ class BuildWoodMillCommand < Command
|
||||
|
||||
def run(ts : Int32, world : World)
|
||||
printf "build mill at [%d,%d]\n", @point.x, @point.y
|
||||
world.map.set(@point, WoodMillTile.new)
|
||||
world.map.set(WoodMillTile.new(@point))
|
||||
world.push(ts + 5, GetWoodCommand.new)
|
||||
end
|
||||
end
|
||||
@ -45,7 +45,7 @@ class BuildForesterHouseCommand < Command
|
||||
|
||||
def run(ts : Int32, world : World)
|
||||
printf "build forester house at [%d,%d]\n", @point.x, @point.y
|
||||
world.map.set(@point, ForesterHouseTile.new)
|
||||
world.map.set(ForesterHouseTile.new(@point))
|
||||
world.push(ts + 10, GrowWoodCommand.new)
|
||||
end
|
||||
end
|
||||
@ -72,8 +72,7 @@ class GrowWoodCommand < Command
|
||||
def run(ts : Int32, world : World)
|
||||
res = world.resources
|
||||
puts "grow wood"
|
||||
c = GetWoodCommand.new
|
||||
world.push(ts + 5, c)
|
||||
world.push(ts + 5, GetWoodCommand.new)
|
||||
end
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user