Removed entity management from Game, refactored Map to MapLoader and added Map to manage world

This commit is contained in:
2019-09-27 14:30:23 -05:00
parent 1bfc6e6929
commit b091a489af
20 changed files with 260 additions and 239 deletions

View File

@@ -1,8 +1,8 @@
class IMICFPS
class CollisionManager
attr_reader :game_state, :collisions
def initialize(game_state:)
@game_state = game_state
attr_reader :map, :collisions
def initialize(map:)
@map = map
@collisions = {}
@aabb_tree = AABBTree.new
@@ -14,7 +14,7 @@ class IMICFPS
end
def update
@game_state.entities.each do |entity|
@map.entities.each do |entity|
next unless entity.is_a?(Entity)
next unless node = @aabb_tree.objects[entity]
@@ -42,7 +42,7 @@ class IMICFPS
@collisions.clear
broadphase = {}
@game_state.entities.each do |entity|
@map.entities.each do |entity|
next unless entity.collidable?
next if entity.collision == :static # Only dynamic entities can be resolved

View File

@@ -2,12 +2,12 @@ class IMICFPS
module EntityManager # Get included into GameState context
def add_entity(entity)
@collision_manager.add(entity)# Add every entity to collision manager
@publisher.publish(:create, self, entity)
Publisher.instance.publish(:create, nil, entity)
@entities << entity
end
def insert_entity(package, name, position, orientation, data = {})
ent = Map::Entity.new(package, name, position, orientation, Vector.new(1,1,1))
ent = MapLoader::Entity.new(package, name, position, orientation, Vector.new(1,1,1))
add_entity(IMICFPS::Entity.new(map_entity: ent, manifest: Manifest.new(package: package, name: name)))
end
@@ -15,11 +15,15 @@ class IMICFPS
@entities.detect {|entity| entity == entity}
end
def find_entity_by(name:)
@entities.detect { |entity| entity.name == name}
end
def remove_entity(entity)
ent = @entities.detect {|entity| entity == entity}
if ent
@collision_manager.remove(entity)
@publisher.publish(:destroy, self, entity)
@publisher.publish(:destroy, nil, entity)
@entities.delete(ent)
end
end

View File

@@ -20,5 +20,11 @@ class IMICFPS
def clear_lights
@lights.clear
end
def available_light
raise "Using to many lights, #{light_count}/#{LightManager::MAX_LIGHTS}" if light_count > LightManager::MAX_LIGHTS
puts "OpenGL::GL_LIGHT#{light_count}" if $debug.get(:stats)
Object.const_get "OpenGL::GL_LIGHT#{light_count}"
end
end
end

View File

@@ -19,7 +19,7 @@ class IMICFPS
end
def simulate
@collision_manager.game_state.entities.each do |entity|
@collision_manager.map.entities.each do |entity|
entity.position.x += entity.velocity.x * entity.delta_time
entity.position.y += entity.velocity.y * entity.delta_time
entity.position.z += entity.velocity.z * entity.delta_time