Improved place buildings tool to use the grid

This commit is contained in:
2019-11-28 08:17:19 -06:00
parent 8869ddb3de
commit e3d1a84efa
9 changed files with 95 additions and 21 deletions

View File

@@ -1,4 +1,12 @@
IMICRTS::Entity.define_entity(:barracks, :building, 400, "Builds and soldiers") do |entity|
tiles = [
[false, false, false, false, false],
[false, true, true, true, false],
[false, true, true, true, false],
[false, true, true, true, false],
[false, true, true, true, false],
]
IMICRTS::Entity.define_entity(:barracks, :building, 400, "Builds and soldiers", tiles) do |entity|
entity.has(:build_queue)
entity.radius = 44

View File

@@ -1,4 +1,12 @@
IMICRTS::Entity.define_entity(:construction_yard, :building, 2_000, "Provides radar and builds construction workers") do |entity|
tiles = [
[false, false, false, false, false],
[false, true, true, true, false],
[false, true, true, true, false],
[false, true, true, true, false],
[false, true, true, true, false],
]
IMICRTS::Entity.define_entity(:construction_yard, :building, 2_000, "Provides radar and builds construction workers", tiles) do |entity|
entity.has(:build_queue)
entity.has(:sidebar_actions)
entity.component(:sidebar_actions).add(:add_to_build_queue, {entity: :construction_worker})

View File

@@ -1,4 +1,12 @@
IMICRTS::Entity.define_entity(:helipad, :building, 1_000, "Builds and rearms helicopters") do |entity|
tiles = [
[false, false, false, false, false],
[false, false, true, false, false],
[false, true, true, true, false],
[false, false, true, false, false],
[false, false, false, false, false],
]
IMICRTS::Entity.define_entity(:helipad, :building, 1_000, "Builds and rearms helicopters", tiles) do |entity|
entity.has(:build_queue)
entity.has(:sidebar_actions)
entity.component(:sidebar_actions).add(:add_to_build_queue, {entity: :helicopter})

View File

@@ -1,4 +1,12 @@
IMICRTS::Entity.define_entity(:power_plant, :building, 800, "Generates power") do |entity|
tiles = [
[false, false, false, false, false],
[false, false, false, false, false],
[false, false, true, false, false],
[false, false, true, false, false],
[false, false, true, false, false],
]
IMICRTS::Entity.define_entity(:power_plant, :building, 800, "Generates power", tiles) do |entity|
entity.radius = 24
entity.max_health = 100.0

View File

@@ -1,4 +1,12 @@
IMICRTS::Entity.define_entity(:refinery, :building, 1_400, "Generates credits") do |entity|
tiles = [
[false, false, false, false, false],
[false, true, true, true, false],
[false, true, true, true, false],
[false, true, true, true, false],
[false, true, true, true, false],
]
IMICRTS::Entity.define_entity(:refinery, :building, 1_400, "Generates credits", tiles) do |entity|
entity.radius = 44
entity.max_health = 100.0

View File

@@ -1,4 +1,12 @@
IMICRTS::Entity.define_entity(:war_factory, :building, 2_000, "Builds units") do |entity|
tiles = [
[false, false, false, false, false],
[false, true, true, true, false],
[false, true, true, true, false],
[false, true, true, true, false],
[false, true, true, true, false],
]
IMICRTS::Entity.define_entity(:war_factory, :building, 2_000, "Builds units", tiles) do |entity|
entity.has(:build_queue)
entity.has(:sidebar_actions)
entity.component(:sidebar_actions).add(:add_to_build_queue, {entity: :jeep})

View File

@@ -1,17 +1,17 @@
class IMICRTS
class Entity
Stub = Struct.new(:name, :type, :cost, :description, :setup)
Stub = Struct.new(:name, :type, :cost, :description, :tiles, :setup)
@entities = {}
def self.get(name)
@entities.dig(name)
end
def self.define_entity(name, type, cost, description, &block)
def self.define_entity(name, type, cost, description, tiles = [[]], &block)
if entity = get(name)
raise "#{name.inspect} is already defined!"
else
@entities[name] = Stub.new(name, type, cost, description, block)
@entities[name] = Stub.new(name, type, cost, description, tiles, block)
end
end

View File

@@ -41,6 +41,20 @@ class IMICRTS
def cancel_tool
@game.set_tool(nil)
end
def use_tool(vector, options = {})
end
def can_use?(vector)
end
def vector_to_grid(vector)
temp = @player.camera.transform(vector)
temp.x = (temp.x / @director.map.tile_size).floor
temp.y = (temp.y / @director.map.tile_size).floor
return temp
end
end
end

View File

@@ -17,12 +17,33 @@ class IMICRTS
def update
# TODO: ensure that construction worker is alive
cancel_tool if @construction_worker.die?
@preview.position = @player.camera.transform(@game.window.mouse)
@preview.position.z = ZOrder::OVERLAY
vector = vector_to_grid(@game.window.mouse)
if tile = @director.map.tile_at(vector.x, vector.y)
position = tile.position.clone
@preview.position = position
@preview.position.z = ZOrder::OVERLAY
else
@preview.position.z = -10
end
@preview.color.alpha = 150
end
def use_tool(vector)
return if @game.sidebar.hit?(@game.window.mouse_x, @game.window.mouse_y)
tile = @director.map.tile_at(vector.x, vector.y)
return unless tile
position = tile.position.clone
@director.spawn_entity(
player_id: @player.id, name: @entity,
position: CyberarmEngine::Vector.new(position.x, position.y, ZOrder::BUILDING)
)
cancel_tool
end
def button_down(id)
case id
when Gosu::MsRight
@@ -33,16 +54,7 @@ class IMICRTS
def button_up(id)
case id
when Gosu::MsLeft
return if @game.sidebar.hit?(@game.window.mouse_x, @game.window.mouse_y)
transform = @player.camera.transform(@game.window.mouse)
@director.spawn_entity(
player_id: @player.id, name: @entity,
position: CyberarmEngine::Vector.new(transform.x, transform.y, ZOrder::BUILDING)
)
cancel_tool
use_tool(vector_to_grid(@game.window.mouse))
end
end
end