mirror of
https://github.com/cyberarm/i-mic-rts.git
synced 2025-12-14 15:22:34 +00:00
Working Camera#transform 😩
This commit is contained in:
@@ -22,6 +22,8 @@ class IMICRTS
|
|||||||
Gosu.clip_to(@viewport.min.x, @viewport.min.y, @viewport.max.x, @viewport.max.y) do
|
Gosu.clip_to(@viewport.min.x, @viewport.min.y, @viewport.max.x, @viewport.max.y) do
|
||||||
Gosu.transform(*worldspace.elements) do
|
Gosu.transform(*worldspace.elements) do
|
||||||
block.call
|
block.call
|
||||||
|
|
||||||
|
Gosu.draw_line(@drag_start.x, @drag_start.y, Gosu::Color::RED, window.mouse.x, window.mouse_x, Gosu::Color::RED, Float::INFINITY) if @drag_start
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -37,14 +39,14 @@ class IMICRTS
|
|||||||
@viewport.max.y = window.height
|
@viewport.max.y = window.height
|
||||||
end
|
end
|
||||||
|
|
||||||
def pick(vector)
|
def transform(vector)
|
||||||
neg_center = CyberarmEngine::Vector.new(-center.x, -center.y)
|
neg_center = CyberarmEngine::Vector.new(-center.x, -center.y)
|
||||||
|
|
||||||
scaled_position = ((vector - (@position)) / @zoom)
|
scaled_position = (vector) / @zoom
|
||||||
scaled_translation = (neg_center / @zoom + center)
|
scaled_translation = (neg_center / @zoom + center)
|
||||||
# TODO: Fix error caused when @position != 0
|
|
||||||
|
|
||||||
inverse = (scaled_position + scaled_translation)
|
inverse = (scaled_position + scaled_translation) - @position
|
||||||
|
|
||||||
inverse.x = inverse.x.floor
|
inverse.x = inverse.x.floor
|
||||||
inverse.y = inverse.y.floor
|
inverse.y = inverse.y.floor
|
||||||
|
|
||||||
@@ -84,14 +86,14 @@ class IMICRTS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def move
|
def move
|
||||||
@velocity.x += @scroll_speed * window.dt if Gosu.button_down?(Gosu::KB_LEFT) || window.mouse_x < 15
|
@velocity.x += (@scroll_speed / @zoom) * window.dt if Gosu.button_down?(Gosu::KB_LEFT) || window.mouse_x < 15
|
||||||
@velocity.x -= @scroll_speed * window.dt if Gosu.button_down?(Gosu::KB_RIGHT) || window.mouse_x > window.width - 15
|
@velocity.x -= (@scroll_speed / @zoom) * window.dt if Gosu.button_down?(Gosu::KB_RIGHT) || window.mouse_x > window.width - 15
|
||||||
@velocity.y += @scroll_speed * window.dt if Gosu.button_down?(Gosu::KB_UP) || window.mouse_y < 15
|
@velocity.y += (@scroll_speed / @zoom) * window.dt if Gosu.button_down?(Gosu::KB_UP) || window.mouse_y < 15
|
||||||
@velocity.y -= @scroll_speed * window.dt if Gosu.button_down?(Gosu::KB_DOWN) || window.mouse_y > window.height - 15
|
@velocity.y -= (@scroll_speed / @zoom) * window.dt if Gosu.button_down?(Gosu::KB_DOWN) || window.mouse_y > window.height - 15
|
||||||
|
|
||||||
if @drag_start
|
if @drag_start
|
||||||
@velocity *= 0.0
|
@velocity *= 0.0
|
||||||
@position = @position.lerp(@drag_start - CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y), @grab_drag)
|
@position = @position.lerp(@drag_start - window.mouse.clone, @grab_drag / @zoom)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -105,7 +107,7 @@ class IMICRTS
|
|||||||
when Gosu::MS_WHEEL_DOWN
|
when Gosu::MS_WHEEL_DOWN
|
||||||
@zoom = (@zoom - 0.25).clamp(@min_zoom, @max_zoom)
|
@zoom = (@zoom - 0.25).clamp(@min_zoom, @max_zoom)
|
||||||
when Gosu::MS_MIDDLE
|
when Gosu::MS_MIDDLE
|
||||||
@drag_start = pick(CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y))
|
@drag_start = transform(window.mouse)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
class IMICRTS
|
class IMICRTS
|
||||||
class Map
|
class Map
|
||||||
Tile = Struct.new(:position, :color, :image)
|
Tile = Struct.new(:position, :color, :image, :state)
|
||||||
|
|
||||||
def initialize(width:, height:, tile_size: 32)
|
def initialize(width:, height:, tile_size: 32)
|
||||||
@width, @height = width, height
|
@width, @height = width, height
|
||||||
@@ -16,7 +16,9 @@ class IMICRTS
|
|||||||
Tile.new(
|
Tile.new(
|
||||||
CyberarmEngine::Vector.new(x * @tile_size, y * @tile_size, ZOrder::TILE),
|
CyberarmEngine::Vector.new(x * @tile_size, y * @tile_size, ZOrder::TILE),
|
||||||
Gosu::Color.rgb(rand(25), rand(150..200), rand(25)),
|
Gosu::Color.rgb(rand(25), rand(150..200), rand(25)),
|
||||||
@tileset.sample
|
@tileset.sample,
|
||||||
|
# :unexplored
|
||||||
|
:visible
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
@@ -50,7 +52,7 @@ class IMICRTS
|
|||||||
next if _y < 0 || _y > @height
|
next if _y < 0 || _y > @height
|
||||||
|
|
||||||
if tile = tile_at(_x, _y)
|
if tile = tile_at(_x, _y)
|
||||||
_tiles.push(tile)
|
_tiles.push(tile) if tile.state != :unexplored
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ class IMICRTS
|
|||||||
center = @player.camera.center - @player.camera.position
|
center = @player.camera.center - @player.camera.position
|
||||||
draw_rect(center.x - 10, center.y - 10, 20, 20, Gosu::Color::RED, Float::INFINITY)
|
draw_rect(center.x - 10, center.y - 10, 20, 20, Gosu::Color::RED, Float::INFINITY)
|
||||||
|
|
||||||
mouse = @player.camera.pick(window.mouse)
|
mouse = @player.camera.transform(window.mouse)
|
||||||
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)
|
||||||
|
|
||||||
draw_rect(@goal.x - 10, @goal.y - 10, 20, 20, Gosu::Color::WHITE, Float::INFINITY) if @goal
|
draw_rect(@goal.x - 10, @goal.y - 10, 20, 20, Gosu::Color::WHITE, Float::INFINITY) if @goal
|
||||||
@@ -88,7 +88,7 @@ class IMICRTS
|
|||||||
select_entities
|
select_entities
|
||||||
end
|
end
|
||||||
|
|
||||||
mouse = @player.camera.pick(window.mouse)
|
mouse = @player.camera.transform(window.mouse)
|
||||||
@debug_info.text = %(
|
@debug_info.text = %(
|
||||||
FPS: #{Gosu.fps}
|
FPS: #{Gosu.fps}
|
||||||
Aspect Ratio: #{@player.camera.aspect_ratio}
|
Aspect Ratio: #{@player.camera.aspect_ratio}
|
||||||
@@ -96,8 +96,8 @@ class IMICRTS
|
|||||||
Window Mouse X: #{window.mouse.x}
|
Window Mouse X: #{window.mouse.x}
|
||||||
Window Mouse Y: #{window.mouse.y}
|
Window Mouse Y: #{window.mouse.y}
|
||||||
|
|
||||||
Camera Position X: #{@player.camera.position.x}
|
Camera Position X: #{@player.camera.position.x / @player.camera.zoom}
|
||||||
Camera Position Y: #{@player.camera.position.y}
|
Camera Position Y: #{@player.camera.position.y / @player.camera.zoom}
|
||||||
|
|
||||||
World Mouse X: #{mouse.x}
|
World Mouse X: #{mouse.x}
|
||||||
World Mouse Y: #{mouse.y}
|
World Mouse Y: #{mouse.y}
|
||||||
@@ -114,7 +114,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)
|
||||||
@selection_start = @player.camera.pick(window.mouse)
|
@selection_start = @player.camera.transform(window.mouse)
|
||||||
end
|
end
|
||||||
when Gosu::MS_RIGHT
|
when Gosu::MS_RIGHT
|
||||||
@anchor = nil
|
@anchor = nil
|
||||||
@@ -127,7 +127,7 @@ class IMICRTS
|
|||||||
|
|
||||||
case id
|
case id
|
||||||
when Gosu::MS_RIGHT
|
when Gosu::MS_RIGHT
|
||||||
@goal = @player.camera.pick(window.mouse)
|
@goal = @player.camera.transform(window.mouse)
|
||||||
when Gosu::MS_LEFT
|
when Gosu::MS_LEFT
|
||||||
@box = nil
|
@box = nil
|
||||||
@selection_start = nil
|
@selection_start = nil
|
||||||
@@ -139,7 +139,7 @@ class IMICRTS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def select_entities
|
def select_entities
|
||||||
@box = CyberarmEngine::BoundingBox.new(@selection_start, @player.camera.pick(window.mouse))
|
@box = CyberarmEngine::BoundingBox.new(@selection_start, @player.camera.transform(window.mouse))
|
||||||
|
|
||||||
selected_entities = @player.entities.select do |ent|
|
selected_entities = @player.entities.select do |ent|
|
||||||
@box.point?(ent.position - ent.radius) ||
|
@box.point?(ent.position - ent.radius) ||
|
||||||
|
|||||||
Reference in New Issue
Block a user