Sly Engine
Advanced Demo

This is an advanced demo. More...

Collaboration diagram for Advanced Demo:

This is an advanced demo.

Similar to the basic demo, this details the basic scene header as well as a game object setup with all of the 'able' functions. Here is the beginning scene:

//TankScene.h
class Tank; // Forward declaration
class TankScene : public Scene {
public:
// We do not create objects at construction
TankScene() = {};
~TankScene() = {};
// Create objects within initialization
void Initialize() override;
void SceneEnd() override;
private:
// Our object
Tank* tank;
// A pointer to a plane
TerrainObject* terrain;
}

As well as the implementation file:

TankScene::Initialize() {
pFrigate = new Frigate();
pFrigate.setPosition(Vect(40, 10, 40)); // This can also be done at construction
// Assuming a terrain has been properly loaded in the asset manager
pGlobTerrain = TerrainObjectManager::Get("Grass");
TerrainManager::SetCurrentTerrain(pGlobTerrain); // Tell the terrain manager about our new terrain
pGlobTerrain->Initialize(); // Setup the terrain for drawing
}
TankScene::SceneEnd() {
delete pFrigate;
BulletFactory::Terminate(); // singleton instance of bullets dynamically allocated with an object pool
TerrainManager::DeinitializeCurrentTerrain(); // Tell the terrain manager to stop drawing
}

The header of the Frigate itself includes:

class Frigate : public GameObject {
public:
// Big four
Frigate();
Frigate(const Frigate&) = delete;
Frigate& operator=(const Frigate&) = delete;
~Frigate();
// Alarmable overrides
virtual void Alarm0() override;
virtual void Alarm1() override;
virtual void Alarm2() override;
virtual void Alarm3() override;
// Standard Update and Draw
virtual void Update() override;
virtual void Draw() override;
// Inputable
virtual void KeyPressed(AZUL_KEY k) override;
virtual void KeyReleased(AZUL_KEY k) override;
// Collidable
virtual void Collision(Cottage*);
virtual void Collision(Frigate*) { DebugMsg::out("Collision Frigate with Frigate\n"); }
// Full TRS ship positioning
Vect shipPos; // T
Matrix shipRot; // R
Matrix shipScale; // S
// Translation and rotation speed
const float shipTransSpeed = 1.0f;
const float shipRotAngle = 0.05f;
// Special camera controls
Vect CamShipOffset;
Vect CamLookAt;
private:
// A texture-light graphics object
GraphicsObject_TextureLight* pObjFT;
Matrix WorldMat1;
float angle;
};

The resulting implementation must define the overloaded functions and do something interesting with the position and activity of the model.

Frigate::Frigate() {
// Light parameters and graphics object setup.
Vect LightColor(1.50f, 1.50f, 1.50f, 1.0f);
Vect LightPos(1.0f, 1.0f, 1.0f, 1.0f);
pObjFT = new GraphicsObject_TextureLight(ModelManager::Get("Space_Frigate"), ShaderManager::Get("TextureFlatRender"), TextureManager::Get("Space_Frigate"), LightColor, LightPos);
// TRS and World matrix setup
shipScale.set(SCALE, 0.5f, 0.5f, 0.5f);
shipRot.set(IDENTITY);
shipPos.set(0, 20, 0);
Matrix world = shipScale * shipRot * Matrix(TRANS, shipPos);
pObjFT->SetWorld(world);
// Submission of all 'able's for registration
// Setting 3 different alarms with various time indexes
// Submitting key registration for space (to fire bullets)
Inputable::SubmitKeyRegistration(AZUL_KEY::KEY_SPACE, KEY_PRESS);
Inputable::SubmitKeyRegistration(AZUL_KEY::KEY_END, KEY_RELEASE);
// Setting a collider model for testing collision.
Collidable::SetColliderModel(pObjFT->getModel(), VolumeType::OBB); // The collider model may be either a BSphere, AABB, or OBB.
Collidable::SetCollidableGroup<Frigate>();
}

How these various 'able' functions are defined are going to vary wildly with each implementation of a game object. Below are a few common ways of implementing a couple of these functions.

