Browse Source

Generate events on unit spawned/destroyed and level loaded

Daniele Bartolini 11 years ago
parent
commit
968c1e268a
3 changed files with 42 additions and 14 deletions
  1. 27 8
      engine/world/world.cpp
  2. 3 0
      engine/world/world.h
  3. 12 6
      engine/world/world_types.h

+ 27 - 8
engine/world/world.cpp

@@ -94,10 +94,7 @@ UnitId World::spawn_unit(const ResourceId id, UnitResource* ur, const Vector3& p
 	const UnitId unit_id = id_array::create(m_units, u);
 	new (u) Unit(*this, unit_id, id, ur, Matrix4x4(rot, pos));
 
-	// SpawnUnitEvent ev;
-	// ev.unit = unit_id;
-	// event_stream::write(m_events, EventType::SPAWN, ev);
-
+	post_unit_spawned_event(unit_id);
 	return unit_id;
 }
 
@@ -106,10 +103,7 @@ void World::destroy_unit(UnitId id)
 {
 	CE_DELETE(m_unit_pool, id_array::get(m_units, id));
 	id_array::destroy(m_units, id);
-
-	// DestroyUnitEvent ev;
-	// ev.unit = id;
-	// event_stream::write(m_events, EventType::DESTROY, ev);
+	post_unit_destroyed_event(id);
 }
 
 //-----------------------------------------------------------------------------
@@ -313,6 +307,8 @@ void World::load_level(const char* name)
 		const LevelSound* ls = res->get_sound(i);
 		play_sound(ls->name, ls->loop, ls->volume, ls->position, ls->range);
 	}
+
+	post_level_loaded_event();
 }
 
 //-----------------------------------------------------------------------------
@@ -344,6 +340,29 @@ SoundWorld* World::sound_world()
 	return m_sound_world;
 }
 
+//-----------------------------------------------------------------------------
+void World::post_unit_spawned_event(UnitId id)
+{
+	UnitSpawnedEvent ev;
+	ev.unit = id;
+	event_stream::write(m_events, EventType::UNIT_SPAWNED, ev);
+}
+
+//-----------------------------------------------------------------------------
+void World::post_unit_destroyed_event(UnitId id)
+{
+	UnitDestroyedEvent ev;
+	ev.unit = id;
+	event_stream::write(m_events, EventType::UNIT_DESTROYED, ev);
+}
+
+//-----------------------------------------------------------------------------
+void World::post_level_loaded_event()
+{
+	LevelLoadedEvent ev;
+	event_stream::write(m_events, EventType::LEVEL_LOADED, ev);
+}
+
 //-----------------------------------------------------------------------------
 void World::process_physics_events()
 {

+ 3 - 0
engine/world/world.h

@@ -159,6 +159,9 @@ public:
 
 private:
 
+	void post_unit_spawned_event(UnitId id);
+	void post_unit_destroyed_event(UnitId id);
+	void post_level_loaded_event();
 	void process_physics_events();
 
 private:

+ 12 - 6
engine/world/world_types.h

@@ -39,21 +39,27 @@ struct EventType
 {
 	enum Enum
 	{
-		SPAWN,
-		DESTROY
+		UNIT_SPAWNED,
+		UNIT_DESTROYED,
+
+		LEVEL_LOADED
 	};
 };
 
-struct SpawnUnitEvent
+struct UnitSpawnedEvent
 {
-	/// The unit being spawned
+	/// The unit spawned
 	UnitId unit;
 };
 
-struct DestroyUnitEvent
+struct UnitDestroyedEvent
 {
-	/// The unit being unspawned
+	/// The unit destroyed
 	UnitId unit;
 };
 
+struct LevelLoadedEvent
+{
+};
+
 } // namespace crown