mirror of
https://github.com/cyberarm/i-mic-rts.git
synced 2025-12-13 06:52:33 +00:00
Further improvements to place entity/building tool
This commit is contained in:
@@ -107,7 +107,7 @@ class IMICRTS
|
||||
|
||||
def spawn_entity(player_id:, name:, position:)
|
||||
_player = player(player_id)
|
||||
_player.entities << Entity.new(
|
||||
ent = Entity.new(
|
||||
name: name,
|
||||
director: self,
|
||||
player: _player,
|
||||
@@ -115,6 +115,9 @@ class IMICRTS
|
||||
position: position,
|
||||
angle: 0
|
||||
)
|
||||
_player.entities << ent
|
||||
|
||||
return ent
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
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],
|
||||
[false, false, true, false, false],
|
||||
|
||||
@@ -10,7 +10,22 @@ class IMICRTS
|
||||
end
|
||||
|
||||
def draw
|
||||
# TODO: draw affected tiles
|
||||
each_tile(vector_to_grid(@game.window.mouse)) do |x, y, tile|
|
||||
if tile.entity || tile.type != :ground || @director.map.ore_at(x, y) # tile unavailable
|
||||
Gosu.draw_rect(
|
||||
tile.position.x + 2, tile.position.y + 2,
|
||||
@director.map.tile_size - 4, @director.map.tile_size - 4,
|
||||
Gosu::Color.rgba(200, 50, 50, 200), ZOrder::OVERLAY
|
||||
)
|
||||
else # tile available
|
||||
Gosu.draw_rect(
|
||||
tile.position.x + 2, tile.position.y + 2,
|
||||
@director.map.tile_size - 4, @director.map.tile_size - 4,
|
||||
Gosu::Color.rgba(100, 100, 100, 200), ZOrder::OVERLAY
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@preview.draw
|
||||
end
|
||||
|
||||
@@ -18,9 +33,10 @@ class IMICRTS
|
||||
# TODO: ensure that construction worker is alive
|
||||
cancel_tool if @construction_worker.die?
|
||||
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 = position + @director.map.tile_size / 2
|
||||
@preview.position.z = ZOrder::OVERLAY
|
||||
else
|
||||
@preview.position.z = -10
|
||||
@@ -34,16 +50,60 @@ class IMICRTS
|
||||
|
||||
tile = @director.map.tile_at(vector.x, vector.y)
|
||||
return unless tile
|
||||
position = tile.position.clone
|
||||
position = tile.position + @director.map.tile_size / 2
|
||||
|
||||
@director.spawn_entity(
|
||||
ent = @director.spawn_entity(
|
||||
player_id: @player.id, name: @entity,
|
||||
position: CyberarmEngine::Vector.new(position.x, position.y, ZOrder::BUILDING)
|
||||
)
|
||||
|
||||
each_tile(vector) do |x, y, tile|
|
||||
tile.entity = ent
|
||||
end
|
||||
|
||||
cancel_tool
|
||||
end
|
||||
|
||||
def can_use?(vector)
|
||||
return false if @game.sidebar.hit?(@game.window.mouse_x, @game.window.mouse_y)
|
||||
useable = true
|
||||
done = false
|
||||
|
||||
if tile = @director.map.tile_at(vector.x, vector.y)
|
||||
ent = Entity.get(@entity)
|
||||
origin = (tile.grid_position - 2)
|
||||
|
||||
each_tile(vector) do |x, y, tile|
|
||||
if tile.entity || tile.type != :ground || @director.map.ore_at(x, y)
|
||||
useable = false
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
return useable
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def each_tile(vector, &block)
|
||||
if tile = @director.map.tile_at(vector.x, vector.y)
|
||||
ent = Entity.get(@entity)
|
||||
origin = (tile.grid_position - 2)
|
||||
|
||||
ent.tiles.each_with_index do |array, y|
|
||||
array.each_with_index do |space_required, x|
|
||||
next unless space_required
|
||||
|
||||
other_tile = @director.map.tile_at(origin.x + x, origin.y + y)
|
||||
if other_tile
|
||||
block.call(origin.x + x, origin.y + y, other_tile)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def button_down(id)
|
||||
case id
|
||||
when Gosu::MsRight
|
||||
@@ -54,7 +114,7 @@ class IMICRTS
|
||||
def button_up(id)
|
||||
case id
|
||||
when Gosu::MsLeft
|
||||
use_tool(vector_to_grid(@game.window.mouse))
|
||||
use_tool(vector_to_grid(@game.window.mouse)) if can_use?(vector_to_grid(@game.window.mouse))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user