Updated Camera to use Matrix, Map now draws image tiles instead of rects, fixed selected units order pushing full array instead of contents of array.

This commit is contained in:
2019-10-06 21:34:39 -05:00
parent 0c22996b9f
commit cc93b5993d
6 changed files with 40 additions and 35 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 4.6 KiB

View File

@@ -21,18 +21,14 @@ class IMICRTS
if block if block
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
center_point = center center_point = center
Gosu.translate(@position.x, @position.y) do Gosu.transform(*worldspace.elements) do
Gosu.scale(@zoom, @zoom, center.x, center.y) do block.call
block.call
end
end end
end end
end end
end end
def update def update
@zoom = 1.0
move move
@velocity *= @drag @velocity *= @drag
@@ -42,14 +38,12 @@ class IMICRTS
@viewport.max.y = window.height @viewport.max.y = window.height
end end
def mouse_pick(x, y) def pick(vector)
mouse = CyberarmEngine::Vector.new(x, y) inverse = (vector - @position) / @zoom
inverse.x = inverse.x.floor
inverse.y = inverse.y.floor
worldspace = (mouse - @position) / @zoom return inverse
worldspace.x = worldspace.x.floor
worldspace.y = worldspace.y.floor
return worldspace
end end
def center def center
@@ -61,6 +55,13 @@ class IMICRTS
@position += center.lerp(vector, factor) * window.dt @position += center.lerp(vector, factor) * window.dt
end end
def worldspace
zoom_vector = CyberarmEngine::Vector.new(@zoom, @zoom)
position_matrix = CyberarmEngine::Matrix.translate(@position)
CyberarmEngine::Matrix.concat(CyberarmEngine::Matrix.scale(zoom_vector, center), position_matrix)
end
def visible?(object) def visible?(object)
if object.is_a?(Map::Tile) if object.is_a?(Map::Tile)
object.position.x - object.size >= @viewport.min.x - @position.x && object.position.x - object.size >= @viewport.min.x - @position.x &&
@@ -99,7 +100,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 = CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y) - @position @drag_start = pick(CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y))
end end
end end

View File

@@ -1,11 +1,13 @@
class IMICRTS class IMICRTS
class Map class Map
Tile = Struct.new(:position, :color, :size) Tile = Struct.new(:position, :color, :image)
def initialize(width:, height:, tile_size: 32) def initialize(width:, height:, tile_size: 32)
@width, @height = width, height @width, @height = width, height
@tile_size = tile_size @tile_size = tile_size
@tileset = Gosu::Image.load_tiles("#{ASSETS_PATH}/tilesets/default.png", tile_size, tile_size, retro: true)
@tiles = [] @tiles = []
height.times do |y| height.times do |y|
@@ -14,7 +16,7 @@ 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)),
@tile_size @tileset.sample
) )
) )
end end
@@ -23,11 +25,7 @@ class IMICRTS
def draw(camera) def draw(camera)
visible_tiles(camera).each do |tile| visible_tiles(camera).each do |tile|
Gosu.draw_rect( tile.image.draw(tile.position.x, tile.position.y, tile.position.z)
tile.position.x, tile.position.y,
@tile_size, @tile_size,
tile.color, tile.position.z
)
end end
end end
@@ -38,10 +36,10 @@ class IMICRTS
top_left.x = top_left.x.ceil top_left.x = top_left.x.ceil
top_left.y = top_left.y.ceil top_left.y = top_left.y.ceil
top_left /= @tiles.first.size top_left /= @tile_size
# +1 to overdraw a bit to hide pop-in # +1 to overdraw a bit to hide pop-in
_width = ($window.width / @tile_size) + 1 _width = ($window.width / @tile_size) + 1
_height = ($window.height / @tile_size) + 1 _height = ($window.height / @tile_size) + 1
_height.times do |y| _height.times do |y|

View File

@@ -1,5 +1,5 @@
IMICRTS::Order.define_handler(IMICRTS::Order::SELECTED_UNITS, arguments: [:player_id, :ids]) do |order, director| IMICRTS::Order.define_handler(IMICRTS::Order::SELECTED_UNITS, arguments: [:player_id, :ids]) do |order, director|
director.player(order.player_id).selected_entities.clear director.player(order.player_id).selected_entities.clear
director.player(order.player_id).selected_entities.push(order.ids) director.player(order.player_id).selected_entities.push(*order.ids)
end end

