Show PlayerController.cpp syntax highlighted
#include "StdAfx.h"
#include "PlayerController.h"
#include "EntityEvents.h"
#include "../kernel.h"
extern Kernel* g_kernel;
/*-------------------------------------------------------------
Define shortcuts
--------------------------------------------------------------*/
#define JOYSTICK_XAXIS ( g_kernel->GetInput()->GetJoystickX() )
#define JOYSTICK_YAXIS ( g_kernel->GetInput()->GetJoystickY() )
#define JOYSTICK_BUTTON( str ) ( g_kernel->GetInput()->IsJoystickButtonDown( g_kernel->GetCvars()->GetFloatValue( str ) ) )
#define KEY_BUTTON( str ) ( g_kernel->GetInput()->IsKeyDown( g_kernel->GetCvars()->GetFloatValue( str ) ) )
#define KEY_BUTTONSTAMP( str ) ( g_kernel->GetInput()->IsKeyDown( g_kernel->GetCvars()->GetFloatValue( str ), true ) )
#define TIME (g_kernel->GetTime())
#include <math.h>
PlayerController::PlayerController(void)
{
m_oldButtons = 0;
}
PlayerController::~PlayerController(void)
{
}
/** Gather input, and send events */
void PlayerController::GatherEvents()
{
int buttons = 0;
// TEMP - Testing joystick module
if ( g_kernel->GetInput()->JoystickEnabled() )
{
if ( JOYSTICK_XAXIS > 15000 )
buttons |= BUTTON_RIGHT;
else if ( JOYSTICK_XAXIS < -20000 )
buttons |= BUTTON_LEFT;
if ( JOYSTICK_YAXIS > 15000 )
buttons |= BUTTON_DOWN;
else if ( JOYSTICK_YAXIS < -20000 )
buttons |= BUTTON_UP;
if ( JOYSTICK_BUTTON( "+j_attack1" ) ) {
buttons |= BUTTON_ATTACK1;
}
if ( JOYSTICK_BUTTON( "+j_use" ) ) {
buttons |= BUTTON_USE;
}
}
// check each button for activation...
if ( KEY_BUTTONSTAMP( "+up" ) ) {
buttons |= BUTTON_UP;
} else if ( KEY_BUTTONSTAMP( "+down" ) ) {
buttons |= BUTTON_DOWN;
}
if ( KEY_BUTTONSTAMP( "+left" ) ) {
buttons |= BUTTON_LEFT;
} else if ( KEY_BUTTONSTAMP( "+right" ) ) {
buttons |= BUTTON_RIGHT;
}
if ( KEY_BUTTON( "+attack1" ) ) {
buttons |= BUTTON_ATTACK1;
}
if ( KEY_BUTTON( "+use" ) ) {
buttons |= BUTTON_USE;
}
// TODO - dispatch different messages for
// different states
// dispatch the message
if ( m_oldButtons != buttons )
{
//g_kernel->GetConsole()->Print(" Dispatching Movement event %d vs %d", buttons, m_oldButtons );
IEvent evnt( EVT_ENTITY, TIME, new EntityEvents(1, buttons), LT_ENTITY );
// Trigger this event NOW since the player updates every 1/100 of a second
EventManager::GetInstance()->TriggerEvent( evnt );
m_oldButtons = buttons;
}
}
void entmove::Update(long dt)
{
static float nextTime = 0;
static int buttons = 0;
static bool sw = false;
static bool nw = false;
static bool m = false;
if (TIME > nextTime)
{ sw = !sw; nw = !nw;
m = rand() % 2 ? true : false;
if ( !sw ) {
buttons |= BUTTON_RIGHT;
buttons &= ~BUTTON_LEFT;
}
if ( sw ) {
buttons |= BUTTON_LEFT;
buttons &= ~BUTTON_RIGHT;
}
if( m ) {
if ( nw ) {
buttons |= BUTTON_DOWN;
buttons &= ~BUTTON_UP;
}
if ( !nw ) {
buttons |= BUTTON_UP;
buttons &= ~BUTTON_DOWN;
}}
IEvent evnt( EVT_ENTITY, TIME, new EntityEvents(4, buttons), LT_ENTITY );
// Trigger this event NOW since the player updates every 1/100 of a second
EventManager::GetInstance()->TriggerEvent( evnt );
nextTime = TIME + 700;
}
}
See more files for this project here