Skip to content

Commit

Permalink
Merge pull request Revolutionary-Games#21 from Revolutionary-Games/li…
Browse files Browse the repository at this point in the history
…ghts

Scriptable Lights
  • Loading branch information
Daniferrito committed May 16, 2013
2 parents e446c8f + 29f139c commit 2aff61d
Show file tree
Hide file tree
Showing 17 changed files with 557 additions and 66 deletions.
2 changes: 1 addition & 1 deletion res/dist/scripts/manifest.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test.lua
sandbox
17 changes: 17 additions & 0 deletions res/dist/scripts/sandbox/background.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
local background = Entity("background")

-- Set up skyplane
local skyplane = SkyPlaneComponent()
skyplane.workingCopy.plane.normal = Vector3(0, 0, 1)
skyplane.workingCopy.plane.d = 1000
skyplane:touch()
background:addComponent(skyplane)

local onupdate = OnUpdateComponent()
background:addComponent(onupdate)
onupdate.callback = function(entityId, milliseconds)
skyplane.workingCopy.plane.d = (skyplane.workingCopy.plane.d + milliseconds) % 10000
skyplane:touch()
end


30 changes: 30 additions & 0 deletions res/dist/scripts/sandbox/light.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
local light = Entity("light")

-- Transform
light.transform = TransformComponent()
light.transform.workingCopy.scale = Vector3(0.01, 0.01, 0.01)
light.transform.workingCopy.position.z = 2.0;
light.transform:touch()
light:addComponent(light.transform)


lightSceneNode = OgreSceneNodeComponent()
light:addComponent(lightSceneNode)
lightComponent = OgreLightComponent()
lightComponent.workingCopy:setRange(200)
lightComponent:touch()
light:addComponent(lightComponent)
lightEntity = OgreEntityComponent(OgreEntityComponent.PT_SPHERE)
light:addComponent(lightEntity)
onupdate = OnUpdateComponent()
light:addComponent(onupdate)
time = 0
onupdate.callback = function(entityId, milliseconds)
time = time + milliseconds / 1000
light.transform.workingCopy.position.x = 5 * math.sin(time)
light.transform.workingCopy.position.y = 5 * math.cos(time)
light.transform:touch()
end



3 changes: 3 additions & 0 deletions res/dist/scripts/sandbox/manifest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
background.lua
light.lua
player.lua
43 changes: 43 additions & 0 deletions res/dist/scripts/sandbox/player.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
local player = Entity("player")
playerTransform = TransformComponent()
player:addComponent(playerTransform)
playerSceneNode = OgreSceneNodeComponent()
player:addComponent(playerSceneNode)
player:addComponent(OgreEntityComponent("Sinbad.mesh"))

playerTransform.workingCopy.position = Vector3(0, 0, 0)
playerTransform:touch()

playerMovable = MovableComponent()
player:addComponent(playerMovable)

playerInput = OnKeyComponent()
player:addComponent(playerInput)
MOVEMENT_SPEED = 10
playerInput.onPressed = function (entityId, event)
if event.key == KeyEvent.KC_W then
playerMovable.velocity.y = playerMovable.velocity.y + MOVEMENT_SPEED
elseif event.key == KeyEvent.KC_S then
playerMovable.velocity.y = playerMovable.velocity.y - MOVEMENT_SPEED
elseif event.key == KeyEvent.KC_A then
playerMovable.velocity.x = playerMovable.velocity.x - MOVEMENT_SPEED
elseif event.key == KeyEvent.KC_D then
playerMovable.velocity.x = playerMovable.velocity.x + MOVEMENT_SPEED
end
end

playerInput.onReleased = function (entityId, event)
if event.key == KeyEvent.KC_W then
playerMovable.velocity.y = playerMovable.velocity.y - MOVEMENT_SPEED
elseif event.key == KeyEvent.KC_S then
playerMovable.velocity.y = playerMovable.velocity.y + MOVEMENT_SPEED
elseif event.key == KeyEvent.KC_A then
playerMovable.velocity.x = playerMovable.velocity.x + MOVEMENT_SPEED
elseif event.key == KeyEvent.KC_D then
playerMovable.velocity.x = playerMovable.velocity.x - MOVEMENT_SPEED
end
end




56 changes: 0 additions & 56 deletions res/dist/scripts/test.lua
Original file line number Diff line number Diff line change
@@ -1,56 +0,0 @@
background = Entity("background")
skyplane = SkyPlaneComponent()
skyplane.workingCopy.plane.normal = Vector3(0, 0, 1)
skyplane.workingCopy.plane.d = 1000
skyplane:touch()
background:addComponent(skyplane)

onupdate = OnUpdateComponent()
background:addComponent(onupdate)
onupdate.callback = function(entityId, milliseconds)
skyplane.workingCopy.plane.d = (skyplane.workingCopy.plane.d + milliseconds) % 10000
skyplane:touch()
end

player = Entity("player")
playerTransform = TransformComponent()
player:addComponent(playerTransform)
playerSceneNode = OgreSceneNodeComponent()
player:addComponent(playerSceneNode)
player:addComponent(OgreEntityComponent("Sinbad.mesh"))

playerTransform.position = Vector3(0, 0, 0)
playerTransform:touch()

playerMovable = MovableComponent()
player:addComponent(playerMovable)

playerInput = OnKeyComponent()
player:addComponent(playerInput)
MOVEMENT_SPEED = 10
playerInput.onPressed = function (entityId, event)
if event.key == KeyEvent.KC_W then
playerMovable.velocity.y = playerMovable.velocity.y + MOVEMENT_SPEED
elseif event.key == KeyEvent.KC_S then
playerMovable.velocity.y = playerMovable.velocity.y - MOVEMENT_SPEED
elseif event.key == KeyEvent.KC_A then
playerMovable.velocity.x = playerMovable.velocity.x - MOVEMENT_SPEED
elseif event.key == KeyEvent.KC_D then
playerMovable.velocity.x = playerMovable.velocity.x + MOVEMENT_SPEED
end
end

