2
0
Эх сурвалжийг харах

merge SounWorld in World and lot of other changes

mikymod 12 жил өмнө
parent
commit
267afc395d

+ 0 - 3
engine/CMakeLists.txt

@@ -76,7 +76,6 @@ set (SRC
 	World.cpp
 	Unit.cpp
 	SceneGraph.cpp
-	SoundWorld.cpp
 	Mesh.cpp
 )
 
@@ -88,7 +87,6 @@ set (HEADERS
 	World.h
 	Unit.h
 	SceneGraph.h
-	SoundWorld.h
 	Mesh.h
 )
 
@@ -340,7 +338,6 @@ set (LUA_SRC
 	lua/LuaWorld.cpp
 	lua/LuaUnit.cpp
 	lua/LuaCamera.cpp
-	lua/LuaSound.cpp
 	lua/LuaMesh.cpp
 )
 

+ 0 - 114
engine/SoundWorld.cpp

@@ -1,114 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "SoundWorld.h"
-#include "Device.h"
-#include "ResourceManager.h"
-#include "SoundRenderer.h"
-#include "SoundResource.h"
-#include "Assert.h"
-
-namespace crown
-{
-
-// //-----------------------------------------------------------------------------
-// void SoundWorld::init()
-// {
-
-// }
-
-// //-----------------------------------------------------------------------------
-// void SoundWorld::shutdown()
-// {
-
-// }
-
-//-----------------------------------------------------------------------------
-SoundInstanceId SoundWorld::play_sound(const char* name, const bool loop, const Vec3& pos)
-{
-	SoundInstanceId id = m_sound_table.create();
-
-	SoundResource* sound = (SoundResource*)device()->resource_manager()->lookup("sound", name);
-
-	m_sound[id.index].m_sound = sound->m_id;
-
-	device()->sound_renderer()->set_sound_loop(m_sound[id.index].m_sound, loop);
-
-	device()->sound_renderer()->set_sound_position(m_sound[id.index].m_sound, pos);
-
-	device()->sound_renderer()->play_sound(m_sound[id.index].m_sound);
-
-	return id;
-}
-
-//-----------------------------------------------------------------------------
-void SoundWorld::pause_sound(SoundInstanceId sound)
-{
-	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
-
-	device()->sound_renderer()->pause_sound(sound);
-}
-
-// //-----------------------------------------------------------------------------
-// void SoundWorld::link_sound(SoundInstanceId sound, UnitId unit)
-// {
-// 	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
-
-// 	// Must be implemented
-// }
-
-//-----------------------------------------------------------------------------
-void SoundWorld::set_listener(const Vec3& pos, const Vec3& vel, const Vec3& or_up, const Vec3& or_at)
-{
-	device()->sound_renderer()->set_listener(pos, vel, or_up, or_at);
-}
-
-//-----------------------------------------------------------------------------
-void SoundWorld::set_sound_position(SoundInstanceId sound, const Vec3& pos)
-{
-	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
-
-	device()->sound_renderer()->set_sound_position(sound, pos);
-}
-
-//-----------------------------------------------------------------------------
-void SoundWorld::set_sound_range(SoundInstanceId sound, const float range)
-{
-	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
-
-	device()->sound_renderer()->set_sound_max_distance(sound, range);
-}
-
-//-----------------------------------------------------------------------------
-void SoundWorld::set_sound_volume(SoundInstanceId sound, const float vol)
-{
-	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
-
-	device()->sound_renderer()->set_sound_gain(sound, vol);
-}
-
-
-} // namespace crown

+ 0 - 67
engine/SoundWorld.h

