Move map to module

This commit is contained in:
Anton Vakhrushev 2019-09-15 10:50:10 +03:00
parent bbd16a92b1
commit 7652adebc4
2 changed files with 78 additions and 66 deletions

78
src/map.cr Normal file
View File

@ -0,0 +1,78 @@
abstract class Tile
abstract def letter : Char
end
class GrassTile < Tile
def letter : Char
'.'
end
end
class WoodTile < Tile
def initialize(cap : Int32)
@cap = cap
@cur = cap
end
def cap
@cap
end
def inc(v : Int32)
@cur += v
if @cur < 0
@cur = 0
end
if @cur > @cap
@cur = @cap
end
end
def letter : Char
'f'
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
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)
end
def get(x : Int32, y : Int32) : Tile
@data[key(Point.new(x, y))]
end
def print
(0...4).each do |x|
(0...4).each do |y|
printf "%c", @data[key(Point.new(x, y))].letter
end
printf "\n"
end
end
private def key(p : Point) : String
return sprintf "%d:%d", p.x, p.y
end
end

View File

@ -70,72 +70,6 @@ class GrowWoodCommand < Command
end end
end end
abstract class Tile
abstract def letter : Char
end
class GrassTile < Tile
def letter : Char
'.'
end
end
class WoodTile < Tile
def initialize(cap : Int32)
@cap = cap
@cur = cap
end
def cap
@cap
end
def inc(v : Int32)
@cur += v
if @cur < 0
@cur = 0
end
if @cur > @cap
@cur = @cap
end
end
def letter : Char
'f'
end
end
class Map
def initialize
@data = {} of String => Tile
(0...4).each do |x|
(0...4).each do |y|
@data[key(x, y)] = GrassTile.new
end
end
@data[key(1, 1)] = WoodTile.new(100)
@data[key(3, 1)] = WoodTile.new(200)
@data[key(2, 2)] = WoodTile.new(100)
end
def get(x : Int32, y : Int32) : Tile
@data[key(x, y)]
end
def print
(0...4).each do |x|
(0...4).each do |y|
printf "%c", @data[key(x, y)].letter
end
printf "\n"
end
end
private def key(x : Int32, y : Int32) : String
return sprintf "%d:%d", x, y
end
end
class World class World
def initialize def initialize
@resources = Resources.new @resources = Resources.new