Finish moving game components to Game namespace
This commit is contained in:
parent
ce9d9afd31
commit
de5a36d6d6
@ -2,9 +2,9 @@ require "./spec_helper"
|
|||||||
require "./../src/game/map"
|
require "./../src/game/map"
|
||||||
require "./../src/game/queue"
|
require "./../src/game/queue"
|
||||||
|
|
||||||
describe Point do
|
describe Game::Point do
|
||||||
p1 = Point.new(0, 0)
|
p1 = Game::Point.new(0, 0)
|
||||||
p2 = Point.new(5, 5)
|
p2 = Game::Point.new(5, 5)
|
||||||
it "can calc distance" do
|
it "can calc distance" do
|
||||||
p1.distance(p2).should eq 10
|
p1.distance(p2).should eq 10
|
||||||
end
|
end
|
||||||
|
@ -16,32 +16,32 @@ end
|
|||||||
|
|
||||||
define_dummy_classes(5)
|
define_dummy_classes(5)
|
||||||
|
|
||||||
describe App::Queue do
|
describe Game::Queue do
|
||||||
it "should pop nil on empty queue" do
|
it "should pop nil on empty queue" do
|
||||||
queue = App::Queue.new
|
queue = Game::Queue.new
|
||||||
item = queue.pop(50)
|
item = queue.pop(50)
|
||||||
item.should be_nil
|
item.should be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should pop command on one element queue" do
|
it "should pop command on one element queue" do
|
||||||
queue = App::Queue.new
|
queue = Game::Queue.new
|
||||||
queue.push(10, Test::DummyCommand1.new)
|
queue.push(10, Test::DummyCommand1.new)
|
||||||
item = queue.pop(50)
|
item = queue.pop(50)
|
||||||
item.nil?.should be_false
|
item.nil?.should be_false
|
||||||
item.as(App::Queue::Item).ts.should eq 10
|
item.as(Game::Queue::Item).ts.should eq 10
|
||||||
item.as(App::Queue::Item).command.should be_a(Test::DummyCommand1)
|
item.as(Game::Queue::Item).command.should be_a(Test::DummyCommand1)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should pop commands in proper order" do
|
it "should pop commands in proper order" do
|
||||||
queue = App::Queue.new
|
queue = Game::Queue.new
|
||||||
queue.push(10, Test::DummyCommand1.new)
|
queue.push(10, Test::DummyCommand1.new)
|
||||||
queue.push(50, Test::DummyCommand2.new)
|
queue.push(50, Test::DummyCommand2.new)
|
||||||
queue.push(30, Test::DummyCommand3.new)
|
queue.push(30, Test::DummyCommand3.new)
|
||||||
item1 = queue.pop(100)
|
item1 = queue.pop(100)
|
||||||
item1.as(App::Queue::Item).command.should be_a(Test::DummyCommand1)
|
item1.as(Game::Queue::Item).command.should be_a(Test::DummyCommand1)
|
||||||
item2 = queue.pop(100)
|
item2 = queue.pop(100)
|
||||||
item2.as(App::Queue::Item).command.should be_a(Test::DummyCommand3)
|
item2.as(Game::Queue::Item).command.should be_a(Test::DummyCommand3)
|
||||||
item3 = queue.pop(100)
|
item3 = queue.pop(100)
|
||||||
item3.as(App::Queue::Item).command.should be_a(Test::DummyCommand2)
|
item3.as(Game::Queue::Item).command.should be_a(Test::DummyCommand2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,18 +4,18 @@ require "../src/game/world"
|
|||||||
describe "World" do
|
describe "World" do
|
||||||
it "should build crystal harvester" do
|
it "should build crystal harvester" do
|
||||||
world = Game::World.new
|
world = Game::World.new
|
||||||
point = Point.new(2, 3)
|
point = Game::Point.new(2, 3)
|
||||||
cmd = Game::BuildCrystalHarvesterCommand.new(point)
|
cmd = Game::BuildCrystalHarvesterCommand.new(point)
|
||||||
world.push(cmd)
|
world.push(cmd)
|
||||||
world.run(100)
|
world.run(100)
|
||||||
world.map.get(point).has_role(TileRole::CrystalHarvester)
|
world.map.get(point).has_role(Game::TileRole::CrystalHarvester)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should fail when not enought resources" do
|
it "should fail when not enought resources" do
|
||||||
world = Game::World.new
|
world = Game::World.new
|
||||||
point = Point.new(2, 3)
|
point = Game::Point.new(2, 3)
|
||||||
cmd = Game::BuildCrystalRestorerCommand.new(point)
|
cmd = Game::BuildCrystalRestorerCommand.new(point)
|
||||||
expect_raises(NotEnoughtResources) do
|
expect_raises(Game::NotEnoughtResources) do
|
||||||
world.push(cmd)
|
world.push(cmd)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -12,8 +12,8 @@ router = CLI::CommandRouter.new
|
|||||||
router.add "st" do
|
router.add "st" do
|
||||||
printf "Stat:\n\tTime: %d\n\tCrystals: %d\n\tTarraform: %d\n",
|
printf "Stat:\n\tTime: %d\n\tCrystals: %d\n\tTarraform: %d\n",
|
||||||
world.ts,
|
world.ts,
|
||||||
world.resources[ResourceType::Crystal],
|
world.resources[Game::ResourceType::Crystal],
|
||||||
world.resources[ResourceType::Terraformation]
|
world.resources[Game::ResourceType::Terraformation]
|
||||||
end
|
end
|
||||||
|
|
||||||
router.add "m" do
|
router.add "m" do
|
||||||
@ -29,20 +29,20 @@ end
|
|||||||
router.add "harv {x} {y}" do |p|
|
router.add "harv {x} {y}" do |p|
|
||||||
x = p["x"].to_i32
|
x = p["x"].to_i32
|
||||||
y = p["y"].to_i32
|
y = p["y"].to_i32
|
||||||
world.push(Game::BuildCrystalHarvesterCommand.new(Point.new(x, y)))
|
world.push(Game::BuildCrystalHarvesterCommand.new(Game::Point.new(x, y)))
|
||||||
printf "Build harvester at %d %d\n", x, y
|
printf "Build harvester at %d %d\n", x, y
|
||||||
end
|
end
|
||||||
|
|
||||||
router.add "rest {x} {y}" do |p|
|
router.add "rest {x} {y}" do |p|
|
||||||
x = p["x"].to_i32
|
x = p["x"].to_i32
|
||||||
y = p["y"].to_i32
|
y = p["y"].to_i32
|
||||||
world.push(Game::BuildCrystalRestorerCommand.new(Point.new(x, y)))
|
world.push(Game::BuildCrystalRestorerCommand.new(Game::Point.new(x, y)))
|
||||||
end
|
end
|
||||||
|
|
||||||
router.add "terr {x} {y}" do |p|
|
router.add "terr {x} {y}" do |p|
|
||||||
x = p["x"].to_i32
|
x = p["x"].to_i32
|
||||||
y = p["y"].to_i32
|
y = p["y"].to_i32
|
||||||
world.push(Game::BuildTerraformerCommand.new(Point.new(x, y)))
|
world.push(Game::BuildTerraformerCommand.new(Game::Point.new(x, y)))
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize_command(cmd)
|
def normalize_command(cmd)
|
||||||
|
@ -1,2 +1,4 @@
|
|||||||
class NotEnoughtResources < Exception
|
module Game
|
||||||
|
class NotEnoughtResources < Exception
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
134
src/game/map.cr
134
src/game/map.cr
@ -1,81 +1,83 @@
|
|||||||
struct Point
|
module Game
|
||||||
property x : Int32
|
struct Point
|
||||||
property y : Int32
|
property x : Int32
|
||||||
|
property y : Int32
|
||||||
|
|
||||||
def initialize(@x : Int32, @y : Int32)
|
def initialize(@x : Int32, @y : Int32)
|
||||||
end
|
|
||||||
|
|
||||||
getter x
|
|
||||||
getter y
|
|
||||||
|
|
||||||
def distance(other) : Int32
|
|
||||||
return (other.x - @x).abs + (other.y - @y).abs
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Map
|
|
||||||
SIZE = 4
|
|
||||||
|
|
||||||
def initialize
|
|
||||||
@data = {} of String => Tile
|
|
||||||
(0...SIZE).each do |x|
|
|
||||||
(0...SIZE).each do |y|
|
|
||||||
self.set(PlateauTile.new(Point.new(x, y)))
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
self.set(MainBaseTile.new(Point.new(0, 0)))
|
|
||||||
self.set(CrystalTile.new(Point.new(1, 1), 100))
|
|
||||||
self.set(CrystalTile.new(Point.new(3, 1), 200))
|
|
||||||
self.set(CrystalTile.new(Point.new(2, 2), 100))
|
|
||||||
end
|
|
||||||
|
|
||||||
def get(point : Point) : Tile
|
getter x
|
||||||
@data[key(point)]
|
getter y
|
||||||
end
|
|
||||||
|
|
||||||
def set(tile : Tile)
|
def distance(other) : Int32
|
||||||
@data[key(tile.point)] = tile
|
return (other.x - @x).abs + (other.y - @y).abs
|
||||||
end
|
|
||||||
|
|
||||||
def set(point : Point, tile : Tile)
|
|
||||||
@data[key(point)] = tile
|
|
||||||
end
|
|
||||||
|
|
||||||
def tiles
|
|
||||||
(0...SIZE).each do |x|
|
|
||||||
(0...SIZE).each do |y|
|
|
||||||
point = Point.new(x, y)
|
|
||||||
tile = self.get(point)
|
|
||||||
yield point, tile
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def nearest_tile(point : Point, &block) : Tile | Nil
|
class Map
|
||||||
seek_tile = nil
|
SIZE = 4
|
||||||
min_dist = Int32::MAX
|
|
||||||
tiles do |tile_point, tile|
|
def initialize
|
||||||
if (yield tile)
|
@data = {} of String => Tile
|
||||||
tile_dist = tile_point.distance(point)
|
(0...SIZE).each do |x|
|
||||||
if tile_dist < min_dist
|
(0...SIZE).each do |y|
|
||||||
min_dist = tile_dist
|
self.set(PlateauTile.new(Point.new(x, y)))
|
||||||
seek_tile = tile
|
end
|
||||||
|
end
|
||||||
|
self.set(MainBaseTile.new(Point.new(0, 0)))
|
||||||
|
self.set(CrystalTile.new(Point.new(1, 1), 100))
|
||||||
|
self.set(CrystalTile.new(Point.new(3, 1), 200))
|
||||||
|
self.set(CrystalTile.new(Point.new(2, 2), 100))
|
||||||
|
end
|
||||||
|
|
||||||
|
def get(point : Point) : Tile
|
||||||
|
@data[key(point)]
|
||||||
|
end
|
||||||
|
|
||||||
|
def set(tile : Tile)
|
||||||
|
@data[key(tile.point)] = tile
|
||||||
|
end
|
||||||
|
|
||||||
|
def set(point : Point, tile : Tile)
|
||||||
|
@data[key(point)] = tile
|
||||||
|
end
|
||||||
|
|
||||||
|
def tiles
|
||||||
|
(0...SIZE).each do |x|
|
||||||
|
(0...SIZE).each do |y|
|
||||||
|
point = Point.new(x, y)
|
||||||
|
tile = self.get(point)
|
||||||
|
yield point, tile
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
seek_tile
|
|
||||||
end
|
|
||||||
|
|
||||||
def print
|
def nearest_tile(point : Point, &block) : Tile | Nil
|
||||||
(0...SIZE).each do |x|
|
seek_tile = nil
|
||||||
(0...SIZE).each do |y|
|
min_dist = Int32::MAX
|
||||||
printf "%c", @data[key(Point.new(x, y))].letter
|
tiles do |tile_point, tile|
|
||||||
|
if (yield tile)
|
||||||
|
tile_dist = tile_point.distance(point)
|
||||||
|
if tile_dist < min_dist
|
||||||
|
min_dist = tile_dist
|
||||||
|
seek_tile = tile
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
printf "\n"
|
seek_tile
|
||||||
|
end
|
||||||
|
|
||||||
|
def print
|
||||||
|
(0...SIZE).each do |x|
|
||||||
|
(0...SIZE).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
|
||||||
end
|
end
|
||||||
|
|
||||||
private def key(p : Point) : String
|
|
||||||
return sprintf "%d:%d", p.x, p.y
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
class App::Queue
|
class Game::Queue
|
||||||
struct Item
|
struct Item
|
||||||
def initialize(@ts : Int32, @command : Game::Command)
|
def initialize(@ts : Int32, @command : Game::Command)
|
||||||
end
|
end
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
require "./exception"
|
require "./exception"
|
||||||
|
|
||||||
enum ResourceType
|
enum Game::ResourceType
|
||||||
Crystal
|
Crystal
|
||||||
Terraformation
|
Terraformation
|
||||||
end
|
end
|
||||||
|
|
||||||
class Resources
|
class Game::Resources
|
||||||
def initialize
|
def initialize
|
||||||
@values = {} of ResourceType => Int32
|
@values = {} of ResourceType => Int32
|
||||||
ResourceType.each do |t|
|
ResourceType.each do |t|
|
||||||
|
192
src/game/tile.cr
192
src/game/tile.cr
@ -1,104 +1,106 @@
|
|||||||
enum TileRole
|
module Game
|
||||||
CrystalDeposits
|
enum TileRole
|
||||||
CrystalHarvester
|
CrystalDeposits
|
||||||
CrystalRestorer
|
CrystalHarvester
|
||||||
Plateau
|
CrystalRestorer
|
||||||
Terraformer
|
Plateau
|
||||||
Warehouse
|
Terraformer
|
||||||
end
|
Warehouse
|
||||||
|
|
||||||
abstract class Tile
|
|
||||||
property cap : Int32 = 0
|
|
||||||
property cur : Int32 = 0
|
|
||||||
|
|
||||||
def initialize(@point : Point)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
getter point
|
abstract class Tile
|
||||||
getter cap
|
property cap : Int32 = 0
|
||||||
getter cur
|
property cur : Int32 = 0
|
||||||
|
|
||||||
abstract def letter : Char
|
def initialize(@point : Point)
|
||||||
abstract def has_role(role : TileRole) : Bool
|
end
|
||||||
|
|
||||||
def withdraw(value)
|
getter point
|
||||||
if value >= @cur
|
getter cap
|
||||||
wd = @cur
|
getter cur
|
||||||
@cur = 0
|
|
||||||
wd
|
abstract def letter : Char
|
||||||
else
|
abstract def has_role(role : TileRole) : Bool
|
||||||
@cur -= value
|
|
||||||
value
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def charge(value)
|
class PlateauTile < Tile
|
||||||
charged = @cur + value
|
def letter : Char
|
||||||
@cur = charged <= @cap ? charged : @cap
|
'.'
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
def has_role(role : TileRole) : Bool
|
||||||
class PlateauTile < Tile
|
role == TileRole::Plateau
|
||||||
def letter : Char
|
end
|
||||||
'.'
|
end
|
||||||
end
|
|
||||||
|
class MainBaseTile < Tile
|
||||||
def has_role(role : TileRole) : Bool
|
def letter : Char
|
||||||
role == TileRole::Plateau
|
'H'
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
def has_role(role : TileRole) : Bool
|
||||||
class MainBaseTile < Tile
|
role == TileRole::Warehouse
|
||||||
def letter : Char
|
end
|
||||||
'H'
|
end
|
||||||
end
|
|
||||||
|
class CrystalTile < Tile
|
||||||
def has_role(role : TileRole) : Bool
|
def initialize(@point : Point, cap : Int32)
|
||||||
role == TileRole::Warehouse
|
@cap = cap
|
||||||
end
|
@cur = cap
|
||||||
end
|
end
|
||||||
|
|
||||||
class CrystalTile < Tile
|
def letter : Char
|
||||||
def initialize(@point : Point, cap : Int32)
|
'f'
|
||||||
@cap = cap
|
end
|
||||||
@cur = cap
|
|
||||||
end
|
def has_role(role : TileRole) : Bool
|
||||||
|
role == TileRole::CrystalDeposits
|
||||||
def letter : Char
|
end
|
||||||
'f'
|
end
|
||||||
end
|
|
||||||
|
class CrystalHarvesterTile < Tile
|
||||||
def has_role(role : TileRole) : Bool
|
def letter : Char
|
||||||
role == TileRole::CrystalDeposits
|
'm'
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
def has_role(role : TileRole) : Bool
|
||||||
class CrystalHarvesterTile < Tile
|
role == TileRole::CrystalHarvester
|
||||||
def letter : Char
|
end
|
||||||
'm'
|
end
|
||||||
end
|
|
||||||
|
class CrystalRestorerTile < Tile
|
||||||
def has_role(role : TileRole) : Bool
|
def letter : Char
|
||||||
role == TileRole::CrystalHarvester
|
'h'
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
def has_role(role : TileRole) : Bool
|
||||||
class CrystalRestorerTile < Tile
|
role == TileRole::CrystalRestorer
|
||||||
def letter : Char
|
end
|
||||||
'h'
|
end
|
||||||
end
|
|
||||||
|
class TerraformerTile < Tile
|
||||||
def has_role(role : TileRole) : Bool
|
def letter : Char
|
||||||
role == TileRole::CrystalRestorer
|
'T'
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
def has_role(role : TileRole) : Bool
|
||||||
class TerraformerTile < Tile
|
role == TileRole::Terraformer
|
||||||
def letter : Char
|
end
|
||||||
'T'
|
|
||||||
end
|
|
||||||
|
|
||||||
def has_role(role : TileRole) : Bool
|
|
||||||
role == TileRole::Terraformer
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -6,7 +6,7 @@ class Game::World
|
|||||||
def initialize(@ts = 0)
|
def initialize(@ts = 0)
|
||||||
@map = Map.new
|
@map = Map.new
|
||||||
@resources = Resources.new
|
@resources = Resources.new
|
||||||
@tasks = App::Queue.new
|
@tasks = Queue.new
|
||||||
end
|
end
|
||||||
|
|
||||||
getter ts
|
getter ts
|
||||||
|
Loading…
Reference in New Issue
Block a user