@@ -1,67 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include "IdTable.h"
-#include "SoundRenderer.h"
-#include "Vec3.h"
-#include "Mat4.h"
-
-namespace crown
-{
-
-typedef Id SoundInstanceId;
-
-struct SoundInstance
-{
-	SoundId m_sound;
-};
-
-class SoundWorld
-{
-public:
-	// void						init();
-	// void						shutdown();
-
-	SoundInstanceId				play_sound(const char* name, const bool loop = false, const Vec3& pos = Vec3::ZERO);
-	void						pause_sound(SoundInstanceId sound);
-
-	// void 						link_sound(SoundInstanceId sound, UnitId unit);
-
-	void						set_listener(const Vec3& pos, const Vec3& vel, const Vec3& or_up, const Vec3& or_at);
-
-	void						set_sound_position(SoundInstanceId sound, const Vec3& pos);
-	void						set_sound_range(SoundInstanceId sound, const float range);
-	void						set_sound_volume(SoundInstanceId sound, const float vol);
-
-private:
-
-	IdTable<MAX_SOUNDS> 		m_sound_table;
-	SoundInstance				m_sound[MAX_SOUNDS];
-};
-
-} // namespace crown

+ 85 - 0
engine/Unit.cpp

@@ -27,12 +27,21 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Unit.h"
 #include "IdTable.h"
 #include "World.h"
+#include "Allocator.h"
+#include "StringUtils.h"
 
 namespace crown
 {
 
 typedef Id CameraId;
 
+Unit::Unit()
+	: m_creator(NULL)
+	, m_resource(NULL)
+	, m_component_list(default_allocator(), 8) // test value
+{
+}
+
 //-----------------------------------------------------------------------------
 void Unit::create(World& creator, const Vec3& pos, const Quat& rot)
 {
@@ -127,4 +136,80 @@ Camera* Unit::camera(const char* /*name*/)
 	return m_camera;
 }
 
+//-----------------------------------------------------------------------------
+bool Unit::next_free_component(uint32_t& index)
+{
+	uint32_t i;
+	for (i = 0; i < m_component_list.size(); i++)
+	{
+		// id == 0 means free slot
+		if (m_component_list[i].id == 0)
+		{
+			index = i;
+			return true;
+		}
+	}
+
+	return false;
+}
+
+//-----------------------------------------------------------------------------
+void Unit::add_component(const char* name, uint32_t type, ComponentId component)
+{
+	CE_ASSERT_NOT_NULL(name);
+
+	uint32_t key = hash::murmur2_32(name, string::strlen(name), 0);
+
+	uint32_t free_index;
+
+	if (next_free_component(free_index))
+	{
+		m_component_list[free_index].id.key = key;
+		m_component_list[free_index].type = (ComponentType::Enum)type;
+		m_component_list[free_index].component = component;
+	}
+
+	Component comp;
+
+	comp.id.key = key;
+	comp.type = (ComponentType::Enum)type;
+	comp.component = component;
+
+	m_component_list.push_back(comp);
+}
+
+//-----------------------------------------------------------------------------
+void Unit::remove_component(const char* name)
+{
+	uint32_t hashed_name = hash::murmur2_32(name, string::strlen(name), 0);
+
+	for (uint32_t i = 0; i < m_component_list.size(); i++)
+	{
+		if (m_component_list[i].id == hashed_name)
+		{
+			m_component_list[i].id = 0;
+			return;
+		}
+	}
+
+	CE_FATAL("Component not found!");
+}
+
+//-----------------------------------------------------------------------------
+Component* Unit::get_component(const char* name)
+{
+	uint32_t hashed_name = hash::murmur2_32(name, string::strlen(name), 0);
+
+	for (uint32_t i = 0; i < m_component_list.size(); i++)
+	{
+		if (m_component_list[i].id == hashed_name)
+		{
+			return &m_component_list[i];
+		}
+	}
+
+	CE_FATAL("Component not found!");
+}
+
+
 } // namespace crown

+ 36 - 0
engine/Unit.h

@@ -29,11 +29,36 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Vec3.h"
 #include "Quat.h"
 #include "Mat4.h"
+#include "Hash.h"
+#include "IdTable.h"
 #include "SceneGraph.h"
 
