Merge branch 'GameLogic' of https://github.com/dean11/Danbias into GameLogic

This commit is contained in:
lindaandersson 2014-02-11 10:11:57 +01:00
commit 071624779e
11 changed files with 658 additions and 2 deletions

View File

@ -17,6 +17,9 @@
#include "vld.h" #include "vld.h"
#include "GameClientRecieverFunc.h" #include "GameClientRecieverFunc.h"
#include "../Misc/EventHandler/EventHandler.h"
using namespace Oyster::Event;
namespace DanBias namespace DanBias
{ {
@ -138,8 +141,6 @@ namespace DanBias
HRESULT DanBiasGame::Update(float deltaTime) HRESULT DanBiasGame::Update(float deltaTime)
{ {
m_data->inputObj->Update(); m_data->inputObj->Update();
if(m_data->serverOwner) if(m_data->serverOwner)

View File

@ -0,0 +1,261 @@
///////////////////////
// Sam Svensson 2013 //
///////////////////////
#ifndef MISC_EVENT_BUTTON_H
#define MISC_EVENT_BUTTON_H
#include "IEventButton.h"
namespace Oyster
{
namespace Event
{
template <typename Owner>
struct ButtonEvent
{
ButtonState state;
IEventButton* sender;
Owner owner;
void* userData;
};
template <typename Owner>
class EventButton : public IEventButton
{
protected:
//typedef for callback function pointer
typedef void (*EventFunc)(Oyster::Event::ButtonEvent<Owner>& e);
struct PrivData
{
PrivData() : ID(currID++){}
static unsigned int currID;
const unsigned int ID;
ButtonState previousState;
Owner owner;
EventFunc eventCallback;
void* userData;
bool enabled;
};
PrivData privData;
private:
//Implement this in the inherited classes for collision against that shape.
virtual bool Collision(InputClass *input) = 0;
public:
EventButton();
EventButton(Owner owner);
EventButton(EventFunc func);
EventButton(EventFunc func, Owner owner);
EventButton(EventFunc func, Owner owner, void* userData);
~EventButton();
void Update(InputClass *input);
//Send event to callback function
void SendEvent(ButtonState state);
//Set
void SetUserData(void* data);
void SetEventFunc(EventFunc func);
void SetOwner(Owner owner);
//Get
bool Enabled();
unsigned int GetID();
//EventFunc GetFunctionPointer();
Owner GetOwner();
bool operator ==(const EventButton<Owner>& obj);
};
template <typename Owner>
unsigned int EventButton<Owner>::PrivData::currID = 0;
template <typename Owner>
EventButton<Owner>::EventButton()
{
this->privData.eventCallback = NULL;
this->privData.userData = NULL;
this->privData.previousState = ButtonState_None;
this->privData.enabled = true;
}
template <typename Owner>
EventButton<Owner>::EventButton(Owner owner)
{
this->privData.owner = owner;
this->privData.eventCallback = NULL;
this->privData.userData = NULL;
this->privData.previousState = ButtonState_None;
this->privData.enabled = true;
}
template <typename Owner>
EventButton<Owner>::EventButton(EventFunc func)
{
this->privData.eventCallback = func;
this->privData.userData = NULL;
this->privData.previousState = ButtonState_None;
this->privData.enabled = true;
}
template <typename Owner>
EventButton<Owner>::EventButton(EventFunc func, Owner owner)
{
this->privData.owner = owner;
this->privData.eventCallback = func;
this->privData.userData = NULL;
this->privData.previousState = ButtonState_None;
this->privData.enabled = true;
}
template <typename Owner>
EventButton<Owner>::EventButton(EventFunc func, Owner owner, void* userData)
{
this->privData.owner = owner;
this->privData.eventCallback = func;
this->privData.userData = userData;
this->privData.previousState = ButtonState_None;
this->privData.enabled = true;
}
template <typename Owner>
EventButton<Owner>::~EventButton()
{}
//Checks for collision and
template <typename Owner>
void EventButton<Owner>::Update(InputClass *input)
{
if(this->privData.enabled)
{
ButtonState currentState = ButtonState_None;
if(Collision(input))
{
if(input->IsMousePressed())
{
//Change state when the mouse button is pressed
switch(this->privData.previousState)
{
case ButtonState_None:
currentState = ButtonState_Hover;
break;
case ButtonState_Hover:
case ButtonState_Released:
currentState = ButtonState_Pressed;
break;
case ButtonState_Pressed:
case ButtonState_Down:
currentState = ButtonState_Down;
break;
default:
break;
}
}
else
{
//Change state when the mouse button is NOT pressed
switch(this->privData.previousState)
{
case ButtonState_None:
case ButtonState_Hover:
case ButtonState_Released:
currentState = ButtonState_Hover;
break;
case ButtonState_Pressed:
case ButtonState_Down:
currentState = ButtonState_Released;
break;
default:
break;
}
}
}
//Only call the callback function when the state has changed.
if(this->privData.previousState != currentState)
SendEvent(currentState);
this->privData.previousState = currentState;
}
}
template <typename Owner>
void EventButton<Owner>::SendEvent(ButtonState state)
{
if(privData.eventCallback != NULL)
{
Oyster::Event::ButtonEvent<Owner> event;
event.state = state;
event.sender = this;
event.owner = privData.owner;
event.userData = privData.userData;
privData.eventCallback(event);
}
}
template <typename Owner>
void EventButton<Owner>::SetUserData(void* data)
{
this->privData.userData = data;
}
template <typename Owner>
void EventButton<Owner>::SetEventFunc(EventFunc func)
{
this->privData.EventFunc = EventFunc;
}
template <typename Owner>
void EventButton<Owner>::SetOwner(Owner owner)
{
this->privData.owner = owner;
}
template <typename Owner>
bool EventButton<Owner>::Enabled()
{
return this->privData.enabled;
}
template <typename Owner>
unsigned int EventButton<Owner>::GetID()
{
return this->privData.ID;
}
/* Something is wrong, can't return EventFunc
template <typename Owner>
EventFunc EventButton<Owner>::GetFunctionPointer()
{
return this->privData.eventCallback;
}*/
template <typename Owner>
Owner EventButton<Owner>::GetOwner()
{
return this->privData.owner;
}
template <typename Owner>
bool EventButton<Owner>::operator ==(const EventButton<Owner>& obj)
{
return (this->privData.ID == obj.privData.ID);
}
}
}
#endif

View File

@ -0,0 +1,64 @@
//////////////////////////////////////
// Created by Pontus Fransson 2014 //
//////////////////////////////////////
#ifndef MISC_EVENT_BUTTON_CIRCLE_H
#define MISC_EVENT_BUTTON_CIRCLE_H
#include "EventButton.h"
#include "../../Input/L_inputClass.h"
namespace Oyster
{
namespace Event
{
template <typename Owner>
class EventButtonCircle : public EventButton<Owner>
{
public:
EventButtonCircle()
: EventButton(), xPos(0), yPos(0), radius(0)
{}
EventButtonCircle(Owner owner, float xPos, float yPos, float radius)
: EventButton(owner), xPos(xPos), yPos(yPos), radius(radius)
{}
EventButtonCircle(void (*EventFunc)( Oyster::Event::ButtonEvent<Owner>& e), float xPos, float yPos, float radius)
: EventButton(EventFunc), xPos(xPos), yPos(yPos), radius(radius)
{}
EventButtonCircle(void (*EventFunc)( Oyster::Event::ButtonEvent<Owner>& e), Owner owner, float xPos, float yPos, float radius)
: EventButton(EventFunc, owner), xPos(xPos), yPos(yPos), radius(radius)
{}
EventButtonCircle(void (*EventFunc)( Oyster::Event::ButtonEvent<Owner>& e), Owner owner, void* userData, float xPos, float yPos, float radius)
: EventButton(EventFunc, owner, userData), xPos(xPos), yPos(yPos), radius(radius)
{}
~EventButtonCircle()
{}
//Circle vs point collision
bool Collision(InputClass* inputObject)
{
//Should come from the InputClass
float xMouse = 2, yMouse = 2;
float xDiff = xMouse - xPos;
float yDiff = yMouse - yPos;
float length = (xDiff * xDiff) + (yDiff * yDiff);
if(length <= radius*radius)
{
return true;
}
return false;
}
private:
float xPos, yPos;
float radius;
};
}
}
#endif

View File

@ -0,0 +1,51 @@
//////////////////////////////////////
// Created by Pontus Fransson 2014 //
//////////////////////////////////////
#include "EventButtonCollection.h"
#include "../../Input/L_inputClass.h"
using namespace Oyster::Event;
EventButtonCollection::EventButtonCollection()
: collectionState(EventCollectionState_Enabled)
{
}
EventButtonCollection::~EventButtonCollection()
{
int size = buttons.size();
for(int i = 0; i < size; i++)
{
delete buttons[i];
buttons[i] = NULL;
}
}
void EventButtonCollection::Update(InputClass* inputObject)
{
if(this->collectionState == EventCollectionState_Enabled)
{
for(int i = 0; i < (int)buttons.size(); i++)
{
buttons[i]->Update(inputObject);
}
}
}
EventCollectionState EventButtonCollection::GetState() const
{
return collectionState;
}
void EventButtonCollection::SetState(const EventCollectionState state)
{
collectionState = state;
}
void EventButtonCollection::Clear()
{
buttons.clear();
collectionState = EventCollectionState_Enabled;
}

View File

@ -0,0 +1,58 @@
//////////////////////////////////////
// Created by Pontus Fransson 2014 //
//////////////////////////////////////
#ifndef MISC_EVENT_BUTTON_COLLECTION_H
#define MISC_EVENT_BUTTON_COLLECTION_H
#include "../../Input/L_inputClass.h"
#include "../DynamicArray.h"
#include "IEventButton.h"
#include "EventButton.h"
#include <vector>
namespace Oyster
{
namespace Event
{
enum EventCollectionState
{
EventCollectionState_Disabled,
EventCollectionState_Enabled,
EventCollectionState_Count,
EventCollectionState_Unknown = -1,
};
class EventButtonCollection
{
public:
EventButtonCollection();
~EventButtonCollection();
void Update(InputClass* inputObject);
template <typename Owner>
void AddButton(EventButton<Owner>* button)
{
buttons.push_back(button);
}
EventCollectionState GetState() const;
void SetState(const EventCollectionState state);
//Clear all buttons and reset the state.
void Clear();
private:
std::vector<IEventButton*> buttons;
EventCollectionState collectionState;
};
}
}
#endif

View File

@ -0,0 +1,60 @@
//////////////////////////////////////
// Created by Pontus Fransson 2014 //
//////////////////////////////////////
#ifndef MISC_EVENT_BUTTON_RECTANGLE_H
#define MISC_EVENT_BUTTON_RECTANGLE_H
#include "EventButton.h"
#include "../../Input/L_inputClass.h"
namespace Oyster
{
namespace Event
{
template <typename Owner>
class EventButtonRectangle : public EventButton<Owner>
{
public:
EventButtonRectangle()
: EventButton(), xPos(0), yPos(0), halfWidth(0), halfHeight(0)
{}
EventButtonRectangle(Owner owner, float xPos, float yPos, float halfWidth, float halfHeight)
: EventButton(owner), xPos(xPos), yPos(yPos), halfWidth(halfWidth), halfHeight(halfHeight)
{}
EventButtonRectangle(void (*EventFunc)( Oyster::Event::ButtonEvent<Owner>& e), float xPos, float yPos, float halfWidth, float halfHeight)
: EventButton(EventFunc), xPos(xPos), yPos(yPos), halfWidth(halfWidth), halfHeight(halfHeight)
{}
EventButtonRectangle(void (*EventFunc)( Oyster::Event::ButtonEvent<Owner>& e), Owner owner, float xPos, float yPos, float halfWidth, float halfHeight)
: EventButton(EventFunc, owner), xPos(xPos), yPos(yPos), halfWidth(halfWidth), halfHeight(halfHeight)
{}
EventButtonRectangle(void (*EventFunc)( Oyster::Event::ButtonEvent<Owner>& e), Owner owner, void* userData, float xPos, float yPos, float halfWidth, float halfHeight)
: EventButton(EventFunc, owner, userData), xPos(xPos), yPos(yPos), halfWidth(halfWidth), halfHeight(halfHeight)
{}
~EventButtonRectangle()
{}
//Circle vs point collision
bool Collision(InputClass* inputObject)
{
//Should come from the InputClass
float xMouse = 1, yMouse = 0;
if(xMouse >= xPos - halfWidth && xMouse <= xPos + halfWidth
&& yMouse >= yPos - halfHeight && yMouse <= yPos + halfHeight)
{
return true;
}
return false;
}
private:
float xPos, yPos;
float halfWidth, halfHeight;
};
}
}
#endif

View File

@ -0,0 +1,47 @@
//////////////////////////////////////
// Created by Pontus Fransson 2014 //
//////////////////////////////////////
#include "EventHandler.h"
using namespace Oyster::Event;
Oyster::Event::EventHandler EvtHandler;
EventHandler& EventHandler::Instance()
{
return EvtHandler;
}
EventHandler::EventHandler()
{
}
EventHandler::~EventHandler()
{
int size = collections.size();
for(int i = 0; i < size; i++)
{
delete collections[i];
}
}
void EventHandler::Update(InputClass* inputObject)
{
for(int i = 0; i < (int)collections.size(); i++)
{
collections.at(i)->Update(inputObject);
}
}
void EventHandler::AddCollection(EventButtonCollection& collection)
{
collections.push_back(&collection);
}
EventButtonCollection& EventHandler::CreateCollection()
{
EventButtonCollection* temp = new EventButtonCollection;
collections.push_back(temp);
return *temp;
}

View File

@ -0,0 +1,41 @@
//////////////////////////////////////
// Created by Pontus Fransson 2014 //
//////////////////////////////////////
#ifndef MISC_EVENT_HANDLER_H
#define MISC_EVENT_HANDLER_H
#include "../../Input/L_inputClass.h"
#include "EventButtonCollection.h"
#include "EventButton.h"
#include "EventButtonCircle.h"
#include "EventButtonRectangle.h"
#include <vector>
namespace Oyster
{
namespace Event
{
class EventHandler
{
public:
EventHandler();
~EventHandler();
static EventHandler& Instance();
void Update(InputClass* inputObject);
void AddCollection(EventButtonCollection& collection);
EventButtonCollection& CreateCollection();
private:
std::vector<EventButtonCollection*> collections;
};
}
}
#endif

View File

@ -0,0 +1,41 @@
//////////////////////////////////////
// Created by Pontus Fransson 2014 //
//////////////////////////////////////
#ifndef MISC_IEVENT_BUTTON
#define MISC_IEVENT_BUTTON
class InputClass;
namespace Oyster
{
namespace Event
{
enum ButtonState
{
ButtonState_None,
ButtonState_Hover,
ButtonState_Pressed,
ButtonState_Down,
ButtonState_Released,
};
class IEventButton
{
public:
virtual ~IEventButton(){}
virtual void Update(InputClass *input){}
virtual void SendEvent(ButtonState state){}
struct ButtonEvent;
virtual void SetEventFunc(void (*EventFunc)( ButtonEvent e )){}
virtual unsigned int GetID(){ return -1; }
};
}
}
#endif

View File

@ -146,6 +146,8 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="EventHandler\EventButtonCollection.cpp" />
<ClCompile Include="EventHandler\EventHandler.cpp" />
<ClCompile Include="Packing\Packing.cpp" /> <ClCompile Include="Packing\Packing.cpp" />
<ClCompile Include="Resource\Loaders\ByteLoader.cpp" /> <ClCompile Include="Resource\Loaders\ByteLoader.cpp" />
<ClCompile Include="Resource\Loaders\CustomLoader.cpp" /> <ClCompile Include="Resource\Loaders\CustomLoader.cpp" />
@ -164,6 +166,12 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="DynamicArray.h" /> <ClInclude Include="DynamicArray.h" />
<ClInclude Include="EventHandler\EventButton.h" />
<ClInclude Include="EventHandler\EventButtonCircle.h" />
<ClInclude Include="EventHandler\EventButtonCollection.h" />
<ClInclude Include="EventHandler\EventButtonRectangle.h" />
<ClInclude Include="EventHandler\EventHandler.h" />
<ClInclude Include="EventHandler\IEventButton.h" />
<ClInclude Include="GID.h" /> <ClInclude Include="GID.h" />
<ClInclude Include="IQueue.h" /> <ClInclude Include="IQueue.h" />
<ClInclude Include="OysterCallback.h" /> <ClInclude Include="OysterCallback.h" />

View File

@ -51,6 +51,12 @@
<ClCompile Include="Packing\Packing.cpp"> <ClCompile Include="Packing\Packing.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="EventHandler\EventButtonCollection.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="EventHandler\EventHandler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Utilities.h"> <ClInclude Include="Utilities.h">
@ -116,5 +122,23 @@
<ClInclude Include="Packing\Packing.h"> <ClInclude Include="Packing\Packing.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="EventHandler\EventButton.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="EventHandler\EventButtonCollection.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="EventHandler\EventHandler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="EventHandler\IEventButton.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="EventHandler\EventButtonCircle.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="EventHandler\EventButtonRectangle.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>