Skip to content

Commit

Permalink
Follow !
Browse files Browse the repository at this point in the history
  • Loading branch information
nbrignol committed Apr 14, 2014
1 parent 530aa78 commit fd9dcf4
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Actor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <iostream>
#include "Actor.h"
#include "KeyboardBehavior.h"
#include "PointerClickBehavior.h"

Actor::Actor(){
this->position = Vector();
Expand Down
4 changes: 2 additions & 2 deletions src/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "InputManager.h"
#include "Vector.h"

class KeyboardBehavior;
class PointerClickBehavior;
class Actor {
public:
Actor();
Expand All @@ -14,7 +14,7 @@ class Actor {
int width;
int height;

KeyboardBehavior * behavior;
PointerClickBehavior * behavior;

std::string name;

Expand Down
117 changes: 117 additions & 0 deletions src/PointerClickBehavior.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include "PointerClickBehavior.h"
#include <iostream>

PointerClickBehavior::PointerClickBehavior(Actor * actor){

this->actor = actor;
this->currentVelocity = Vector();
this->maxVelocity = Vector();
this->acceleration = Vector();

this->destination = Vector();

}

void PointerClickBehavior::checkMove(bool moveWay1, bool moveWay2, float &currentVelocity, float &startVelocity, float &acceleration, float &way){

bool move = moveWay1 || moveWay2;

if (currentVelocity == 0 && ! move ) {
return;
}

if (currentVelocity == 0 && move) {
currentVelocity = startVelocity;
acceleration = 1.3;
return;
}

if (currentVelocity != 0 && !move) {
acceleration = 0.8;
return;
}


if (moveWay1) {
way = -1;
}

if (moveWay2) {
way = 1;
}

if (moveWay1 && moveWay2) {
currentVelocity = 0;
}

}

void PointerClickBehavior::doMove(float elapsedMs, float &acceleration, float &currentVelocity, float &startVelocity, float &maxVelocity, float &position, float &way, float &maxMove) {

std::cout << "Acceleration : " << acceleration << "" << std::endl;

//acceleration
currentVelocity *= acceleration;

//bounds
if (currentVelocity < startVelocity) {
currentVelocity = 0;
way = 1;
}

if (currentVelocity > maxVelocity) {
currentVelocity = maxVelocity;
}

float elapsedSec = elapsedMs / 1000;
float move = currentVelocity * elapsedSec ;

if (move > maxMove) {
move = maxMove;
}

move *= way;

std::cout << "Velocity : " << currentVelocity << " px/s" << std::endl;
std::cout << "Elapsed : " << elapsedSec << " s" << std::endl;
std::cout << "Move : " << move << " px" << std::endl;

position = position + move;
}

void PointerClickBehavior::move(float elapsedMs) {

if (this->inputManager->isPressed(InputManager::INPUT_POINTER_MAIN_BUTTON)) {
this->destination.x = this->inputManager->pointer.x;
this->destination.y = this->inputManager->pointer.y;

std::cout << "Destination : x:" << this->destination.x << "/ y:" << this->destination.y << std::endl;
} else {
return;
}

bool left, right, up, down = false;

if (this->destination.x > this->actor->position.x) {
right = true;
} else if (this->destination.x < this->actor->position.x) {
left = true;
}

if (this->destination.y > this->actor->position.y) {
down = true;
} else if (this->destination.y < this->actor->position.y) {
up = true;
}


this->checkMove(left, right, this->currentVelocity.x, this->startVelocity.x, this->acceleration.x, this->way.x);
this->checkMove(up, down, this->currentVelocity.y, this->startVelocity.y, this->acceleration.y, this->way.y);

float maxMoveX = abs(this->destination.x - this->actor->position.x);
float maxMoveY = abs(this->destination.y - this->actor->position.y);

this->doMove(elapsedMs, this->acceleration.x, this->currentVelocity.x, this->startVelocity.x, this->maxVelocity.x, this->actor->position.x, this->way.x, maxMoveX);
this->doMove(elapsedMs, this->acceleration.y, this->currentVelocity.y, this->startVelocity.y, this->maxVelocity.y, this->actor->position.y, this->way.y, maxMoveY);

}
32 changes: 32 additions & 0 deletions src/PointerClickBehavior.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef POINTERCLICKBEHAVIORH
#define POINTERCLICKBEHAVIORH

#include <string>
#include "InputManager.h"
#include "Vector.h"
#include "Actor.h"

class PointerClickBehavior {
protected:

Vector destination;

void checkMove(bool moveWay1, bool moveWay2, float &currentVelocity, float &startVelocity, float &acceleration, float &way);
void doMove(float elapsedMs, float &acceleration, float &currentVelocity, float &startVelocity, float &maxVelocity, float &position, float &way, float &maxMove);
public:
PointerClickBehavior(Actor * actor);
~PointerClickBehavior();

Actor * actor;

Vector way;
Vector currentVelocity;
Vector startVelocity;
Vector maxVelocity;
Vector acceleration;

InputManager * inputManager;

void move(float elapsedMs);
};
#endif
23 changes: 12 additions & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "InputManager.h"
#include "MainController.h"
#include "KeyboardBehavior.h"
#include "PointerClickBehavior.h"

const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
Expand All @@ -25,22 +26,22 @@ int main( int argc, char* args[] )
hero->position.x = (WINDOW_WIDTH/2) - hero->width/2;
hero->position.y = (WINDOW_HEIGHT/2) - hero->height/2;

KeyboardBehavior * keyboardBehavior = new KeyboardBehavior(hero);
hero->behavior = keyboardBehavior;
PointerClickBehavior * behavior = new PointerClickBehavior(hero);
hero->behavior = behavior;

keyboardBehavior->currentVelocity.x = 0;
keyboardBehavior->currentVelocity.y = 0;
behavior->currentVelocity.x = 0;
behavior->currentVelocity.y = 0;

keyboardBehavior->startVelocity.x = 20;
keyboardBehavior->startVelocity.y = 20;
behavior->startVelocity.x = 20;
behavior->startVelocity.y = 20;

keyboardBehavior->maxVelocity.x = 300;
keyboardBehavior->maxVelocity.y = 300;
behavior->maxVelocity.x = 300;
behavior->maxVelocity.y = 300;

keyboardBehavior->acceleration.x = 0;
keyboardBehavior->acceleration.y = 0;
behavior->acceleration.x = 0;
behavior->acceleration.y = 0;

keyboardBehavior->inputManager = inputManager;
behavior->inputManager = inputManager;



Expand Down

0 comments on commit fd9dcf4

Please sign in to comment.