+
 namespace crown
 {
 
+typedef	Id ComponentId;
+
+//-----------------------------------------------------------------------------
+struct ComponentType
+{
+	enum Enum
+	{
+		UNKNOWN,
+		RENDER,
+		SOUND
+	};
+};
+
+//-----------------------------------------------------------------------------
+struct Component
+{
+	Id32				id;
+	ComponentType::Enum type;
+	ComponentId			component;
+};
+
+//-----------------------------------------------------------------------------
 struct UnitResource
 {
 };
@@ -43,6 +68,7 @@ class World;
 
 struct Unit
 {
+					Unit();
 	void			create(World& creator, const Vec3& pos, const Quat& rot);
 	void			destroy();
 
@@ -62,8 +88,16 @@ struct Unit
 	void			set_local_rotation(const Quat& rot, int32_t node = 0);
 	void			set_local_pose(const Mat4& pose, int32_t node = 0);
 
+	void			add_component(const char* name, uint32_t type, ComponentId component);
+	void			remove_component(const char* name);
+	Component*		get_component(const char* name);
+
 	Camera*			camera(const char* name);
 
+private:
+
+	bool			next_free_component(uint32_t& index);
+
 public:
 
 	World*			m_creator;
@@ -73,6 +107,8 @@ public:
 	Camera*			m_camera;
 
 	UnitResource*	m_resource;
+
+	List<Component> m_component_list;
 };
 
 } // namespace crown

+ 70 - 7
engine/World.cpp

@@ -27,6 +27,10 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Assert.h"
 #include "World.h"
 #include "Allocator.h"
+#include "Device.h"
+#include "ResourceManager.h"
+#include "SoundRenderer.h"
+#include "SoundResource.h"
 
 namespace crown
 {
@@ -46,7 +50,6 @@ void World::init()
 //-----------------------------------------------------------------------------
 void World::shutdown()
 {
-
 }
 
 //-----------------------------------------------------------------------------
@@ -115,12 +118,6 @@ void World::update(Camera& camera, float dt)
 	(void)dt;
 }
 
-//-----------------------------------------------------------------------------
-SoundWorld&	World::sound_world()
-{
-	return m_sound_world;
-}
-
 //-----------------------------------------------------------------------------
 CameraId World::create_camera(Unit& parent, int32_t node)
 {
@@ -136,5 +133,71 @@ void World::destroy_camera(CameraId camera)
 	m_camera_table.destroy(camera);
 }
 
+//-----------------------------------------------------------------------------
+SoundInstanceId World::play_sound(const char* name, const bool loop, const float volume, const Vec3& pos, const float range)
+{
+	SoundInstanceId id = m_sound_table.create();
+
+	SoundResource* sound = (SoundResource*)device()->resource_manager()->lookup(SOUND_EXTENSION, name);
+
+	m_sound[id.index].m_sound = sound->m_id;
+
+	device()->sound_renderer()->set_sound_loop(m_sound[id.index].m_sound, loop);
+	device()->sound_renderer()->set_sound_gain(m_sound[id.index].m_sound, volume);
+	device()->sound_renderer()->set_sound_position(m_sound[id.index].m_sound, pos);
+	device()->sound_renderer()->set_sound_max_distance(m_sound[id.index].m_sound, range);
+
+	device()->sound_renderer()->play_sound(m_sound[id.index].m_sound);
+
+	return id;
+}
+
+//-----------------------------------------------------------------------------
+void World::pause_sound(SoundInstanceId sound)
+{
+	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
+
+	device()->sound_renderer()->pause_sound(m_sound[sound.index].m_sound);
+}
+
+//-----------------------------------------------------------------------------
+void World::link_sound(SoundInstanceId sound, UnitId unit)
+{
+	CE_ASSERT(m_unit_table.has(unit), "Unit does not exists");
+	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
+
+	Vec3 pos = m_units[unit.index].world_position();
+	device()->sound_renderer()->set_sound_position(m_sound[sound.index].m_sound, pos);
+}
+
+//-----------------------------------------------------------------------------
+void World::set_listener(const Vec3& pos, const Vec3& vel, const Vec3& or_up, const Vec3& or_at)
+{
+	device()->sound_renderer()->set_listener(pos, vel, or_up, or_at);
+}
+
+//-----------------------------------------------------------------------------
+void World::set_sound_position(SoundInstanceId sound, const Vec3& pos)
+{
+	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
+
+	device()->sound_renderer()->set_sound_position(m_sound[sound.index].m_sound, pos);
+}
+
+//-----------------------------------------------------------------------------
+void World::set_sound_range(SoundInstanceId sound, const float range)
+{
+	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
+
+	device()->sound_renderer()->set_sound_max_distance(m_sound[sound.index].m_sound, range);
+}
+
+//-----------------------------------------------------------------------------
+void World::set_sound_volume(SoundInstanceId sound, const float vol)
+{
+	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
+
+	device()->sound_renderer()->set_sound_gain(m_sound[sound.index].m_sound, vol);
+}
 
 } // namespace crown

