Просмотр исходного кода

This is the ugly thing the previous commit to LuaEnvironment was referring to

Daniele Bartolini 12 лет назад
Родитель
Сommit
b183cd3d2e
2 измененных файлов с 109 добавлено и 34 удалено
  1. 70 0
      engine/world/World.cpp
  2. 39 34
      engine/world/World.h

+ 70 - 0
engine/world/World.cpp

@@ -30,6 +30,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Device.h"
 #include "ResourceManager.h"
 #include "DebugLine.h"
+#include "Actor.h"
+#include "LuaEnvironment.h"
 
 namespace crown
 {
@@ -162,6 +164,8 @@ void World::update(float dt)
 	m_scenegraph_manager.update();
 
 	m_sound_world->update();
+
+	process_physics_events();
 }
 
 //-----------------------------------------------------------------------------
@@ -294,4 +298,70 @@ SoundWorld* World::sound_world()
 	return m_sound_world;
 }
 
+//-----------------------------------------------------------------------------
+void World::process_physics_events()
+{
+	EventStream& events = m_physics_world.events();
+
+	// Read all events
+	const char* ee = array::begin(events);
+	while (ee != array::end(events))
+	{
+		event_stream::Header h = *(event_stream::Header*) ee;
+
+		// Log::d("=== PHYSICS EVENT ===");
+		// Log::d("type = %d", h.type);
+		// Log::d("size = %d", h.size);
+
+		const char* event = ee + sizeof(event_stream::Header);
+
+		switch (h.type)
+		{
+			case physics_world::EventType::COLLISION:
+			{
+				physics_world::CollisionEvent coll_ev = *(physics_world::CollisionEvent*) event;
+
+				// Log::d("type    = %s", coll_ev.type == physics_world::CollisionEvent::BEGIN_TOUCH ? "begin" : "end");
+				// Log::d("actor_0 = (%p)", coll_ev.actors[0]);
+				// Log::d("actor_1 = (%p)", coll_ev.actors[1]);
+				// Log::d("unit_0  = (%p)", coll_ev.actors[0]->unit());
+				// Log::d("unit_1  = (%p)", coll_ev.actors[1]->unit());
+				// Log::d("where   = (%f %f %f)", coll_ev.where.x, coll_ev.where.y, coll_ev.where.z);
+				// Log::d("normal  = (%f %f %f)", coll_ev.normal.x, coll_ev.normal.y, coll_ev.normal.z);
+
+				device()->lua_environment()->call_physics_callback(
+					coll_ev.actors[0],
+					coll_ev.actors[1],
+					coll_ev.actors[0]->unit(),
+					coll_ev.actors[1]->unit(),
+					coll_ev.where,
+					coll_ev.normal,
+					(coll_ev.type == physics_world::CollisionEvent::BEGIN_TOUCH) ? "begin" : "end");
+				break;
+			}
+			case physics_world::EventType::TRIGGER:
+			{
+				physics_world::TriggerEvent trigg_ev = *(physics_world::TriggerEvent*) event;
+
+				// Log::d("type    = %s", trigg_ev.type == physics_world::TriggerEvent::BEGIN_TOUCH ? "begin" : "end");
+				// Log::d("trigger = (%p)", trigg_ev.trigger);
+				// Log::d("other   = (%p)", trigg_ev.other);
+				break;
+			}
+			default:
+			{
+				CE_FATAL("Unknown Physics event");
+				break;
+			}
+		}
+
+		// Log::d("=====================");
+
+		// Next event
+		ee += sizeof(event_stream::Header) + h.size;
+	}
+
+	array::clear(events);
+}
+
 } // namespace crown

+ 39 - 34
engine/world/World.h

