Rewrite map rendering

This commit is contained in:
Anton Vakhrushev 2019-10-15 16:28:04 +03:00
parent e0050b920e
commit c05199c7d8
8 changed files with 67 additions and 125 deletions

View File

@ -3,7 +3,7 @@ require "./spec_helper"
module Game::TestBuilding module Game::TestBuilding
describe Building do describe Building do
it "should create storehouse" do it "should create storehouse" do
bg = Building.new Building::Type::Storehouse, storage: 100 bg = Building.new Building::Type::StartPoint, storage: 100
bg.storage.should eq 100 bg.storage.should eq 100
end end

View File

@ -13,7 +13,8 @@ module Game::TestCommand
Point.new(0, 0), Point.new(0, 0),
Building.new( Building.new(
Building::Type::StartPoint, Building::Type::StartPoint,
storage: 200, roles: [Building::Role::Storehouse] storage: 200,
roles: [Building::Role::Storehouse]
) )
) )
map.set DepositTile.new( map.set DepositTile.new(

View File

@ -14,7 +14,7 @@ class App
@buildings.items.each do |i| @buildings.items.each do |i|
b = i[:b] b = i[:b]
route = sprintf "%s {x} {y}", b.name.downcase route = sprintf "%s {x} {y}", b.shortcut
desc = sprintf "Build %s at x,y", b.name desc = sprintf "Build %s at x,y", b.name
@router.add route, desc do |p| @router.add route, desc do |p|
x = p["x"].to_i32 x = p["x"].to_i32
@ -54,7 +54,7 @@ class App
printf "|" printf "|"
(0...cols).each do |y| (0...cols).each do |y|
tile = world.map.get(x, y) tile = world.map.get(x, y)
printf "%s %d%d|", tile.letter.colorize(:green), x, y printf "%s %d%d\|", render_tile_letter(tile), x, y
end end
print "\n" print "\n"
printf "|" printf "|"
@ -65,11 +65,7 @@ class App
printf "|" printf "|"
(0...cols).each do |y| (0...cols).each do |y|
tile = world.map.get(x, y) tile = world.map.get(x, y)
if tile.letter == 'v' printf "%6s|", render_tile_number(tile).to_s
printf "%6d|", world.map.get(x, y).cur
else
printf " |", world.map.get(x, y).cur
end
end end
print "\n" print "\n"
printf "+" printf "+"
@ -80,6 +76,43 @@ class App
end end
end end
def render_tile_letter(tile : Game::Tile)
case tile
when Game::ConstructionSiteTile then '_'.colorize(:red)
when Game::DepositTile then render_deposit_resource tile.dep.type
when Game::BuildingTile then render_building_letter tile.building.type
else
' '
end
end
def render_building_letter(building_type : Game::Building::Type)
case building_type
when Game::Building::Type::StartPoint then 'S'.colorize(:yellow)
when Game::Building::Type::CrystalMiner then 'M'.colorize(:yellow)
when Game::Building::Type::CrystalRestorer then 'R'.colorize(:yellow)
when Game::Building::Type::Terraformer then 'T'.colorize(:yellow)
else
' '
end
end
def render_deposit_resource(res_type : Game::Resource::Type)
case res_type
when Game::Resource::Type::Crystals then 'v'.colorize(:blue)
else
' '
end
end
def render_tile_number(tile : Game::Tile)
case tile
when Game::DepositTile then tile.dep.cur
else
' '
end
end
def render_commands(world) def render_commands(world)
items = world.queue.top(5) items = world.queue.top(5)
if items.size != 0 if items.size != 0
@ -99,8 +132,8 @@ class App
def render_resources(world) def render_resources(world)
printf "Resources:\n Crystals: %5d\n Terraformation: %5d\n", printf "Resources:\n Crystals: %5d\n Terraformation: %5d\n",
world.resources[Game::Resources::Type::Crystals], world.resources[Game::Resource::Type::Crystals],
world.resources[Game::Resources::Type::Terraformation] world.resources[Game::Resource::Type::Terraformation]
end end
def render_world(world) def render_world(world)

View File

@ -45,7 +45,6 @@ module Game
enum Type enum Type
StartPoint StartPoint
Storehouse
CrystalMiner CrystalMiner
CrystalRestorer CrystalRestorer
Terraformer Terraformer
@ -60,7 +59,8 @@ module Game
production : Production | Nil = nil, production : Production | Nil = nil,
mining : Mining | Nil = nil, mining : Mining | Nil = nil,
restoration : Mining | Nil = nil, restoration : Mining | Nil = nil,
storage : Capacity | Nil = nil storage : Capacity | Nil = nil,
shortcut : String = ""
) )
@name = name != "" ? name : @type.to_s @name = name != "" ? name : @type.to_s
@roles = roles.nil? ? Array(Role).new : roles.as(Array(Role)) @roles = roles.nil? ? Array(Role).new : roles.as(Array(Role))
@ -69,6 +69,7 @@ module Game
@mining = mining @mining = mining
@restoration = restoration @restoration = restoration
@storage = storage.nil? ? 0 : storage @storage = storage.nil? ? 0 : storage
@shortcut = shortcut
end end
getter type getter type
@ -79,6 +80,7 @@ module Game
getter mining getter mining
getter restoration getter restoration
getter storage getter storage
getter shortcut
def has_role(role : Role) : Bool def has_role(role : Role) : Bool
@roles.includes? role @roles.includes? role