+ 33 - 18
engine/World.h

@@ -26,22 +26,29 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
-#include "SceneGraph.h"
 #include "HeapAllocator.h"
 #include "IdTable.h"
+#include "LinearAllocator.h"
 #include "Unit.h"
 #include "Camera.h"
-#include "SoundWorld.h"
-#include "LinearAllocator.h"
+#include "SceneGraph.h"
+#include "SoundRenderer.h"
 
 namespace crown
 {
 
 #define MAX_UNITS 65000
+#define	MAX_SOUNDS 64
 #define MAX_CAMERAS 16
 
 typedef Id UnitId;
 typedef Id CameraId;
+typedef Id SoundInstanceId;
+
+struct SoundInstance
+{
+	SoundId m_sound;
+};
 
 class Vec3;
 class Quat;
@@ -49,27 +56,34 @@ class Quat;
 class World
 {
 public:
-						World();
-
-	void				init();
-	void				shutdown();
+							World();
 
-	UnitId				spawn_unit(const char* name, const Vec3& pos = Vec3::ZERO, const Quat& rot = Quat(Vec3(0, 1, 0), 0.0f));
-	void				kill_unit(UnitId unit);
+	void					init();
+	void					shutdown();
 
-	void				link_unit(UnitId child, UnitId parent);
-	void				unlink_unit(UnitId child, UnitId parent);
+	UnitId					spawn_unit(const char* name, const Vec3& pos = Vec3::ZERO, const Quat& rot = Quat(Vec3(0, 1, 0), 0.0f));
+	void					kill_unit(UnitId unit);
 
-	Unit*				lookup_unit(UnitId unit);
-	Camera*				lookup_camera(CameraId camera);
+	void					link_unit(UnitId child, UnitId parent);
+	void					unlink_unit(UnitId child, UnitId parent);
 
-	void				update(Camera& camera, float dt);
+	void					update(Camera& camera, float dt);
+	Unit*					lookup_unit(UnitId unit);
+	Camera*					lookup_camera(CameraId camera);
 
-	SoundWorld&			sound_world();
+	void					update(float dt);
 
-	CameraId			create_camera(Unit& parent, int32_t node);
-	void				destroy_camera(CameraId camera);
+	CameraId				create_camera(Unit& parent, int32_t node);
+	void					destroy_camera(CameraId camera);
 
+	SoundInstanceId			play_sound(const char* name, const bool loop = false, const float volume = 1.0f, const Vec3& pos = Vec3::ZERO, const float range = 50.0f);
+	void					pause_sound(SoundInstanceId sound);
+	void 					link_sound(SoundInstanceId sound, UnitId unit);
+	void					set_listener(const Vec3& pos, const Vec3& vel, const Vec3& or_up, const Vec3& or_at);
+	void					set_sound_position(SoundInstanceId sound, const Vec3& pos);
+	void					set_sound_range(SoundInstanceId sound, const float range);
+	void					set_sound_volume(SoundInstanceId sound, const float vol);
+	
 private:
 
 	LinearAllocator			m_allocator;
@@ -82,7 +96,8 @@ private:
 	IdTable<MAX_CAMERAS>	m_camera_table;
 	Camera					m_camera[MAX_CAMERAS];
 
-	SoundWorld				m_sound_world;
+	IdTable<MAX_SOUNDS> 	m_sound_table;
+	SoundInstance			m_sound[MAX_SOUNDS];
 };
 
 } // namespace crown

+ 0 - 1
engine/lua/LuaEnvironment.cpp

@@ -61,7 +61,6 @@ CE_EXPORT int luaopen_libcrown(lua_State* /*L*/)
 	load_unit(*env);
 	load_camera(*env);
 	load_world(*env);
-	load_sound(*env);
 	load_mesh(*env);
 
 	return 1;

+ 0 - 1
engine/lua/LuaEnvironment.h