playerInput.onReleased = function (entityId, event)
if event.key == KeyEvent.KC_W then
playerMovable.velocity.y = playerMovable.velocity.y - MOVEMENT_SPEED
elseif event.key == KeyEvent.KC_S then
playerMovable.velocity.y = playerMovable.velocity.y + MOVEMENT_SPEED
elseif event.key == KeyEvent.KC_A then
playerMovable.velocity.x = playerMovable.velocity.x + MOVEMENT_SPEED
elseif event.key == KeyEvent.KC_D then
playerMovable.velocity.x = playerMovable.velocity.x - MOVEMENT_SPEED
end
end



2 changes: 1 addition & 1 deletion src/common/movement.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MovableComponent : public Component {
/**
* @brief The entity's velocity in units per second
*/
Ogre::Vector3 m_velocity;
Ogre::Vector3 m_velocity = Ogre::Vector3::ZERO;

};

Expand Down
2 changes: 2 additions & 0 deletions src/ogre/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ add_sources(
${CMAKE_CURRENT_SOURCE_DIR}/entity_system.h
${CMAKE_CURRENT_SOURCE_DIR}/keyboard_system.cpp
${CMAKE_CURRENT_SOURCE_DIR}/keyboard_system.h
${CMAKE_CURRENT_SOURCE_DIR}/light_system.cpp
${CMAKE_CURRENT_SOURCE_DIR}/light_system.h
${CMAKE_CURRENT_SOURCE_DIR}/ogre_engine.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ogre_engine.h
${CMAKE_CURRENT_SOURCE_DIR}/on_key.cpp
Expand Down
34 changes: 29 additions & 5 deletions src/ogre/entity_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ using namespace thrive;

OgreEntityComponent::OgreEntityComponent(
std::string meshName
) : m_meshName(meshName)
) : m_meshName(meshName),
m_prefabType(Ogre::SceneManager::PT_SPHERE)
{
}


OgreEntityComponent::OgreEntityComponent(
Ogre::SceneManager::PrefabType prefabType
) : m_meshName(""),
m_prefabType(prefabType)
{
}

Expand All @@ -28,8 +37,15 @@ OgreEntityComponent::luaBindings() {
def("TYPE_NAME", &OgreEntityComponent::TYPE_NAME),
def("TYPE_ID", &OgreEntityComponent::TYPE_ID)
]
.enum_("PrefabType") [
value("PT_PLANE", Ogre::SceneManager::PT_PLANE),
value("PT_CUBE", Ogre::SceneManager::PT_CUBE),
value("PT_SPHERE", Ogre::SceneManager::PT_SPHERE)
]
.def(constructor<std::string>())
.def(constructor<Ogre::SceneManager::PrefabType>())
.def_readonly("meshName", &OgreEntityComponent::m_meshName)
.def_readonly("prefabType", &OgreEntityComponent::m_prefabType)
;
}

Expand Down Expand Up @@ -93,19 +109,27 @@ OgreEntitySystem::update(int) {
EntityId entityId = entry.first;
OgreSceneNodeComponent* sceneNodeComponent = std::get<0>(entry.second);
OgreEntityComponent* ogreEntityComponent = std::get<1>(entry.second);
Ogre::Entity* ogreEntity = m_impl->m_sceneManager->createEntity(
ogreEntityComponent->m_meshName
);
Ogre::Entity* ogreEntity = nullptr;
if (not ogreEntityComponent->m_meshName.empty()) {
ogreEntity = m_impl->m_sceneManager->createEntity(
ogreEntityComponent->m_meshName
);
}
else {
ogreEntity = m_impl->m_sceneManager->createEntity(
ogreEntityComponent->m_prefabType
);
}
sceneNodeComponent->m_sceneNode->attachObject(ogreEntity);
m_impl->m_ogreEntities.emplace(entityId, ogreEntity);
}
m_impl->m_entities.clearChanges();
for (EntityId entityId : m_impl->m_entities.removedEntities()) {
Ogre::Entity* ogreEntity = m_impl->m_ogreEntities.at(entityId);
ogreEntity->detachFromParent();
m_impl->m_sceneManager->destroyEntity(ogreEntity);
m_impl->m_ogreEntities.erase(entityId);
}
m_impl->m_entities.clearChanges();
}


30 changes: 30 additions & 0 deletions src/ogre/entity_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "engine/system.h"

#include <memory>
#include <OgreSceneManager.h>
#include <OgreString.h>


Expand All @@ -18,10 +19,26 @@ class OgreEntityComponent : public Component {

public:

/**
* @brief Constructor
*
* @param meshName
* The name of the mesh the entity is to be based on
*/
OgreEntityComponent(
std::string meshName
);

/**
* @brief Constructor
*
* @param prefabType
* The prefab model to use (\c PT_PLANE, \c PT_CUBE or \c PT_SPHERE)
*/
OgreEntityComponent(
Ogre::SceneManager::PrefabType prefabType
);

/**
* @brief Lua bindings
*
Expand All @@ -33,8 +50,21 @@ class OgreEntityComponent : public Component {
static luabind::scope
luaBindings();

/**
* @brief The name of the entity's mesh
*
* If the entity was not constructed with a named mesh, this string will
* be empty.
*/
const std::string m_meshName;

/**
* @brief The prefab mesh this entity uses, if any
*
* Defaults to Ogre::Entity::PT_SPHERE.
*/
const Ogre::SceneManager::PrefabType m_prefabType;

};


Expand Down
Loading

0 comments on commit 2aff61d

Please sign in to comment.