void Frigate::Alarm0() {
DebugMsg::out("Alarm0 called in Frigate, deregistering alarm 2\n"); // Lets the user know what alarm triggered
Alarmable::SubmitAlarmDeregistration(2); // Deregisters a different alarm
}
void Frigate::KeyPressed(AZUL_KEY inputKey) {
switch (inputKey) { // Begrudgingly switch on the input
case AZUL_KEY::KEY_SPACE:
//DebugMsg::out("<SPACE> HAS BEEN PRESSED\n");
EnemyBulletFactory::CreateBullet(shipRot, shipPos); // Factory implementaiton!
break;
default:
DebugMsg::out("PROBLEM, Key %i was not meant to be processed", inputKey); // No key other than those registered should be present
}
}
void Frigate::Collision(Cottage* theCottage) {
UNREFERENCED_PARAMETER(theCottage); // Using only for type safety in this demo, though cottage work can be done.
DebugMsg::out("Collision Frigate with Cottage\n");
Visualizer::ShowBSphere(GetBSphere(), Color::Red); // Display a sphere on screen, be sure to checkout the visualizer tool.
}
void Frigate::Update() {
if (Keyboard::GetKeyState(AZUL_KEY::KEY_W))
{
shipPos += Vect(0, 0, 1) * shipRot * shipTransSpeed;
}
else if (Keyboard::GetKeyState(AZUL_KEY::KEY_S))
{
shipPos += Vect(0, 0, 1) * shipRot * -shipTransSpeed;
}
// Ship Rotation movement (not using time-based values for simplicity)
if (Keyboard::GetKeyState(AZUL_KEY::KEY_A))
{
shipRot *= Matrix(ROT_Y, shipRotAngle);
}
else if (Keyboard::GetKeyState(AZUL_KEY::KEY_D))
{
shipRot *= Matrix(ROT_Y, -shipRotAngle);
}
// Adjust Spaceship Matrix
Matrix world = shipScale * shipRot * Matrix(TRANS, shipPos);
pObjFT->SetWorld(world);
this->UpdateCollisionData(world);
}
void Frigate::Draw() {
pObjFT->Render(SceneManager::GetCurrentScene()->GetCamera());
}
Frigate::~Frigate() {
delete pObjFT;
}
Scene::SceneEnd
virtual void SceneEnd()=0
Shuts down the scene. Put scene related end tasks here.
ModelManager::Get
static Model * Get(std::string key)
Gets a model* using the given key.
Definition: ModelManager.h:115
Visualizer::ShowBSphere
static void ShowBSphere(const CollisionVolumeBSphere &S, const Vect &col=DEFAULT_COLOR)
Shows the b sphere with the passed in color (or a default color).
Definition: Visualizer.cpp:35
Alarmable::Alarm2
virtual void Alarm2()
Definition: Alarmable.cpp:26
Inputable::KeyReleased
virtual void KeyReleased(AZUL_KEY inputKey)
Key released callback.
Definition: Inputable.cpp:20
Alarmable::Alarm3
virtual void Alarm3()
Definition: Alarmable.cpp:30
ShaderManager::Get
static ShaderObject * Get(std::string key)
Gets a shader object* using the given key.
Definition: ShaderManager.h:96
Drawable::SubmitDrawRegistration
void SubmitDrawRegistration()
Submit draw registration.
Definition: Drawable.cpp:33
Alarmable::SubmitAlarmDeregistration
void SubmitAlarmDeregistration(int id)
Submit alarm deregistration.
Definition: Alarmable.cpp:63
Alarmable::SubmitAlarmRegistration
void SubmitAlarmRegistration(int id, float t)
Submit alarm registration.
Definition: Alarmable.cpp:50
Inputable::SubmitKeyRegistration
void SubmitKeyRegistration(AZUL_KEY inputKey, EVENT_TYPE e)
Submit key registration.
Definition: Inputable.cpp:39
Alarmable::Alarm0
virtual void Alarm0()
Definition: Alarmable.cpp:18
GameObject::operator=
GameObject & operator=(const GameObject &)=delete
Assignment operator.
Scene
Definition: Scene.h:21
TerrainObjectManager::Get
static TerrainObject * Get(std::string key)
Gets a terrain object* using the given key.
Definition: TerrainObjectManager.h:102
TerrainManager::SetCurrentTerrain
static void SetCurrentTerrain(TerrainObject *newTerrain)
Sets current terrain.
Definition: TerrainManager.h:81
TerrainObject
Definition: TerrainObject.h:15
TextureManager::Get
static Texture * Get(std::string key)
Gets a texture* using the given key.
Definition: TextureManager.h:99
SceneManager::GetCurrentScene
static Scene * GetCurrentScene()
Gets current scene.
Definition: SceneManager.h:156
TerrainManager::DeinitializeCurrentTerrain
static void DeinitializeCurrentTerrain()
Deinitialize the current terrain. Required if a scene will be revisited later on.
Definition: TerrainManager.h:86
Updatable::Update
virtual void Update()=0
Base class update.
Drawable::Draw
virtual void Draw()=0
Pure virtual function draw. Derived objects must override.
Updatable::SubmitUpdateRegistration
void SubmitUpdateRegistration()
Submit update registration.
Definition: Updatable.cpp:34
Alarmable::Alarm1
virtual void Alarm1()
Definition: Alarmable.cpp:22
Inputable::KeyPressed
virtual void KeyPressed(AZUL_KEY inputKey)
Key pressed callback.
Definition: Inputable.cpp:15
Collidable::SubmitCollisionRegistration
void SubmitCollisionRegistration()
Submits the collision registration from the user's perspective.
Definition: Collidable.cpp:31
Color::Red
static const Vect Red
Definition: Color.h:128
Scene::Initialize
virtual void Initialize()=0
Initializes the scene. Put scene related startup here.
Collidable::SetColliderModel
void SetColliderModel(Model *mod, VolumeType vol)
Sets collider model.
Definition: Collidable.cpp:43
GameObject
A game object entity.
Definition: GameObject.h:18