@@ -111,7 +111,6 @@ void load_resource_package(LuaEnvironment& env);
 void load_unit(LuaEnvironment& env);
 void load_camera(LuaEnvironment& env);
 void load_world(LuaEnvironment& env);
-void load_sound(LuaEnvironment& env);
 void load_mesh(LuaEnvironment& env);
 
 CE_EXPORT int32_t luaopen_libcrown(lua_State* L);

+ 0 - 128
engine/lua/LuaSound.cpp

@@ -1,128 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-#include "LuaStack.h"
-#include "LuaEnvironment.h"
-#include "Device.h"
-#include "World.h"
-#include "SoundWorld.h"
-
-namespace crown
-{
-
-//-----------------------------------------------------------------------------
-CE_EXPORT int sound_world_play_sound(lua_State* L)
-{
-	LuaStack stack(L);
-
-	World* world = (World*) stack.get_lightdata(1);
-	const char* name = stack.get_string(2);
-
-	const bool loop = stack.num_args() > 2 ? stack.get_bool(3) : false;
-	const Vec3& pos = stack.num_args() > 3 ? stack.get_vec3(4) : Vec3::ZERO;
-
-	SoundInstanceId id = world->sound_world().play_sound(name, loop, pos);
-
-	stack.push_int32(id.encode());
-
-	return 1;
-}
-
-//-----------------------------------------------------------------------------
-CE_EXPORT int sound_world_pause_sound(lua_State* L)
-{
-	LuaStack stack(L);
-
-	World* world = (World*) stack.get_lightdata(1);
-
-	SoundInstanceId id;
-	id.decode(stack.get_int(2));
-
-	world->sound_world().pause_sound(id);
-
-	return 0;
-}
-
-// //-----------------------------------------------------------------------------
-// CE_EXPORT int sound_world_link_sound(lua_State* L)
-// {
-
-// }
-
-// //-----------------------------------------------------------------------------
-// CE_EXPORT int sound_world_set_listener(lua_State* L)
-// {
-
-// }
-
-//-----------------------------------------------------------------------------
-CE_EXPORT int sound_world_set_sound_position(lua_State* /*L*/)
-{
-	// LuaStack stack
-	return 0;
-}
-
-//-----------------------------------------------------------------------------
-CE_EXPORT int sound_world_set_sound_range(lua_State* L)
-{
-	LuaStack stack(L);
-
-	World* world = (World*) stack.get_lightdata(1);
-
-	SoundInstanceId id;
-	id.decode(stack.get_int(2));
-	float range = stack.get_float(3);
-
-	world->sound_world().set_sound_range(id, range);
-
-	return 0;
-}
-
-//-----------------------------------------------------------------------------
-CE_EXPORT int sound_world_set_sound_volume(lua_State* L)
-{
-	LuaStack stack(L);
-
-	World* world = (World*) stack.get_lightdata(1);
-
-	SoundInstanceId id;
-	id.decode(stack.get_int(2));
-	float vol = stack.get_float(2);
-
-	world->sound_world().set_sound_volume(id, vol);
-
-	return 0;
-}
-
-//-----------------------------------------------------------------------------
-void load_sound(LuaEnvironment& env)
-{
-	env.load_module_function("Sound", "play_sound",	sound_world_play_sound);
-	env.load_module_function("Sound", "pause_sound", sound_world_pause_sound);
-	env.load_module_function("Sound", "set_sound_range", sound_world_set_sound_range);
-	env.load_module_function("Sound", "set_sound_volume", sound_world_set_sound_volume);
-}
-
-} // namespace crown

+ 27 - 0
engine/lua/LuaUnit.cpp

@@ -133,6 +133,33 @@ CE_EXPORT int32_t unit_set_local_pose(lua_State* L)
 	return 0;
 }
 
