From 0369d8cf199f0d05ed13e0ee9900e6aad4c2af43 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Tue, 25 Feb 2014 14:36:54 +0100 Subject: [PATCH 1/3] Not finished Pickup system. --- Code/Game/GameLogic/CollisionManager.cpp | 29 +++++++++++++++ Code/Game/GameLogic/GameLogic.vcxproj | 6 ++++ Code/Game/GameLogic/PickupSystem/Pickup.cpp | 29 +++++++++++++++ Code/Game/GameLogic/PickupSystem/Pickup.h | 35 +++++++++++++++++++ .../GameLogic/PickupSystem/PickupHealth.cpp | 18 ++++++++++ .../GameLogic/PickupSystem/PickupHealth.h | 27 ++++++++++++++ .../GameLogic/PickupSystem/PickupSystem.cpp | 22 ++++++++++++ .../GameLogic/PickupSystem/PickupSystem.h | 30 ++++++++++++++++ Code/Game/LevelLoader/ObjectDefines.h | 1 + 9 files changed, 197 insertions(+) create mode 100644 Code/Game/GameLogic/PickupSystem/Pickup.cpp create mode 100644 Code/Game/GameLogic/PickupSystem/Pickup.h create mode 100644 Code/Game/GameLogic/PickupSystem/PickupHealth.cpp create mode 100644 Code/Game/GameLogic/PickupSystem/PickupHealth.h create mode 100644 Code/Game/GameLogic/PickupSystem/PickupSystem.cpp create mode 100644 Code/Game/GameLogic/PickupSystem/PickupSystem.h diff --git a/Code/Game/GameLogic/CollisionManager.cpp b/Code/Game/GameLogic/CollisionManager.cpp index 1ab9afbd..a5ae6380 100644 --- a/Code/Game/GameLogic/CollisionManager.cpp +++ b/Code/Game/GameLogic/CollisionManager.cpp @@ -10,6 +10,8 @@ #include "Portal.h" #include "ExplosiveCrate.h" +#include "PickupSystem/PickupHealth.h" + using namespace Oyster; using namespace GameLogic; @@ -281,4 +283,31 @@ using namespace GameLogic; } + } + + //General collision collision for pickups + //It calls the collision function defined in each pickup. + void Pickup::PickupCollision(Oyster::Physics::ICustomBody* objA, Oyster::Physics::ICustomBody* objB, Oyster::Math::Float kineticEnergyLoss) + { + //Check if player is a player. + Object* a = (Object*)objA->GetCustomTag(); + Object* b = (Object*)objB->GetCustomTag(); + + if(!a) + return; + if(!b) + return; + + if(b->GetObjectType() == ObjectSpecialType_Player) + { + ((Pickup*)a)->OnCollision((Player*)(b)); + } + else if(a->GetObjectType() != ObjectSpecialType_Player) + { + //One of the objects are not a player. + //Do nothing. + return; + } + + ((Pickup*)b)->OnCollision((Player*)a); } \ No newline at end of file diff --git a/Code/Game/GameLogic/GameLogic.vcxproj b/Code/Game/GameLogic/GameLogic.vcxproj index 52b7722b..08a932d2 100644 --- a/Code/Game/GameLogic/GameLogic.vcxproj +++ b/Code/Game/GameLogic/GameLogic.vcxproj @@ -173,6 +173,9 @@ + + + @@ -194,6 +197,9 @@ + + + diff --git a/Code/Game/GameLogic/PickupSystem/Pickup.cpp b/Code/Game/GameLogic/PickupSystem/Pickup.cpp new file mode 100644 index 00000000..84b95e87 --- /dev/null +++ b/Code/Game/GameLogic/PickupSystem/Pickup.cpp @@ -0,0 +1,29 @@ +#include "Pickup.h" + +using namespace GameLogic; + +Pickup::Pickup(Oyster::Physics::ICustomBody *rigidBody, EventOnCollision collisionFunc, ObjectSpecialType type, int objectID, Oyster::Math::Float spawnTime) + : StaticObject(rigidBody, collisionFunc, type, objectID) +{ + this->active = true; + this->spawnTime = spawnTime; +} + +Pickup::~Pickup() +{} + +void Pickup::Update() +{ + if(!active) + { + if(timer.getElapsedSeconds() >= spawnTime) + { + active = true; + } + } +} + +bool Pickup::IsActive() +{ + return active; +} \ No newline at end of file diff --git a/Code/Game/GameLogic/PickupSystem/Pickup.h b/Code/Game/GameLogic/PickupSystem/Pickup.h new file mode 100644 index 00000000..e48017fc --- /dev/null +++ b/Code/Game/GameLogic/PickupSystem/Pickup.h @@ -0,0 +1,35 @@ +#ifndef PICKUP_H +#define PICKUP_H + +#include "../StaticObject.h" +#include "../Player.h" +#include "WinTimer.h" + +typedef void (*EventOnCollision)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter,Oyster::Math::Float kineticEnergyLoss); + +namespace GameLogic +{ + class Pickup : public StaticObject + { + public: + Pickup(Oyster::Physics::ICustomBody *rigidBody, EventOnCollision collisionFunc, ObjectSpecialType type, int objectID, Oyster::Math::Float spawnTime); + virtual ~Pickup(); + + virtual void Update(); + + bool IsActive(); + + virtual void OnCollision(Player *player) = 0; + + static void PickupCollision(Oyster::Physics::ICustomBody *rigidBodyCrate, Oyster::Physics::ICustomBody *obj, Oyster::Math::Float kineticEnergyLoss); + + protected: + bool active; + + Utility::WinTimer timer; + double spawnTime; + + }; +} + +#endif \ No newline at end of file diff --git a/Code/Game/GameLogic/PickupSystem/PickupHealth.cpp b/Code/Game/GameLogic/PickupSystem/PickupHealth.cpp new file mode 100644 index 00000000..56cbef1e --- /dev/null +++ b/Code/Game/GameLogic/PickupSystem/PickupHealth.cpp @@ -0,0 +1,18 @@ +#include "PickupHealth.h" + +using namespace GameLogic; + +PickupHealth::PickupHealth(Oyster::Physics::ICustomBody *rigidBody, ObjectSpecialType type, int objectID, Oyster::Math::Float spawnTime, Oyster::Math::Float healthValue) + : Pickup(rigidBody, Pickup::PickupCollision, type, objectID, spawnTime) +{ + this->hpValue = healthValue; +} + +PickupHealth::~PickupHealth() +{} + +void PickupHealth::OnCollision(Player *player) +{ + timer.reset(); + player->DamageLife(-hpValue); +} \ No newline at end of file diff --git a/Code/Game/GameLogic/PickupSystem/PickupHealth.h b/Code/Game/GameLogic/PickupSystem/PickupHealth.h new file mode 100644 index 00000000..5d5db809 --- /dev/null +++ b/Code/Game/GameLogic/PickupSystem/PickupHealth.h @@ -0,0 +1,27 @@ +////////////////////////////////////// +// Created by Pontus Fransson 2014 // +////////////////////////////////////// + +#ifndef PICKUP_HEALTH_H +#define PICKUP_HEALTH_H + +#include "Pickup.h" + +namespace GameLogic +{ + class PickupHealth : public Pickup + { + public: + PickupHealth(Oyster::Physics::ICustomBody *rigidBody, ObjectSpecialType type, int objectID, Oyster::Math::Float spawnTime, Oyster::Math::Float HealthValue); + virtual ~PickupHealth(); + + + void OnCollision(Player *player); + + protected: + int hpValue; + + }; +} + +#endif \ No newline at end of file diff --git a/Code/Game/GameLogic/PickupSystem/PickupSystem.cpp b/Code/Game/GameLogic/PickupSystem/PickupSystem.cpp new file mode 100644 index 00000000..0f60ddc9 --- /dev/null +++ b/Code/Game/GameLogic/PickupSystem/PickupSystem.cpp @@ -0,0 +1,22 @@ +#include "PickupSystem.h" + +using namespace GameLogic; + +PickupSystem::PickupSystem() +{} + +PickupSystem::~PickupSystem() +{} + +void PickupSystem::CreatePickup(Pickup* pickup) +{ + pickups.push_back(pickup); +} + +void PickupSystem::Update() +{ + for(int i = 0; i < pickups.size(); i++) + { + pickups.at(i)->Update(); + } +} \ No newline at end of file diff --git a/Code/Game/GameLogic/PickupSystem/PickupSystem.h b/Code/Game/GameLogic/PickupSystem/PickupSystem.h new file mode 100644 index 00000000..4bd1e69e --- /dev/null +++ b/Code/Game/GameLogic/PickupSystem/PickupSystem.h @@ -0,0 +1,30 @@ +////////////////////////////////////// +// Created by Pontus Fransson 2014 // +////////////////////////////////////// + +#ifndef PICKUP_SYSTEM_H +#define PICKUP_SYSTEM_H + +#include + +#include "Pickup.h" + +namespace GameLogic +{ + class PickupSystem + { + public: + PickupSystem(); + ~PickupSystem(); + + void CreatePickup(Pickup* pickup); + + void Update(); + + private: + std::vector> pickups; + + }; +} + +#endif \ No newline at end of file diff --git a/Code/Game/LevelLoader/ObjectDefines.h b/Code/Game/LevelLoader/ObjectDefines.h index 918fb4e6..151bf27c 100644 --- a/Code/Game/LevelLoader/ObjectDefines.h +++ b/Code/Game/LevelLoader/ObjectDefines.h @@ -42,6 +42,7 @@ namespace GameLogic ObjectSpecialType_Player, ObjectSpecialType_Generic, + ObjectSpecialType_PickupHealth, ObjectSpecialType_Count, ObjectSpecialType_Unknown = -1 From d56d1d75694da7369481e499132b98b2beb6fe8a Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Tue, 25 Feb 2014 16:07:50 +0100 Subject: [PATCH 2/3] Fixed error with reading lights from level format. --- Code/Game/LevelLoader/ParseFunctions.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Code/Game/LevelLoader/ParseFunctions.cpp b/Code/Game/LevelLoader/ParseFunctions.cpp index ac361c46..060180a2 100644 --- a/Code/Game/LevelLoader/ParseFunctions.cpp +++ b/Code/Game/LevelLoader/ParseFunctions.cpp @@ -23,9 +23,9 @@ namespace GameLogic void ParseLight(char* buffer, BasicLight& header, int& size) { int start = 0; - memcpy(&header.typeID, &buffer[start], 40); - start += 40; - /* + memcpy(&header.typeID, &buffer[start], 4); + start += 4; + memcpy(&header.lightType, &buffer[start], 4); start += 4; @@ -39,7 +39,7 @@ namespace GameLogic start += 4; memcpy(&header.intensity, &buffer[start], 4); - start += 4;*/ + start += 4; size += start; From 11eda5065e09d9f5b114cf13ba67506943ab35ee Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Tue, 25 Feb 2014 16:10:02 +0100 Subject: [PATCH 3/3] Reading hp pickups from levelformat. Updating pickups. --- Code/Game/GameLogic/Level.cpp | 16 +++++++++++++--- Code/Game/GameLogic/Level.h | 4 ++++ Code/Game/GameLogic/PickupSystem/Pickup.cpp | 3 +++ Code/Game/LevelLoader/LevelParser.cpp | 15 +++++++++++++++ Code/Game/LevelLoader/ObjectDefines.h | 6 +++++- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Code/Game/GameLogic/Level.cpp b/Code/Game/GameLogic/Level.cpp index 439f3ac8..0f0ab632 100644 --- a/Code/Game/GameLogic/Level.cpp +++ b/Code/Game/GameLogic/Level.cpp @@ -131,6 +131,11 @@ Object* Level::CreateGameObj(ObjectHeader* obj, ICustomBody* rigidBody) gameObj = new StaticObject(rigidBody, Object::DefaultOnCollision, (ObjectSpecialType)obj->specialTypeID, objID); } break; + case ObjectSpecialType_PickupHealth: + { + gameObj = new PickupHealth(rigidBody, obj->specialTypeID, objID, ((PickupHealthAttributes*)obj)->spawnTime, ((PickupHealthAttributes*)obj)->healthValue); + } + break; default: { gameObj = new StaticObject(rigidBody, Object::DefaultOnCollision, (ObjectSpecialType)obj->specialTypeID, objID); @@ -216,7 +221,6 @@ bool Level::InitiateLevel(std::wstring levelPath) std::string convertedLevelPath = converterX.to_bytes(levelPath); objects = ll.LoadLevel(convertedLevelPath); - if(objects.size() == 0) return false; @@ -264,7 +268,12 @@ bool Level::InitiateLevel(std::wstring levelPath) { // create game object Object* staticGameObj = CreateGameObj(staticObjData, rigidBody_Static); - if(staticGameObj != NULL) + + if(staticObjData->specialTypeID == ObjectSpecialType_PickupHealth) + { + this->pickupSystem.CreatePickup((PickupHealth*)staticGameObj); + } + else if(staticGameObj != NULL) { this->staticObjects.Push((StaticObject*)staticGameObj); } @@ -321,6 +330,7 @@ bool Level::InitiateLevel(std::wstring levelPath) break; } } + return true; } bool Level::InitiateLevel(float radius) @@ -440,7 +450,7 @@ void Level::Update(float deltaTime) } } - + this->pickupSystem.Update(); } int Level::getNrOfDynamicObj() { diff --git a/Code/Game/GameLogic/Level.h b/Code/Game/GameLogic/Level.h index 9fb3dbad..cf7ab6d8 100644 --- a/Code/Game/GameLogic/Level.h +++ b/Code/Game/GameLogic/Level.h @@ -16,6 +16,9 @@ #include "DynamicArray.h" #include "LevelLoader.h" +#include "PickupSystem\PickupSystem.h" +#include "PickupSystem\PickupHealth.h" + const int DEATH_TIMER = 5; namespace GameLogic { @@ -89,6 +92,7 @@ namespace GameLogic StaticObject *levelObj; int objID; Utility::DynamicMemory::DynamicArray spawnPoints; + PickupSystem pickupSystem; }; diff --git a/Code/Game/GameLogic/PickupSystem/Pickup.cpp b/Code/Game/GameLogic/PickupSystem/Pickup.cpp index 84b95e87..73a319f8 100644 --- a/Code/Game/GameLogic/PickupSystem/Pickup.cpp +++ b/Code/Game/GameLogic/PickupSystem/Pickup.cpp @@ -1,4 +1,5 @@ #include "Pickup.h" +#include "../Game.h" using namespace GameLogic; @@ -7,6 +8,7 @@ Pickup::Pickup(Oyster::Physics::ICustomBody *rigidBody, EventOnCollision collisi { this->active = true; this->spawnTime = spawnTime; + timer.reset(); } Pickup::~Pickup() @@ -19,6 +21,7 @@ void Pickup::Update() if(timer.getElapsedSeconds() >= spawnTime) { active = true; + ((Game*)&Game::Instance())->onEnableFnc(this); } } } diff --git a/Code/Game/LevelLoader/LevelParser.cpp b/Code/Game/LevelLoader/LevelParser.cpp index fe349988..680b0358 100644 --- a/Code/Game/LevelLoader/LevelParser.cpp +++ b/Code/Game/LevelLoader/LevelParser.cpp @@ -156,6 +156,21 @@ std::vector> LevelParser::Parse(std::string filen objects.push_back(header); break; } + + case ObjectSpecialType_PickupHealth: + { + PickupHealthAttributes* header = new PickupHealthAttributes; + ParseObject(&buffer[counter], *header, counter, loadCgf); + + ParseObject(&buffer[counter], &header->spawnTime, 4); + counter += 4; + + ParseObject(&buffer[counter], &header->healthValue, 4); + counter += 4; + + objects.push_back(header); + break; + } //this is a hotfix, fix so you only load the relevant data when the file is updated default: //Couldn't find specialType diff --git a/Code/Game/LevelLoader/ObjectDefines.h b/Code/Game/LevelLoader/ObjectDefines.h index 9ce7d86a..aadc3382 100644 --- a/Code/Game/LevelLoader/ObjectDefines.h +++ b/Code/Game/LevelLoader/ObjectDefines.h @@ -259,7 +259,11 @@ namespace GameLogic float skySize; }; - + struct PickupHealthAttributes : public ObjectHeader + { + float spawnTime; + float healthValue; + };