View File

@ -4,11 +4,21 @@ module Game
@items = [] of NamedTuple(t: Building::Type, b: Building) @items = [] of NamedTuple(t: Building::Type, b: Building)
add( add(
Building.new Building::Type::StartPoint, storage: 100 Building.new Building::Type::StartPoint, **{
shortcut: "sp",
storage: 100,
roles: [Building::Role::Storehouse],
}
) )
add( add(
Building.new Building::Type::CrystalMiner, **{ Building.new Building::Type::CrystalMiner, **{
shortcut: "miner",
construction: Construction.new(
ts: 10,
cost: ResourceBag.new,
requirements: [] of Game::Building::Type
),
mining: Mining.new( mining: Mining.new(
ts: 20, ts: 20,
resource: Resource.new(Resource::Type::Crystals, 40), resource: Resource.new(Resource::Type::Crystals, 40),
@ -19,6 +29,7 @@ module Game
add( add(
Building.new Building::Type::CrystalRestorer, **{ Building.new Building::Type::CrystalRestorer, **{
shortcut: "rest",
construction: Construction.new( construction: Construction.new(
ts: 30, ts: 30,
cost: ResourceBag.new({ cost: ResourceBag.new({
@ -36,6 +47,7 @@ module Game
add( add(
Building.new Building::Type::Terraformer, **{ Building.new Building::Type::Terraformer, **{
shortcut: "terr",
construction: Construction.new( construction: Construction.new(
ts: 120, ts: 120,
cost: ResourceBag.new({ cost: ResourceBag.new({

View File

@ -56,7 +56,7 @@ module Game
def desc : String def desc : String
if @holded if @holded
sprintf "Mine %s from %d,%d", @holded.type, @point.x, @point.y sprintf "Mine %s from %d,%d", @holded.as(Resource).type, @point.x, @point.y
else else
sprintf "Wait for resources at %d,%d", @point.x, @point.y sprintf "Wait for resources at %d,%d", @point.x, @point.y
end end
@ -107,7 +107,7 @@ module Game
def desc : String def desc : String
if @holded if @holded
sprintf "Restore %s from %d,%d", @holded.type, @point.x, @point.y sprintf "Restore %s from %d,%d", @holded.as(Resource).type, @point.x, @point.y
else else
sprintf "Wait for resources at %d,%d", @point.x, @point.y sprintf "Wait for resources at %d,%d", @point.x, @point.y
end end

View File

@ -66,11 +66,11 @@ module Game
rnd = Random.new rnd = Random.new
map = Map.new(rows, cols) map = Map.new(rows, cols)
5.times do 5.times do
pnt = Point.new(rnd.rand(0...rows), rnd.rand(0...cols)) point = Point.new(rnd.rand(0...rows), rnd.rand(0...cols))
cap = rnd.rand(2...6) cap = rnd.rand(2...6)
map.set(CrystalTile.new(pnt, cap * 50)) deposit = Deposit.new(Resource::Type::Crystals, cap * 50)
map.set DepositTile.new(point, deposit)
end end
map.set(MainBaseTile.new(Point.new(0, 0)))
map map
end end
end end

View File

@ -1,127 +1,21 @@
module Game module Game
abstract class Tile abstract class Tile
property cap : Int32 = 0
property cur : Int32 = 0
def initialize(@point : Point) def initialize(@point : Point)
end end
getter point getter point
getter cap
getter cur
def has_role(role : TileRole) : Bool
false
end
def letter : Char
' '
end
def withdraw(value)
if value >= @cur
wd = @cur
@cur = 0
wd
else
@cur -= value
value
end
end
def charge(value)
charged = @cur + value
@cur = charged <= @cap ? charged : @cap
end
def can_build?
has_role(TileRole::Plateau)
end
end end
class PlateauTile < Tile class PlateauTile < Tile
def letter : Char
' '
end
def has_role(role : TileRole) : Bool
role == TileRole::Plateau
end
end end
class ConstructionSiteTile < Tile class ConstructionSiteTile < Tile
def letter : Char
'_'
end
def has_role(role : TileRole) : Bool
role == TileRole::ConstructionSite
end
end
class MainBaseTile < Tile
def letter : Char
'W'
end
def has_role(role : TileRole) : Bool
role == TileRole::Warehouse
end
end
class CrystalTile < Tile
def initialize(@point : Point, cap : Int32)
@cap = cap
@cur = cap
end
def letter : Char
'v'
end
def has_role(role : TileRole) : Bool
role == TileRole::CrystalDeposits
end
end
class CrystalHarvesterTile < Tile
def letter : Char
'H'
end
def has_role(role : TileRole) : Bool
role == TileRole::CrystalHarvester
end
end
class CrystalRestorerTile < Tile
def letter : Char
'R'
end
def has_role(role : TileRole) : Bool
role == TileRole::CrystalRestorer
end
end
class TerraformerTile < Tile
def letter : Char
'T'
end
def has_role(role : TileRole) : Bool
role == TileRole::Terraformer
end
end end
class BuildingTile < Tile class BuildingTile < Tile
def initialize(@point : Point, @building : Building) def initialize(@point : Point, @building : Building)
end end
def has_role(role : TileRole) : Bool
role == TileRole::Building
end
getter building getter building
end end