+//-----------------------------------------------------------------------------
+CE_EXPORT void unit_add_component(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Unit* unit = stack.get_unit(1);
+	const char* name = stack.get_string(2);
+	uint32_t type = stack.get_int(3);
+
+	ComponentId component;
+	component.decode(stack.get_int(4));
+
+	unit->add_component(name, type, component);
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT void unit_remove_component(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Unit* unit = stack.get_unit(1);
+	const char* name = stack.get_string(2);
+
+	unit->remove_component(name);
+}
+
+//-----------------------------------------------------------------------------
 CE_EXPORT int unit_camera(lua_State* L)
 {
 	LuaStack stack(L);

+ 128 - 0
engine/lua/LuaWorld.cpp

@@ -49,10 +49,138 @@ CE_EXPORT int world_spawn_unit(lua_State* L)
 	return 1;
 }
 
+//-----------------------------------------------------------------------------
+CE_EXPORT int world_play_sound(lua_State* L)
+{
+	LuaStack stack(L);
+
+	World* world = (World*) stack.get_lightdata(1);
+	const char* name = stack.get_string(2);
+
+	const bool loop = stack.num_args() > 2 ? stack.get_bool(3) : false;
+	const float volume = stack.num_args() > 3 ? stack.get_float(4) : 1.0f; // test value
+	const Vec3& pos = stack.num_args() > 4 ? stack.get_vec3(5) : Vec3::ZERO;
+	const float range = stack.num_args() > 5 ? stack.get_float(6) : 1000.0f; // test value
+
+	SoundInstanceId id = world->play_sound(name, loop, volume, pos, range);
+
+	stack.push_int32(id.encode());
+
+	return 1;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int world_pause_sound(lua_State* L)
+{
+	LuaStack stack(L);
+
+	World* world = (World*) stack.get_lightdata(1);
+
+	SoundInstanceId id;
+	id.decode(stack.get_int(2));
+
+	world->pause_sound(id);
+
+	return 0;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int world_link_sound(lua_State* L)
+{
+	LuaStack stack(L);
+
+	World* world = (World*) stack.get_lightdata(1);
+
+	SoundInstanceId sound;
+	sound.decode(stack.get_int(2));
+
+	UnitId unit;
+	unit.decode(stack.get_int(3));
+
+	world->link_sound(sound, unit);
+
+	return 0;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int world_set_listener(lua_State* L)
+{
+	LuaStack stack(L);
+
+	World* world = (World*) stack.get_lightdata(1);
+
+	const Vec3& pos = stack.get_vec3(2);
+	const Vec3& vel = stack.get_vec3(3);
+	const Vec3& or_up = stack.get_vec3(4);
+	const Vec3& or_at = stack.get_vec3(5);
+
+	world->set_listener(pos, vel, or_up, or_at);
+
+	return 0;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int world_set_sound_position(lua_State* L)
+{
+	LuaStack stack(L);
+
+	World* world = (World*) stack.get_lightdata(1);
+
+	SoundInstanceId id;
+	id.decode(stack.get_int(2));
+
+	const Vec3& pos = stack.get_vec3(3);
+
+	world->set_sound_position(id, pos);
+
+	return 0;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int world_set_sound_range(lua_State* L)
+{
+	LuaStack stack(L);
+
+	World* world = (World*) stack.get_lightdata(1);
+
+	SoundInstanceId id;
+	id.decode(stack.get_int(2));
+
+	float range = stack.get_float(3);
+
+	world->set_sound_range(id, range);
+
+	return 0;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int world_set_sound_volume(lua_State* L)
+{
+	LuaStack stack(L);
+
+	World* world = (World*) stack.get_lightdata(1);
+
+	SoundInstanceId id;
+	id.decode(stack.get_int(2));
+	float vol = stack.get_float(3);
+
+	world->set_sound_volume(id, vol);
+
+	return 0;
+}
+
 //-----------------------------------------------------------------------------
 void load_world(LuaEnvironment& env)
 {
 	env.load_module_function("World", "spawn_unit",			world_spawn_unit);
+
+	env.load_module_function("World", "play_sound",			world_play_sound);
+	env.load_module_function("World", "pause_sound", 		world_pause_sound);
+	env.load_module_function("World", "link_sound",			world_link_sound);
+	env.load_module_function("World", "set_listener", 		world_set_listener);
+	env.load_module_function("World", "set_sound_position", world_set_sound_position);
+	env.load_module_function("World", "set_sound_range", 	world_set_sound_range);
+	env.load_module_function("World", "set_sound_volume", 	world_set_sound_volume);
 }
 
 } // namespace crown