mirror of
https://github.com/cyberarm/i-mic-rts.git
synced 2025-12-14 23:32:35 +00:00
Basic unit selection implemented
This commit is contained in:
@@ -18,8 +18,8 @@ class IMICRTS
|
|||||||
|
|
||||||
def draw(&block)
|
def draw(&block)
|
||||||
if block
|
if block
|
||||||
Gosu.translate(@position.x, @position.y) do
|
|
||||||
center_point = center
|
center_point = center
|
||||||
|
Gosu.translate(@position.x, @position.y) do
|
||||||
Gosu.scale(@zoom, @zoom, center.x, center.y) do
|
Gosu.scale(@zoom, @zoom, center.x, center.y) do
|
||||||
block.call
|
block.call
|
||||||
end
|
end
|
||||||
@@ -28,6 +28,8 @@ class IMICRTS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
|
@zoom = 1.0
|
||||||
|
|
||||||
move
|
move
|
||||||
@velocity *= @drag
|
@velocity *= @drag
|
||||||
|
|
||||||
@@ -35,9 +37,9 @@ class IMICRTS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def mouse_pick(x, y)
|
def mouse_pick(x, y)
|
||||||
mouse = CyberarmEngine::Vector.new(x, y) / @zoom
|
mouse = CyberarmEngine::Vector.new(x, y)
|
||||||
|
|
||||||
worldspace = (mouse - @position)
|
worldspace = (mouse - @position) / @zoom
|
||||||
worldspace.x = worldspace.x.floor
|
worldspace.x = worldspace.x.floor
|
||||||
worldspace.y = worldspace.y.floor
|
worldspace.y = worldspace.y.floor
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,41 @@
|
|||||||
class IMICRTS
|
class IMICRTS
|
||||||
class Entity
|
class Entity
|
||||||
def initialize(images:, position:, angle:)
|
attr_reader :position, :angle
|
||||||
|
def initialize(manifest: nil, images:, position:, angle:)
|
||||||
|
@manifest = manifest
|
||||||
@images = images
|
@images = images
|
||||||
@position = position
|
@position = position
|
||||||
@angle = angle
|
@angle = angle
|
||||||
|
|
||||||
|
@radius = 32 / 2
|
||||||
|
end
|
||||||
|
|
||||||
|
def hit?(x_or_vector, y = nil)
|
||||||
|
vector = nil
|
||||||
|
if x_or_vector.is_a?(CyberarmEngine::Vector)
|
||||||
|
vector = x_or_vector
|
||||||
|
else
|
||||||
|
raise "Y cannot be nil!" if y.nil?
|
||||||
|
vector = CyberarmEngine::Vector.new(x_or_vector, y)
|
||||||
|
end
|
||||||
|
|
||||||
|
@position.distance(vector) < @radius + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
@images.draw_rot(@position.x, @position.y, @position.z, @angle)
|
@images.draw_rot(@position.x, @position.y, @position.z, @angle)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def selected_draw
|
||||||
|
draw_bounding_box
|
||||||
|
draw_gizmos
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw_bounding_box
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw_gizmos
|
||||||
|
Gosu.draw_rect(@position.x - @radius, @position.y - (@radius + 2), @radius * 2, 2, Gosu::Color::GREEN, ZOrder::ENTITY_GIZMOS)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -2,6 +2,7 @@ class IMICRTS
|
|||||||
class Game < CyberarmEngine::GuiState
|
class Game < CyberarmEngine::GuiState
|
||||||
def setup
|
def setup
|
||||||
@units = []
|
@units = []
|
||||||
|
@selected_entities = []
|
||||||
@camera = Camera.new
|
@camera = Camera.new
|
||||||
@mouse_pos = CyberarmEngine::Text.new("X: 0\nY: 0", x: 500, y: 10, z: Float::INFINITY)
|
@mouse_pos = CyberarmEngine::Text.new("X: 0\nY: 0", x: 500, y: 10, z: Float::INFINITY)
|
||||||
|
|
||||||
@@ -42,12 +43,13 @@ class IMICRTS
|
|||||||
|
|
||||||
@camera.draw do
|
@camera.draw do
|
||||||
@units.each(&:draw)
|
@units.each(&:draw)
|
||||||
|
@selected_entities.each(&:selected_draw)
|
||||||
|
|
||||||
draw_rect(@anchor.x - 10, @anchor.y - 10, 20, 20, Gosu::Color::RED, Float::INFINITY) if @anchor
|
# draw_rect(@camera.center.x - 10, @camera.center.y - 10, 20, 20, Gosu::Color::BLACK, Float::INFINITY)
|
||||||
draw_rect(@camera.center.x - 10, @camera.center.y - 10, 20, 20, Gosu::Color::BLACK, Float::INFINITY)
|
|
||||||
|
|
||||||
mouse = @camera.mouse_pick(window.mouse_x, window.mouse_y)
|
# mouse = @camera.mouse_pick(window.mouse_x, window.mouse_y)
|
||||||
draw_rect(mouse.x - 10, mouse.y - 10, 20, 20, Gosu::Color::YELLOW, Float::INFINITY)
|
# draw_rect(mouse.x - 10, mouse.y - 10, 20, 20, Gosu::Color::YELLOW, Float::INFINITY)
|
||||||
|
Gosu.draw_rect(@box.min.x, @box.min.y, @box.width, @box.height, Gosu::Color.rgba(50, 50, 50, 150), Float::INFINITY) if @box
|
||||||
end
|
end
|
||||||
|
|
||||||
@mouse_pos.draw
|
@mouse_pos.draw
|
||||||
@@ -58,8 +60,8 @@ class IMICRTS
|
|||||||
|
|
||||||
@camera.update
|
@camera.update
|
||||||
|
|
||||||
if @anchor
|
if @selection_start
|
||||||
@camera.center_around(@anchor, 0.9)
|
select_entities
|
||||||
end
|
end
|
||||||
|
|
||||||
mouse = @camera.mouse_pick(window.mouse_x, window.mouse_y)
|
mouse = @camera.mouse_pick(window.mouse_x, window.mouse_y)
|
||||||
@@ -72,7 +74,7 @@ class IMICRTS
|
|||||||
case id
|
case id
|
||||||
when Gosu::MS_LEFT
|
when Gosu::MS_LEFT
|
||||||
unless @sidebar.hit?(window.mouse_x, window.mouse_y)
|
unless @sidebar.hit?(window.mouse_x, window.mouse_y)
|
||||||
@anchor = @camera.mouse_pick(window.mouse_x, window.mouse_y)
|
@selection_start = CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y) - @camera.position
|
||||||
end
|
end
|
||||||
when Gosu::MS_RIGHT
|
when Gosu::MS_RIGHT
|
||||||
@anchor = nil
|
@anchor = nil
|
||||||
@@ -83,9 +85,29 @@ class IMICRTS
|
|||||||
def button_up(id)
|
def button_up(id)
|
||||||
super
|
super
|
||||||
|
|
||||||
|
case id
|
||||||
|
when Gosu::MS_LEFT
|
||||||
|
@box = nil
|
||||||
|
@selection_start = nil
|
||||||
|
end
|
||||||
|
|
||||||
@camera.button_up(id)
|
@camera.button_up(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def select_entities
|
||||||
|
@box = CyberarmEngine::BoundingBox.new(@selection_start, CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y) - @camera.position)
|
||||||
|
|
||||||
|
selected_entities = @units.select do |ent|
|
||||||
|
@box.point?(ent.position)
|
||||||
|
end
|
||||||
|
|
||||||
|
if Gosu.button_down?(Gosu::KB_LEFT_SHIFT) || Gosu.button_down?(Gosu::KB_RIGHT_SHIFT)
|
||||||
|
@selected_entities = @selected_entities.union(selected_entities)
|
||||||
|
else
|
||||||
|
@selected_entities = selected_entities
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def finalize
|
def finalize
|
||||||
# TODO: Release bound objects/remove self from Window.states array
|
# TODO: Release bound objects/remove self from Window.states array
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,7 +2,14 @@ class IMICRTS
|
|||||||
class ZOrder
|
class ZOrder
|
||||||
base_z = 5
|
base_z = 5
|
||||||
enum = [
|
enum = [
|
||||||
:GROUND_VEHICLE
|
:TILE,
|
||||||
|
:DECORATION,
|
||||||
|
:GROUND_VEHICLE,
|
||||||
|
:BUILDING,
|
||||||
|
:AIR_VEHICLE,
|
||||||
|
|
||||||
|
:ENTITY_BOUNDING_BOX,
|
||||||
|
:ENTITY_GIZMOS, # Health bar and the like
|
||||||
]
|
]
|
||||||
|
|
||||||
enum.each_with_index do |constant, index|
|
enum.each_with_index do |constant, index|
|
||||||
|
|||||||
Reference in New Issue
Block a user