Skip to content

Commit

Permalink
Added beginnings of a key handler to the controller, as well as funct…
Browse files Browse the repository at this point in the history
…ions to switch weapons. Added jump functionality. Also added gravity functionality, as well as ground pushback to Physics. Added Location field to Object, and Physics to World. World will have a ticker that will cause each object to tick. All movement will be done based on an object's velocity which is based on the object's acceleration.
  • Loading branch information
JLmike7 committed Oct 10, 2014
1 parent f082444 commit 771bc4b
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 17 deletions.
4 changes: 3 additions & 1 deletion SeniorDesignProject/SeniorDesignProject/Biped.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ int Biped::takeHit(int damage){
stats->setHealth(stats->getHealth()-damage);
}
void Biped::jump(){
position->move(Direction::UP, stats->getMaxJumpSpeed());
if (position->isOnGround()){
position->addVelocity(new Point(0.0, stats->getMaxJumpSpeed(), 0.0));
}
}
void Biped::fire(){
//TODO: implement
Expand Down
46 changes: 46 additions & 0 deletions SeniorDesignProject/SeniorDesignProject/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,49 @@ void Controller::fire(){
void Controller::jump(){
biped->jump();
}
void Controller::nextWeapon(){
biped->nextWeapon();
}
void Controller::prevWeapon(){
biped->prevWeapon();
}
void Controller::handleKeyPress(char key){
switch (key){
case 'w':
move(Direction::FRONT);
break;
case 's':
move(Direction::BACK);
break;
case 'a':
move(Direction::LEFT);
break;
case 'd':
move(Direction::RIGHT);
break;
case ' ':
jump();
break;
case '4':
look(Direction::LEFT);
break;
case '6':
look(Direction::RIGHT);
break;
case '8':
look(Direction::UP);
break;
case '2':
look(Direction::DOWN);
break;
case '0':
fire();
break;
case 'q':
prevWeapon();
break;
case 'e':
nextWeapon();
break;
}
}
3 changes: 3 additions & 0 deletions SeniorDesignProject/SeniorDesignProject/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Controller :
void look(Direction direction);
void fire();
void jump();
void nextWeapon();
void prevWeapon();
void handleKeyPress(char key);
private:

protected:
Expand Down
9 changes: 9 additions & 0 deletions SeniorDesignProject/SeniorDesignProject/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ bool Object::getDeath()
void Object::setDeath(bool isDead)
{
Object::isDead = isDead;
}

Position* Object::getPosition()
{
return position;
}
void Object::setPosition(Position *_position)
{
position = position;
}
5 changes: 4 additions & 1 deletion SeniorDesignProject/SeniorDesignProject/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ class Object
bool getDeath();
void setDeath(bool isDead);

Position* getPosition();
void setPosition(Position *position);

Object();
~Object();

private:


protected:

Position* position;
bool isDead;
};

9 changes: 9 additions & 0 deletions SeniorDesignProject/SeniorDesignProject/Physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ float Physics::getWaterChoppiness(){
void Physics::setWaterChoppiness(float newChoppiness){
waterChoppiness = newChoppiness;
}
void Physics::enableGravity(Position* pos){
pos->addAccel(new Point(0, -gravity, 0));
}
void Physics::applyGroundPushback(Position* pos){
//If on the ground, force Y velocity to zero
if (!(pos->isOnGround())){
pos->getVelocity()->setY(0);
}
}
5 changes: 3 additions & 2 deletions SeniorDesignProject/SeniorDesignProject/Physics.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "Position.h"
class Physics
{

Expand All @@ -13,6 +14,8 @@ class Physics
void setGroundHeight(float newHeight);
float getWaterChoppiness();
void setWaterChoppiness(float newChoppiness); //I think this is a word but it looks wrong...oh well.
void enableGravity(Position* pos);
void applyGroundPushback(Position* pos);
Physics();
~Physics();

Expand All @@ -23,7 +26,5 @@ class Physics
double wind;
float groundHeight;
float waterChoppiness;


};

2 changes: 2 additions & 0 deletions SeniorDesignProject/SeniorDesignProject/Point.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Point
void moveX(float x);
void moveY(float y);
void moveZ(float z);
void move(Point* other);
void move(float coords[3]);
float* getCoords();
float distanceTo(Point* other);
private:
Expand Down
36 changes: 25 additions & 11 deletions SeniorDesignProject/SeniorDesignProject/Position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#define RAD2DEG (180.0f/3.14159f)
#define DEG2RAD (3.14159f/180.0f)
#define GRAVITY 9.8


Position::Position()
Expand All @@ -15,20 +16,15 @@ Position::~Position()
}

void Position::Init(){
location = (0, 0, 0);
speed = (0, 0, 0);
accel = (0, 0, 0);
location = new Point(0, 0, 0);
velocity = new Point(0, 0, 0);
accel = new Point(0, 0, 0);
}

//Getters/Settings for Stats.
Point* Position::getLocation(){
return location;
}

void Position::setLocation(Point* point){
location = point;
}

LookDirection* Position::getLook(){
return look;
}
Expand All @@ -42,11 +38,15 @@ void Position::lookAt(Point* point){
}

Point* Position::getVelocity(){
return speed;
return velocity;
}

void Position::addVelocity(Point* newSpeed){
velocity->move(newSpeed);
}

void Position::setVelocity(Point* newSpeed){
speed = newSpeed;
velocity = newSpeed;
}

Point* Position::getAccel(){
Expand All @@ -57,8 +57,12 @@ void Position::setAccel(Point* newAccel){
accel = newAccel;
}

void Position::addAccel(Point* newAccel){
accel->move(newAccel);
}

void Position::teleport(Point* coord){
setLocation(coord);
location = coord;
}

void Position::move(Direction direction,float magnitude){
Expand Down Expand Up @@ -87,5 +91,15 @@ void Position::move(Direction direction,float magnitude){
//Camera shouldn't matter what direction is up/down
location->moveY(magnitude);
}
}
bool Position::isOnGround(){
//TODO: Make this more intricate to account for other landscapes
return (location->getX() == 0);
}

void Position::applyTickMovement(){
//add accel to velocity each tick
velocity->move(accel);
//then add velocity to location each tick
location->move(velocity);
}
7 changes: 5 additions & 2 deletions SeniorDesignProject/SeniorDesignProject/Position.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ class Position
public:
void Init();
Point* getLocation();
void setLocation(Point* location);
LookDirection* getLook();
Point* getVelocity();
void setVelocity(Point* speed);
void addVelocity(Point* speed);
Point* getAccel();
void setAccel(Point* accel);
void addAccel(Point* accel);
void teleport(Point* coord);
void move(Direction direction,float magnitude);
void lookTo(Direction direction,float magnitude);
void lookAt(Point* point);
bool isOnGround();
void applyTickMovement();
Position();
~Position();

Expand All @@ -25,7 +28,7 @@ class Position
protected:
Point* location;
LookDirection* look;
Point* speed;
Point* velocity;
Point* accel;
};

14 changes: 14 additions & 0 deletions SeniorDesignProject/SeniorDesignProject/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ World::~World()
}

