Class: Chingu::GameObjectMap
- Inherits:
-
Object
- Object
- Chingu::GameObjectMap
- Defined in:
- lib/chingu/game_object_map.rb
Overview
** This class is under heavy development, API will most likely change! **
GameObjectMap can convert any set of game objects into a 2D-array for fast lookup You can set any gridsize with :grid, defaults to [32,32]. The smaller the grid the more memory it will eat.
Basic usage:
@map = GameObjectMap.new(:game_objects => TerrainObject.all, :grid => [32, 32])
@map.at(100, 100) # returns one TerrainObject at x/y: 100/100
@map.game_object(player) # returns one TerrainObject which collides with player.bounding_box
** This class is under heavy development, API will most likely change! **
Instance Attribute Summary collapse
-
#game_object_positions ⇒ Object
readonly
Returns the value of attribute game_object_positions.
-
#map ⇒ Object
readonly
Returns the value of attribute map.
Instance Method Summary collapse
-
#at(x, y) ⇒ Object
Gets a game object from the array-map on a certain X/Y.
-
#clear_at(x, y) ⇒ Object
Clear game object from the array-map on a certain X/Y.
-
#clear_game_object(game_object) ⇒ Object
Removes a specific game object from the map.
-
#create_map ⇒ Object
Creates a “tilemap” of game objects using @grid and @game_objects Useful for faster collision detection on a grid-based freeform map created with the Edit game state.
- #from_game_object(game_object) ⇒ Object
-
#initialize(options = {}) ⇒ GameObjectMap
constructor
A new instance of GameObjectMap.
Constructor Details
#initialize(options = {}) ⇒ GameObjectMap
Returns a new instance of GameObjectMap.
39 40 41 42 43 44 |
# File 'lib/chingu/game_object_map.rb', line 39 def initialize( = {}) @game_objects = [:game_objects] @grid = [:grid] || [32,32] @debug = [:debug] create_map end |
Instance Attribute Details
#game_object_positions ⇒ Object (readonly)
Returns the value of attribute game_object_positions.
37 38 39 |
# File 'lib/chingu/game_object_map.rb', line 37 def game_object_positions @game_object_positions end |
#map ⇒ Object (readonly)
Returns the value of attribute map.
37 38 39 |
# File 'lib/chingu/game_object_map.rb', line 37 def map @map end |
Instance Method Details
#at(x, y) ⇒ Object
Gets a game object from the array-map on a certain X/Y
106 107 108 109 110 |
# File 'lib/chingu/game_object_map.rb', line 106 def at(x, y) lookup_x = (x / @grid[0]).to_i lookup_y = (y / @grid[1]).to_i @map[lookup_x][lookup_y] rescue nil end |
#clear_at(x, y) ⇒ Object
Clear game object from the array-map on a certain X/Y
97 98 99 100 101 |
# File 'lib/chingu/game_object_map.rb', line 97 def clear_at(x, y) lookup_x = (x / @grid[0]).to_i lookup_y = (y / @grid[1]).to_i @map[lookup_x][lookup_y] = nil end |
#clear_game_object(game_object) ⇒ Object
Removes a specific game object from the map
84 85 86 87 88 89 90 91 92 |
# File 'lib/chingu/game_object_map.rb', line 84 def clear_game_object(game_object) range_x, range_y = @game_object_positions[game_object] range_x.each do |x| range_y.each do |y| @map[x][y] = nil end end end |
#create_map ⇒ Object
Creates a “tilemap” of game objects using @grid and @game_objects Useful for faster collision detection on a grid-based freeform map created with the Edit game state.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/chingu/game_object_map.rb', line 50 def create_map @map = [] @game_object_positions = {} @game_objects.each do |game_object| puts "#{game_object.class} @ #{game_object.x} / #{game_object.y}" if @debug start_x = (game_object.bb.left / @grid[0]).to_i stop_x = ( (game_object.bb.right-1) / @grid[0] ).to_i #if game_object.zorder == 80 # puts "x: #{game_object.x}, y: #{game_object.y}" # puts "width: #{game_object.width}, height: #{game_object.height}" # puts "start_x: #{start_x}, stop_x: #{stop_x}" #end (start_x .. stop_x).each do |x| start_y = (game_object.bb.top / @grid[1] ).to_i stop_y = ( (game_object.bb.bottom-1) / @grid[1] ).to_i @game_object_positions[game_object] = [(start_x .. stop_x), (start_y .. stop_y)] @map[x] ||= [] (start_y .. stop_y).each do |y| @map[x][y] = game_object end end end end |
#from_game_object(game_object) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/chingu/game_object_map.rb', line 112 def from_game_object(game_object) start_x = (game_object.bb.left / @grid[0]).to_i stop_x = (game_object.bb.right / @grid[0]).to_i (start_x .. stop_x).each do |x| start_y = (game_object.bb.top / @grid[1]).to_i stop_y = (game_object.bb.bottom / @grid[1]).to_i (start_y .. stop_y).each do |y| return @map[x][y] if @map[x] && @map[x][y] end end return nil end |