View File

@@ -53,17 +53,20 @@ class IMICRTS
def draw def draw
super super
Gosu.draw_rect(0, 0, window.width, window.height, Gosu::Color.rgb(10, 175, 35)) # Gosu.draw_rect(0, 0, window.width, window.height, Gosu::Color.rgb(10, 175, 35))
@player.camera.draw do @player.camera.draw do
@director.map.draw(@player.camera) @director.map.draw(@player.camera)
@director.entities.each(&:draw) @director.entities.each(&:draw)
@selected_entities.each(&:selected_draw) @selected_entities.each(&:selected_draw)
# draw_rect(@camera.center.x - 10, @camera.center.y - 10, 20, 20, Gosu::Color::BLACK, Float::INFINITY) draw_rect(@player.camera.center.x - 10, @player.camera.center.y - 10, 20, 20, Gosu::Color::RED, Float::INFINITY)
mouse = @player.camera.pick(window.mouse)
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
# 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)
Gosu.draw_rect(@box.min.x, @box.min.y, @box.width, @box.height, Gosu::Color.rgba(50, 50, 50, 150), Float::INFINITY) if @box 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
@@ -76,7 +79,7 @@ class IMICRTS
@director.update @director.update
@player.camera.update @player.camera.update
@selected_entities.each do |ent| @player.selected_entities.each do |ent|
ent.rotate_towards(@goal) if @goal ent.rotate_towards(@goal) if @goal
end end
@@ -84,13 +87,13 @@ class IMICRTS
select_entities select_entities
end end
mouse = @player.camera.mouse_pick(window.mouse_x, window.mouse_y) mouse = @player.camera.pick(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}
Zoom: #{@player.camera.zoom} Zoom: #{@player.camera.zoom}
Window Mouse X: #{window.mouse_x} Window Mouse X: #{window.mouse.x}
Window Mouse Y: #{window.mouse_y} Window Mouse Y: #{window.mouse.y}
World Mouse X: #{mouse.x} World Mouse X: #{mouse.x}
World Mouse Y: #{mouse.y} World Mouse Y: #{mouse.y}
@@ -107,7 +110,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 = CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y) - @player.camera.position @selection_start = @player.camera.pick(window.mouse)
end end
when Gosu::MS_RIGHT when Gosu::MS_RIGHT
@anchor = nil @anchor = nil
@@ -120,7 +123,7 @@ class IMICRTS
case id case id
when Gosu::MS_RIGHT when Gosu::MS_RIGHT
@goal = @player.camera.mouse_pick(window.mouse_x, window.mouse_y) @goal = @player.camera.pick(window.mouse)
when Gosu::MS_LEFT when Gosu::MS_LEFT
@box = nil @box = nil
@selection_start = nil @selection_start = nil
@@ -132,7 +135,7 @@ class IMICRTS
end end
def select_entities def select_entities
@box = CyberarmEngine::BoundingBox.new(@selection_start, CyberarmEngine::Vector.new(window.mouse_x, window.mouse_y) - @player.camera.position) @box = CyberarmEngine::BoundingBox.new(@selection_start, @player.camera.pick(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) ||

View File

@@ -1,7 +1,9 @@
class IMICRTS class IMICRTS
class Window < CyberarmEngine::Engine class Window < CyberarmEngine::Engine
attr_reader :mouse
def setup def setup
@last_update_time = Gosu.milliseconds @last_update_time = Gosu.milliseconds
@mouse = CyberarmEngine::Vector.new
self.caption = "#{IMICRTS::NAME} (#{IMICRTS::VERSION} #{IMICRTS::VERSION_NAME})" self.caption = "#{IMICRTS::NAME} (#{IMICRTS::VERSION} #{IMICRTS::VERSION_NAME})"
if ARGV.join.include?("--fast") if ARGV.join.include?("--fast")
@@ -14,6 +16,7 @@ class IMICRTS
end end
def update def update
@mouse.x, @mouse.y = self.mouse_x, self.mouse_y
super super
@last_update_time = Gosu.milliseconds @last_update_time = Gosu.milliseconds