@@ -61,77 +61,82 @@ struct DebugLine;
 class World
 {
 public:
-										World();
-										~World();
+	
+	World();
+	~World();
 
-	WorldId								id() const;
-	void								set_id(WorldId id);
+	WorldId id() const;
+	void set_id(WorldId id);
 
 	/// Spawns a new instance of the unit @a name at the given @a position and @a rotation.
-	UnitId								spawn_unit(const char* name, const Vector3& position = vector3::ZERO, const Quaternion& rotation = quaternion::IDENTITY);
-	UnitId								spawn_unit(const ResourceId id, UnitResource* ur, const Vector3& pos, const Quaternion& rot);
+	UnitId spawn_unit(const char* name, const Vector3& position = vector3::ZERO, const Quaternion& rotation = quaternion::IDENTITY);
+	UnitId spawn_unit(const ResourceId id, UnitResource* ur, const Vector3& pos, const Quaternion& rot);
 
 	/// Destroys the unit with the given @a id.
-	void								destroy_unit(UnitId id);
-	void								reload_units(UnitResource* old_ur, UnitResource* new_ur);
+	void destroy_unit(UnitId id);
+	void reload_units(UnitResource* old_ur, UnitResource* new_ur);
 
 	/// Returns the number of units in the world.
-	uint32_t							num_units() const;
+	uint32_t num_units() const;
 
 	/// Links the unit @a child to the @a node of the unit @a parent.
 	/// After this call, @a child will follow the @a parent unit.
-	void								link_unit(UnitId child, UnitId parent, int32_t node);
+	void link_unit(UnitId child, UnitId parent, int32_t node);
 
 	/// Unlinks the unit @a unit from its parent if it has any.
-	void								unlink_unit(UnitId unit);
+	void unlink_unit(UnitId unit);
 
-	Unit*								lookup_unit(UnitId unit);
-	Camera*								lookup_camera(CameraId camera);
+	Unit* lookup_unit(UnitId unit);
+	Camera* lookup_camera(CameraId camera);
 
 	/// Updates all units and sub-systems with the given @a dt delta time.
-	void								update(float dt);
+	void update(float dt);
 
 	/// Renders the world form the point of view of the given @a camera.
-	void								render(Camera* camera);
+	void render(Camera* camera);
 
-	CameraId							create_camera(SceneGraph& sg, int32_t node);
-	void								destroy_camera(CameraId id);
+	CameraId create_camera(SceneGraph& sg, int32_t node);
+	void destroy_camera(CameraId id);
 
 	/// Plays the sound with the given @æ name at the given @a position, with the given
 	/// @a volume and @a range. @a loop controls whether the sound must loop or not.
-	SoundInstanceId						play_sound(const char* name, bool loop = false, float volume = 1.0f, const Vector3& position = vector3::ZERO, float range = 50.0f);
+	SoundInstanceId play_sound(const char* name, bool loop = false, float volume = 1.0f, const Vector3& position = vector3::ZERO, float range = 50.0f);
 
 	/// Stops the sound with the given @a id.
-	void								stop_sound(SoundInstanceId id);
+	void stop_sound(SoundInstanceId id);
 
 	/// Links the sound @a if to the @a node of the given @æ unit.
 	/// After this call, the sound @a id will follow the unit @æ unit.
-	void								link_sound(SoundInstanceId id, Unit* unit, int32_t node);
+	void link_sound(SoundInstanceId id, Unit* unit, int32_t node);
 
 	/// Sets the @a pose of the listener.
-	void								set_listener_pose(const Matrix4x4& pose);
+	void set_listener_pose(const Matrix4x4& pose);
 
 	/// Sets the @a position of the sound @a id.
-	void								set_sound_position(SoundInstanceId id, const Vector3& position);
+	void set_sound_position(SoundInstanceId id, const Vector3& position);
 
 	/// Sets the @a range of the sound @a id.
-	void								set_sound_range(SoundInstanceId id, float range);
+	void set_sound_range(SoundInstanceId id, float range);
 
 	/// Sets the @a volume of the sound @a id.
-	void								set_sound_volume(SoundInstanceId id, float volume);
+	void set_sound_volume(SoundInstanceId id, float volume);
 
-	GuiId								create_window_gui(const char* name);
-	GuiId								create_world_gui(const Matrix4x4 pose, const uint32_t width, const uint32_t height);
-	void								destroy_gui(GuiId id);
-	Gui*								lookup_gui(GuiId id);
+	GuiId create_window_gui(const char* name);
+	GuiId create_world_gui(const Matrix4x4 pose, const uint32_t width, const uint32_t height);
+	void destroy_gui(GuiId id);
+	Gui* lookup_gui(GuiId id);
 
-	DebugLine*							create_debug_line(bool depth_test);
-	void								destroy_debug_line(DebugLine* line);
+	DebugLine* create_debug_line(bool depth_test);
+	void destroy_debug_line(DebugLine* line);
 
-	SceneGraphManager*					scene_graph_manager();
-	RenderWorld*						render_world();
-	PhysicsWorld*						physics_world();
-	SoundWorld*							sound_world();
+	SceneGraphManager* scene_graph_manager();
+	RenderWorld* render_world();
+	PhysicsWorld* physics_world();
+	SoundWorld* sound_world();
+
+private:
+
+	void process_physics_events();
 
 private: