From 3dd067612a3204c28c0e20c7effdd635cfe49d76 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Thu, 3 Oct 2019 10:40:57 -0500 Subject: [PATCH] Stubbed Connection, added #next_entity_id to Player, made Director store and pass to Player the current_tick --- i-mic-rts.rb | 1 + lib/connection.rb | 9 +++++++++ lib/director.rb | 8 +++++++- lib/entity.rb | 15 +++++++-------- lib/player.rb | 12 ++++++++++-- lib/states/game.rb | 6 +++++- 6 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 lib/connection.rb diff --git a/i-mic-rts.rb b/i-mic-rts.rb index 72f581a..25d3fb5 100644 --- a/i-mic-rts.rb +++ b/i-mic-rts.rb @@ -26,5 +26,6 @@ require_relative "lib/entity" require_relative "lib/order" require_relative "lib/director" require_relative "lib/player" +require_relative "lib/connection" IMICRTS::Window.new(width: Gosu.screen_width / 4 * 3, height: Gosu.screen_height / 4 * 3, fullscreen: false, resizable: true).show \ No newline at end of file diff --git a/lib/connection.rb b/lib/connection.rb new file mode 100644 index 0000000..224f63c --- /dev/null +++ b/lib/connection.rb @@ -0,0 +1,9 @@ +class IMICRTS + class Connection + def initialize(*args) + end + + def update + end + end +end \ No newline at end of file diff --git a/lib/director.rb b/lib/director.rb index af4a263..d4a6156 100644 --- a/lib/director.rb +++ b/lib/director.rb @@ -1,13 +1,16 @@ class IMICRTS class Director + attr_reader :current_tick def initialize(map:, players:, networking_mode: :virtual, tick_rate: 10) @map = map @players = players + @connection = IMICRTS::Connection.new(director: self, mode: networking_mode) @networking_mode = networking_mode @tick_rate = tick_rate @last_tick_at = Gosu.milliseconds @tick_time = 1000.0 / @tick_rate + @current_tick = 0 end def update @@ -15,11 +18,14 @@ class IMICRTS @last_tick_at = Gosu.milliseconds tick + @connection.update end end def tick - @players.each(&:tick) + @players.each { |player| player.tick(@current_tick) } + + @current_tick += 1 end def player(id) diff --git a/lib/entity.rb b/lib/entity.rb index 69d41a1..d85fe1a 100644 --- a/lib/entity.rb +++ b/lib/entity.rb @@ -1,7 +1,8 @@ class IMICRTS class Entity - attr_reader :position, :angle, :radius - def initialize(manifest: nil, images:, position:, angle:) + attr_reader :id, :position, :angle, :radius + def initialize(id:, manifest: nil, images:, position:, angle:) + @id = id @manifest = manifest @images = images @position = position @@ -40,20 +41,18 @@ class IMICRTS def rotate_towards(vector) _angle = Gosu.angle(@position.x, @position.y, vector.x, vector.y) - a = (360 + (_angle - @angle)) % 360 + a = (360.0 + (_angle - @angle)) % 360.0 # Fails if vector is directly behind entity if @angle.between?(_angle - 3, _angle + 3) @angle = _angle elsif a < 180 - @angle -= 1 + @angle -= 1.0 else - @angle += 1 + @angle += 1.0 end - @angle %= 360 - - pp [a, _angle, @angle] + @angle %= 360.0 end end end \ No newline at end of file diff --git a/lib/player.rb b/lib/player.rb index f2932f9..09f3a0f 100644 --- a/lib/player.rb +++ b/lib/player.rb @@ -1,6 +1,7 @@ class IMICRTS class Player attr_reader :id, :name, :entities, :orders, :camera + attr_reader :selected_entities def initialize(id:, name: nil) @id = id @name = name ? name : "Novice-#{id}" @@ -8,13 +9,20 @@ class IMICRTS @entities = [] @orders = [] @camera = Camera.new + + @selected_entities = [] + @current_entity_id = 0 end - def tick - puts "Player #{@id}-#{@name} ticked: #{Gosu.milliseconds}" + def tick(tick_id) end def entity(id) + @entities.find { |ent| ent.id == id } + end + + def next_entity_id + @current_entity_id += 1 end end end \ No newline at end of file diff --git a/lib/states/game.rb b/lib/states/game.rb index 242cc48..87deb34 100644 --- a/lib/states/game.rb +++ b/lib/states/game.rb @@ -2,10 +2,12 @@ class IMICRTS class Game < CyberarmEngine::GuiState def setup window.show_cursor = true - @selected_entities = [] @player = Player.new(id: 0) @director = Director.new(map: nil, players: [@player]) + + @selected_entities = [] + @mouse_pos = CyberarmEngine::Text.new("X: 0\nY: 0", x: 500, y: 10, z: Float::INFINITY) @sidebar = stack(height: 1.0) do @@ -17,6 +19,7 @@ class IMICRTS @buttons = stack(width: 75) do @h = button("Harvester", width: 1.0) do @player.entities << Entity.new( + id: @player.next_entity_id, images: Gosu::Image.new("#{ASSETS_PATH}/vehicles/harvester/images/harvester.png", retro: true), position: CyberarmEngine::Vector.new(rand(window.width), rand(window.height), ZOrder::GROUND_VEHICLE), angle: rand(360) @@ -24,6 +27,7 @@ class IMICRTS end @c = button("Construction Worker", width: 1.0) do @player.entities << Entity.new( + id: @player.next_entity_id, images: Gosu::Image.new("#{ASSETS_PATH}/vehicles/construction_worker/images/construction_worker.png", retro: true), position: CyberarmEngine::Vector.new(rand(window.width), rand(window.height), ZOrder::GROUND_VEHICLE), angle: rand(360)