void World::Init(){
physics = new Physics();
playerIndex = -1;
for (int team = 0; team < NUMBER_TEAMS; team++){
for (int teamBipedIndex = 0; teamBipedIndex < TEAM_SIZE; teamBipedIndex++){
Expand All @@ -21,10 +22,23 @@ void World::Init(){
setPlayer(i);
}
bipeds[i]->setTeam(team);
physics->enableGravity(bipeds[i]);
}
}
}

void World::tick(){
//Tick all players then all objects
for (int b = 0; b < TEAM_SIZE*NUMBER_TEAMS; b++){
bipeds[b]->getPosition()->applyTickMovement();
}

//TODO: finish object gravity. Ticking 1024 objects per frame is bad.
// for (int o = 0; o < MAX_OBJECTS; o++){
// objects[o]->getPosition()->applyTickMovement();
// }
}

void World::setPlayer(int index)
{
playerIndex = index;
Expand Down
5 changes: 5 additions & 0 deletions SeniorDesignProject/SeniorDesignProject/World.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once
#include "Biped.h"
#include "Team.h"
#include "Physics.h"

#define TEAM_SIZE 5
#define NUMBER_TEAMS 2
#define FRIENDLY_TEAM 0
#define MAX_OBJECTS 1024

class World
{
Expand All @@ -17,6 +19,7 @@ class World
Team* getTeam(int teamIndex, bool aliveOnly);
int getTeamSize(int teamIndex, bool aliveOnly);
Team** getTeams(bool aliveOnly);
void tick();


World();
Expand All @@ -27,4 +30,6 @@ class World
protected:
int playerIndex;
Biped* bipeds[TEAM_SIZE*NUMBER_TEAMS];
Physics* physics;
Object* objects[MAX_OBJECTS];
};
9 changes: 9 additions & 0 deletions SeniorDesignProject/SeniorDesignProject/point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ void Point::moveZ(float _z){
z += _z;
}

void Point::move(Point* other){
move(other->getCoords);
}
void Point::move(float coords[3]){
moveX(coords[0]);
moveY(coords[1]);
moveZ(coords[2]);
}

float Point::getX(){
return x;
}
Expand Down

0 comments on commit 771bc4b

Please sign in to comment.