Arena.cpp
#include "Arena.h"
#include "StaticMesh.h"
#include "ArcadeGame.h"
void Arena::OnLoadResources()
{
// Load the models
resourceManager->LoadResourceModel("monkey_head", "/media/models/monkey.obj");
resourceManager->LoadResourceModel("arena_floor", "/media/models/arenagrid.obj");
resourceManager->LoadResourceModel("arena_walls", "/media/models/arenawalls.obj");
resourceManager->LoadResourceModel("pink_enemy", "/media/models/pinkenemy.obj");
resourceManager->LoadResourceModel("razor_enemy", "/media/models/razor.obj");
resourceManager->LoadResourceModel("player", "/media/models/playerbottom.obj");
// Load the textures
resourceManager->LoadResourceImage("arena_grid_large", "/media/images/arenagridlarge.png", true);
resourceManager->LoadResourceImage("arena_walls", "/media/images/arenawalls.png", true);
resourceManager->LoadResourceImage("pink_enemy", "/media/images/pinkenemy.png", true);
resourceManager->LoadResourceImage("player", "/media/images/playerbottom.png", true);
resourceManager->LoadResourceImage("tex_map", "/media/images/texmap.png", true);
// Load the shaders
resourceManager->LoadResourceShader("default_shader", "/media/shaders/default.vs", "/media/shaders/default.ps");
}
void Arena::OnStart()
{
std::cout << "Arena loaded" << std::endl;
SetGame<ArcadeGame>();
agk::SetCameraPosition(1, 0, 0, 130);
agk::SetCameraLookAt(1, 0, 0, 0, 180);
StaticMesh *floor = Spawn<StaticMesh>(AGKVector(92, 55, 0));
floor->meshComponent0->AddMesh("arena_floor");
floor->meshComponent0->SetMeshScale("arena_floor", AGKVector(9, 9, 9));
floor->meshComponent0->SetMeshTexture("arena_floor", "arena_grid_large");
StaticMesh *walls = Spawn<StaticMesh>(AGKVector(92, 55, 0));
walls->meshComponent0->AddMesh("arena_walls");
walls->meshComponent0->SetMeshScale("arena_walls", AGKVector(9, 9, 9));
walls->meshComponent0->SetMeshTexture("arena_walls", "arena_walls");
}
void Arena::OnEnd()
{
}
Arena extends my base World class which handles resources, simulaton, game modes etc. Arena is a basic world loading a few resources, and spawning and setting up some static meshes for the environment.
StaticMesh.cpp
#include "StaticMesh.h"
StaticMesh::StaticMesh() {}
StaticMesh::~StaticMesh() {}
void StaticMesh::OnSpawn()
{
meshComponent0 = AddComponent<MeshComponent>("mesh_component_0");
}
StaticMesh extends my base game Object, which all objects are derived from. StaticMesh is basically the base game object with a MeshComponent attached to it.
MeshComponent.cpp
#include "MeshComponent.h"
#include "Object.h"
#include "World.h"
std::vector<int> MeshComponent::allMeshes;
MeshComponent::MeshComponent()
{
}
MeshComponent::~MeshComponent()
{
}
void MeshComponent::Clean()
{
//std::cout << "Deleting Meshes" << std::endl;
for(std::map<std::string, Mesh*>::iterator it = meshes.begin(); it != meshes.end(); ++it) {
agk::DeleteObject((*it).second->id);
delete (*it).second;
}
}
Mesh *MeshComponent::AddMesh(const std::string &name)
{
Mesh *m = new Mesh;
m->id = agk::InstanceObject(owner->world->resourceManager->models[name]);
meshes[name] = m;
allMeshes.push_back(m->id);
agk::SetObjectPosition(m->id, owner->GetX(), owner->GetY(), owner->GetZ());
SetMeshVisible(name, true);
agk::SetObjectLightMode(m->id, 1);
return m;
}
void MeshComponent::SetMeshVisible(const std::string &name, const bool &mode)
{
agk::SetObjectVisible(meshes[name]->id, mode);
}
void MeshComponent::SetMesh(const std::string &name)
{
}
void MeshComponent::SetShader(const std::string &meshName, const std::string &shaderName)
{
agk::SetObjectShader(meshes[meshName]->id, owner->world->resourceManager->shaders[shaderName]);
}
void MeshComponent::SetMeshScale(const std::string &meshName, AGKVector scale)
{
meshes[meshName]->scale = scale;
agk::SetObjectScale(meshes[meshName]->id, scale.x, scale.y, scale.z);
}
void MeshComponent::SetMeshTexture(const std::string &meshName, const std::string &imageName)
{
agk::SetObjectImage(meshes[meshName]->id, owner->world->resourceManager->images[imageName], 0);
}
void MeshComponent::SetMeshColor(const std::string &meshName, const float &r, const float &g, const float &b, const float &a) {
agk::SetObjectColor(meshes[meshName]->id, r, g, b, a);
}
MeshComponent is basically a bunch of wrappers around AppGameKit commands for setting up meshes and indexing them within the MeshComponent.
The only difference between my enemies and static meshes, is that my enemies extend my MovableObject class, which is basically the base game Object with a MovementComponent and a MovableMeshComponent(which is a MeshComponent with some code to sync up the mesh position with the object position)attached. Display wise, both enemies and environment use the mesh component for their models
MovableMeshComponent.cpp
#include "MovableMeshComponent.h"
#include "Object.h"
MovableMeshComponent::MovableMeshComponent()
{
}
MovableMeshComponent::~MovableMeshComponent()
{
}
void MovableMeshComponent::Step(float dt)
{
if(owner->isStatic)
{
return;
}
for(std::map<std::string, Mesh*>::iterator it = meshes.begin(); it != meshes.end(); ++it) {
agk::SetObjectPosition((*it).second->id, owner->position.x, owner->position.y, owner->position.z);
agk::SetObjectRotation((*it).second->id, owner->rotation.x, owner->rotation.y, owner->rotation.z);
}
}
void MovableMeshComponent::FrameUpdate()
{
}
EnemyBase.cpp
#include "EnemyBase.h"
#include "BaseGame.h"
#include <vector>
#include "Arena.h"
#include "MathHelper.h"
EnemyBase::EnemyBase() : scoreValue(5), enemyTypeId(0) {
collisionComponent = AddComponent<CollisionComponent>("collision_component0");
}
EnemyBase::~EnemyBase() {
}
void EnemyBase::OnSpawn() {
}
void EnemyBase::CorrectLocation()
{
float dist = MathHelper::PointDistance2D(GetX(), GetY(), target->GetX(), target->GetY());
if(dist < 80)
{
float dir = MathHelper::PointDirection2D(target->GetX(), target->GetY(), GetX(), GetY());
SetPosition(AGKVector(target->GetX() + MathHelper::LengthDirectionX(80, dir), target->GetY() + MathHelper::LengthDirectionY(80, dir), GetZ()));
}
ConstrainToArena();
}
void EnemyBase::Step(float dt) {
}
void EnemyBase::BulletCheck() {
/*
for(std::vector<Bullet*>::iterator it = target->bullets.begin(); it != target->bullets.end();) {
float dist = MathHelper::PointDistance2D((*it)->GetX(), (*it)->GetY(), GetX(), GetY());
if(dist < 3) {
deathType = BULLET;
(*it)->Destroy();
it = target->bullets.erase(it);
Destroy();
}
else
++it;
}
*/
}
void EnemyBase::PlayerCheck() {
/*
float dist = MathHelper::PointDistance2D(GetX(), GetY(), target->GetX(), target->GetY());
if(dist < 4) {
if(target->hasShield)
{
level->game->EnemyDestroyed(id);
Destroy();
return;
}
level->game->EnemyDestroyed(id);
Destroy();
target->Kill();
}
*/
}
void EnemyBase::ConstrainToArena() {
float xx = GetX();
float yy = GetY();
if(xx < 0)
{
SetX(0);
}
if(xx > 200)
{
SetX(200);
}
if(yy < 0)
{
SetY(0);
}
if(yy > 110)
{
SetY(110);
}
}
void EnemyBase::OnDestroy() {
/*
if(deathType == BULLET) {
if(enemyTypeId == target->lastKilled) {
++target->combo;
target->comboResetTimer = 60.0f;
}
else {
target->lastKilled = 0;
target->combo = 1;
}
Force *f = new Force(GetPosition(), 1);
agk::SetShaderConstantByName(level->resourceMgr->GetShader("shadervtest"), "game_force", f->force.x, f->force.y, f->force.z, 0);
f->level = level;
level->forces.push_back(f);
Multiplier *m = level->Spawn<Multiplier>("multiplier", GetPosition());
m->target = target;
level->particleSystem->RemoveParticleForce("exp");
level->testEmitter->Burst(GetPosition());
level->particleSystem->AddParticleForce("exp", ParticleForce::ParticleForceType::REPULSOR);
level->particleSystem->SetParticleForceStrength("exp", 10.0f);
level->particleSystem->SetParticleForceLocation("exp", GetPosition());
target->score += (scoreValue * target->combo) * target->multiplierCount;
target->lastKilled = enemyTypeId;
level->game->EnemyDestroyed(id);
agk::PlaySound(5, 15);
}
level->brightness += 10000000;
*/
}
void EnemyBase::BombCheck() {
/*
if(target->bombActive) {
const float dist = MathHelper::PointDistance3D(GetPosition(), target->GetPosition());
if(dist < target->bombRadius) {
deathType = BOMB;
force += (AGKVectorExt(GetPosition() - target->GetPosition()) / dist) * 5;
level->game->EnemyDestroyed(id);
Destroy();
}
}
*/
}
EnemyBase is the base class of all enemies.
PinkEnemy.cpp
#include "PinkEnemy.h"
#include "MathHelper.h"
#include "World.h"
void PinkEnemy::OnSpawn() {
EnemyBase::OnSpawn();
collisionComponent->SetCollisionChannel(1);
collisionComponent->AddCollisionChannel(1);
meshComponent0->AddMesh("pink_enemy");
meshComponent0->SetMeshTexture("pink_enemy", "pink_enemy");
meshComponent0->SetMeshScale("pink_enemy", AGKVector(4, 4, 4));
meshComponent0->SetMeshColor("pink_enemy", 255, 0, 234, 1000);
}
void PinkEnemy::PrePlay()
{
tag = "pink_enemy";
}
void PinkEnemy::Step(float dt)
{
}
As you can see the pink enemy mesh is set up in the same way as meshes for the environment (static meshes) above, yet they aren't visible.
So to summarize the three things I need to fix are the clear color always being blue, textures not appearing on objects and some objects not appearing regardless of them using the same object system as others. None of these problems occur in windows.