mirror of
https://github.com/cyberarm/i-mic-rts.git
synced 2025-12-14 23:32:35 +00:00
Implemented Zoom
This commit is contained in:
@@ -1,12 +1,17 @@
|
|||||||
class IMICRTS
|
class IMICRTS
|
||||||
class Camera
|
class Camera
|
||||||
attr_reader :position, :velocity, :drag
|
attr_reader :position, :velocity, :zoom, :drag
|
||||||
def initialize(scroll_speed: 10, position: CyberarmEngine::Vector.new(0.0, 0.0))
|
def initialize(scroll_speed: 10, position: CyberarmEngine::Vector.new(0.0, 0.0))
|
||||||
@scroll_speed = scroll_speed
|
@scroll_speed = scroll_speed
|
||||||
@position = position
|
@position = position
|
||||||
@velocity = CyberarmEngine::Vector.new(0.0, 0.0)
|
@velocity = CyberarmEngine::Vector.new(0.0, 0.0)
|
||||||
|
|
||||||
@drag = 0.8
|
@zoom = 1.0
|
||||||
|
@min_zoom = 0.25
|
||||||
|
@max_zoom = 4.0
|
||||||
|
|
||||||
|
@drag = 0.8 # Used to arrest velocity
|
||||||
|
@grab_drag = 0.5 # Used when camera is panned using middle mouse button
|
||||||
end
|
end
|
||||||
|
|
||||||
def window; $window; end
|
def window; $window; end
|
||||||
@@ -14,7 +19,10 @@ class IMICRTS
|
|||||||
def draw(&block)
|
def draw(&block)
|
||||||
if block
|
if block
|
||||||
Gosu.translate(@position.x, @position.y) do
|
Gosu.translate(@position.x, @position.y) do
|
||||||
block.call
|
center_point = center
|
||||||
|
Gosu.scale(@zoom, @zoom, center.x, center.y) do
|
||||||
|
block.call
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -26,8 +34,18 @@ class IMICRTS
|
|||||||
@position += @velocity * @scroll_speed
|
@position += @velocity * @scroll_speed
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def mouse_pick(x, y)
|
||||||
|
mouse = CyberarmEngine::Vector.new(x, y)
|
||||||
|
|
||||||
|
normalized = (mouse / @zoom - @position / @zoom) * @zoom
|
||||||
|
normalized.x = normalized.x.floor
|
||||||
|
normalized.y = normalized.y.floor
|
||||||
|
|
||||||
|
return normalized
|
||||||
|
end
|
||||||
|
|
||||||
def center
|
def center
|
||||||
CyberarmEngine::Vector.new(window.width / 2, window.height / 2) - @position
|
(CyberarmEngine::Vector.new(window.width / 2, window.height / 2) / @zoom - @position / @zoom) * @zoom
|
||||||
end
|
end
|
||||||
|
|
||||||
def center_around(vector, factor)
|
def center_around(vector, factor)
|
||||||
@@ -48,7 +66,7 @@ class IMICRTS
|
|||||||
|
|
||||||
if @drag_start
|
if @drag_start
|
||||||
@velocity *= 0.0
|
@velocity *= 0.0
|
||||||
@position = CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y) - @drag_start
|
@position = lerp(@position, CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y) - @drag_start, @grab_drag)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -57,9 +75,13 @@ class IMICRTS
|
|||||||
when Gosu::KB_H
|
when Gosu::KB_H
|
||||||
@position.x, @position.y = 0.0, 0.0
|
@position.x, @position.y = 0.0, 0.0
|
||||||
@velocity *= 0.0
|
@velocity *= 0.0
|
||||||
|
when Gosu::MS_WHEEL_UP
|
||||||
|
@zoom = (@zoom + 0.25).clamp(@min_zoom, @max_zoom)
|
||||||
|
when Gosu::MS_WHEEL_DOWN
|
||||||
|
@zoom = (@zoom - 0.25).clamp(@min_zoom, @max_zoom)
|
||||||
when Gosu::MS_MIDDLE
|
when Gosu::MS_MIDDLE
|
||||||
@position_start = @position.clone
|
@position_start = @position.clone
|
||||||
@drag_start = CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y) - @position
|
@drag_start = CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -18,11 +18,11 @@ class IMICRTS
|
|||||||
factor = (1.0 - ((Gosu.milliseconds - @started_at) / @close_time.to_f)).clamp(0.0, 1.0)
|
factor = (1.0 - ((Gosu.milliseconds - @started_at) / @close_time.to_f)).clamp(0.0, 1.0)
|
||||||
@color.alpha = 255 * (factor - 0.1)
|
@color.alpha = 255 * (factor - 0.1)
|
||||||
|
|
||||||
window.close if Gosu.milliseconds - @started_at >= @close_time
|
window.close! if Gosu.milliseconds - @started_at >= @close_time
|
||||||
end
|
end
|
||||||
|
|
||||||
def button_up(id)
|
def button_up(id)
|
||||||
window.close if id == Gosu::KB_ESCAPE
|
window.close!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -6,19 +6,18 @@ class IMICRTS
|
|||||||
@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)
|
||||||
|
|
||||||
@sidebar = stack(height: 1.0) do
|
@sidebar = stack(height: 1.0) do
|
||||||
background [0xff555555, Gosu::Color::GRAY]
|
background [0x55555555, 0x55666666]
|
||||||
|
|
||||||
label "SIDEBAR", text_size: 78
|
label "SIDEBAR", text_size: 78, margin_bottom: 20
|
||||||
label ""
|
|
||||||
|
|
||||||
button("Harvest", width: 1.0) do
|
@h = button("Harvester", width: 1.0) do
|
||||||
@units << Entity.new(
|
@units << Entity.new(
|
||||||
images: Gosu::Image.new("assets/vehicles/harvester/images/harvester.png", retro: true),
|
images: Gosu::Image.new("assets/vehicles/harvester/images/harvester.png", retro: true),
|
||||||
position: CyberarmEngine::Vector.new(rand(window.width), rand(window.height), ZOrder::GROUND_VEHICLE),
|
position: CyberarmEngine::Vector.new(rand(window.width), rand(window.height), ZOrder::GROUND_VEHICLE),
|
||||||
angle: rand(360)
|
angle: rand(360)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
button("Construction Worker", width: 1.0) do
|
@c = button("Construction Worker", width: 1.0) do
|
||||||
@units << Entity.new(
|
@units << Entity.new(
|
||||||
images: Gosu::Image.new("assets/vehicles/construction_worker/images/construction_worker.png", retro: true),
|
images: Gosu::Image.new("assets/vehicles/construction_worker/images/construction_worker.png", retro: true),
|
||||||
position: CyberarmEngine::Vector.new(rand(window.width), rand(window.height), ZOrder::GROUND_VEHICLE),
|
position: CyberarmEngine::Vector.new(rand(window.width), rand(window.height), ZOrder::GROUND_VEHICLE),
|
||||||
@@ -27,11 +26,13 @@ class IMICRTS
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
button("Leave", width: 1.0) do
|
button("Leave", width: 1.0, margin_top: 20) do
|
||||||
finalize
|
finalize
|
||||||
push_state(MainMenu)
|
push_state(MainMenu)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
100.times { |i| [@c, @h].sample.instance_variable_get("@block").call }
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
@@ -43,10 +44,10 @@ class IMICRTS
|
|||||||
@units.each(&:draw)
|
@units.each(&:draw)
|
||||||
|
|
||||||
draw_rect(@anchor.x - 10, @anchor.y - 10, 20, 20, Gosu::Color::RED, Float::INFINITY) if @anchor
|
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_center = CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y) - @camera.position
|
mouse = @camera.mouse_pick(window.mouse_x, window.mouse_y)
|
||||||
# draw_rect(mouse_center.x - 10, mouse_center.y - 10, 20, 20, Gosu::Color::YELLOW, Float::INFINITY)
|
draw_rect(mouse.x - 10, mouse.y - 10, 20, 20, Gosu::Color::YELLOW, Float::INFINITY)
|
||||||
end
|
end
|
||||||
|
|
||||||
@mouse_pos.draw
|
@mouse_pos.draw
|
||||||
@@ -61,7 +62,8 @@ class IMICRTS
|
|||||||
@camera.center_around(@anchor, 0.9)
|
@camera.center_around(@anchor, 0.9)
|
||||||
end
|
end
|
||||||
|
|
||||||
@mouse_pos.text = "X: #{window.mouse_x}\nY: #{window.mouse_y}"
|
mouse = @camera.mouse_pick(window.mouse_x, window.mouse_y)
|
||||||
|
@mouse_pos.text = "Zoom: #{@camera.zoom}\nX: #{window.mouse_x}\nY: #{window.mouse_y}\n\nX: #{mouse.x}\nY: #{mouse.y}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def button_down(id)
|
def button_down(id)
|
||||||
@@ -70,12 +72,12 @@ 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 = CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y) - @camera.position
|
@anchor = @camera.mouse_pick(window.mouse_x, window.mouse_y)
|
||||||
end
|
end
|
||||||
when Gosu::MS_RIGHT
|
when Gosu::MS_RIGHT
|
||||||
@anchor = nil
|
@anchor = nil
|
||||||
end
|
end
|
||||||
@camera.button_down(id)
|
@camera.button_down(id) unless @sidebar.hit?(window.mouse_x, window.mouse_y)
|
||||||
end
|
end
|
||||||
|
|
||||||
def button_up(id)
|
def button_up(id)
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ class IMICRTS
|
|||||||
@last_update_time = Gosu.milliseconds
|
@last_update_time = Gosu.milliseconds
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def close
|
||||||
|
push_state(Closing) unless current_state.is_a?(Closing)
|
||||||
|
end
|
||||||
|
|
||||||
def delta_time
|
def delta_time
|
||||||
Gosu.milliseconds - @last_update_time
|
Gosu.milliseconds - @last_update_time
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user