Show Player.cpp syntax highlighted
/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG *
*Copyright (C) 2006, Tony Sparks *
* *
*This library is free software; you can redistribute it and/or *
*modify it under the terms of the GNU Lesser General Public *
*License as published by the Free Software Foundation; either *
*version 2.1 of the License, or (at your option) any later version. *
* *
*This library is distributed in the hope that it will be useful, *
*but WITHOUT ANY WARRANTY; without even the implied warranty of *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
*Lesser General Public License for more details. *
* *
*You should have received a copy of the GNU Lesser General Public *
*License along with this library; if not, write to the Free Software *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
**************************************************************************************
*/
#include "StdAfx.h"
#include "Player.h"
#include <iostream>
#include "PlayerController.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 ) )
/*------------------------------------------------------------
Player Class
-------------------------------------------------------------*/
#define TIME g_kernel->GetTime()
Player::Player(void) : IEntity()//, IEventListener()
{
// button pressed
buttons.btnPressed = 0;
buttons.btnReleased = 0;
buttons.buttons = 0;
// next update
m_nextUpdate = 0.0f;
m_position.Set( 400, 300);
m_baseVelocity.Set( 180, 180 );
m_velocity.Set(0,0);
m_world.Set( 400, 300 );
// register the config
m_config.Register();
// -----------add states-------------------
// NOTE: States will be changed by the
// PlayerController, the Player class acts
// as a holder and executer of the current state
// the controller will read input by the user, and
// determine which state to make the player
// ----------------------------------------
m_entCtrl.SetEntity( this );
m_FSM.SetOwner( this );
SetFace(SOUTH);
}
/**
=============================
Update the current state
=============================
*/
void Player::Update(long dt)
{
IEntity::Update( dt );
//m_nextUpdate = TIME + 10;
//// update the current state
//m_FSM.Update();
//CheckKeys(); // update the input
// CheckFace();
// // TODO - Trampped Being Carried -
// // player is walking or idle, and attacks
// if ( GetState() == ENT_STATES[IDLE] || GetState() == ENT_STATES[WALKING] )
// {
// if ( buttons.buttons & BUTTON_ATTACK1 )
// {
// m_nextUpdate = TIME + 450;
// SetState( ENT_STATES[ATTACKING1] );
// return;
// }
// }
// // player is carrying an item and throws it
// if ( GetState() == ENT_STATES[CARRYING] || GetState() == ENT_STATES[CARRYING_WALKING] )
// {
// if ( buttons.buttons & (BUTTON_ATTACK1|BUTTON_USE) )
// {
// m_nextUpdate = TIME + 200;
// Throw();
// return;
// }
// }
//// else if no movement, set to idle state
// if ( m_velocity.Length() == 0 ) {
// if ( GetState() == ENT_STATES[CARRYING] || GetState()==ENT_STATES[CARRYING_WALKING] )
// SetState( ENT_STATES[CARRYING] );
// else
// SetState( ENT_STATES[IDLE] );
// } else {
// if ( GetState() == ENT_STATES[CARRYING] || GetState()==ENT_STATES[CARRYING_WALKING] )
// SetState( ENT_STATES[CARRYING_WALKING] );
// else
// SetState( ENT_STATES[WALKING] );
// }
}
/** Damage this entity */
int Player::Damage( int hit )
{
// old health
int old = IEntity::Damage( hit );
// return old health
return old;
}
void Player::Trigger()
{
// do nothing
}
/** Collide with another entity */
bool Player::CollideWith( IEntity* ent )
{
//
// TODO Make this a lot cleaner
//
if ( GetState() == ENT_STATES[ATTACKING1] ) {
ent->Damage( -100 );
}
else if ( buttons.buttons & BUTTON_USE )
{
if ( ent->m_attributes.Has( "dialog" ) )
{
ent->SetFace( (GetFace() + 2) % 4);
ent->SetState( ENT_STATES[TALKING] );
}
else if ( ent->m_attributes.Has( "liftable" ) )
{
if ( !CarryingEntity() )
{
Carry( ent );
}
}
}
return false;
}
/** Pick Up an Item */
void Player::PickUpItem(IEntity* ent)
{
IEntity::PickUpItem( ent );
}
Player::~Player(void)
{
// IEventListener::~IEventListener();
m_config.Save();
}
/*-----------------------------------------
IEventListener Interface
------------------------------------------*/
/** Handle an event */
//bool Player::HandleEvent( IEvent* e )
//{
// switch( e->GetType() )
// {
// // player is moving
// case EVT_MOVE:
// buttons_t* b = GetButtons();
// b->buttons = e->GetData<PlayerMovement>()->buttons;
// break;
// };
// return true;
//}
See more files for this project here