Browse Source

Add multiple character support

master
Josh Gordon 4 years ago
parent
commit
2d30343dea
  1. 38
      app/action/Character.cpp
  2. 43
      app/action/Character.h
  3. 2
      app/action/Map.h
  4. BIN
      app/action/action
  5. BIN
      app/action/assets/Cave.png
  6. BIN
      app/action/assets/jj.png
  7. 0
      app/action/assets/sp.png
  8. 74
      app/action/main.cpp

38
app/action/Character.cpp

@ -8,29 +8,51 @@ void Character::handleEvent(SDL_Event& e) {
case SDLK_LEFT: vx_ -= CHAR_SPEED; break;
case SDLK_RIGHT: vx_ += CHAR_SPEED; break;
}
if(turnTimer.isPaused()) {
printf("Unpause\n");
turnTimer.unpause();
}
} else if(e.type == SDL_KEYUP && e.key.repeat == 0) {
if(vy_ == 0 && vx_ == 0) //this is what's called a pro gamer move
return;
switch(e.key.keysym.sym) {
case SDLK_UP: vy_ += CHAR_SPEED; break;
case SDLK_DOWN: vy_ -= CHAR_SPEED; break;
case SDLK_LEFT: vx_ += CHAR_SPEED; break;
case SDLK_RIGHT: vx_ -= CHAR_SPEED; break;
}
if(!turnTimer.isPaused() && vy_ == 0 && vx_ == 0) {
printf("Pause\n");
turnTimer.pause();
}
}
}
//update function
void Character::move() {
printf("Moving char at (%d,%d)\n",x_/TILE_SIZE,y_/TILE_SIZE);
//x_ and y_ are in pixel coords, not tile coords
void Character::move(Quad* level) {
//printf("Turn time is %d\n",turnTimer.getTicks());
//printf("Moving char at (%d,%d)\n",x_/TILE_SIZE,y_/TILE_SIZE);
x_ += vx_;
if((x_ < 0) || (x_ + sprite_.getWidth() > L_W*TILE_SIZE)) {
printf("%d is too far on x, bounds are 0 and %d\n",x_,L_W*TILE_SIZE);
if((x_ < 0)
|| (x_ + sprite_.getWidth() > L_W*TILE_SIZE)
|| (level->lookup((x_)/TILE_SIZE,(y_)/TILE_SIZE) == WALL)
|| (level->lookup((x_ + sprite_.getWidth())/TILE_SIZE,(y_)/TILE_SIZE) == WALL)//) {
|| (level->lookup((x_)/TILE_SIZE,(y_ + sprite_.getHeight())/TILE_SIZE) == WALL)
|| (level->lookup((x_ + sprite_.getWidth())/TILE_SIZE,(y_ + sprite_.getHeight())/TILE_SIZE) == WALL)) {
//printf("%d is too far on x, bounds are 0 and %d\n",x_,L_W*TILE_SIZE);
x_ -= vx_;
}
y_ += vy_;
if((y_ < 0) || (y_ + sprite_.getHeight() > L_H*TILE_SIZE)) {
printf("%d is too far on y, bounds are 0 and %d\n",y_,L_H*TILE_SIZE);
if((y_ < 0)
|| (y_ + sprite_.getHeight() > L_H*TILE_SIZE)
|| (level->lookup((x_)/TILE_SIZE,(y_)/TILE_SIZE) == WALL)
|| (level->lookup((x_ + sprite_.getWidth())/TILE_SIZE,(y_)/TILE_SIZE) == WALL)
|| (level->lookup((x_)/TILE_SIZE,(y_ + sprite_.getHeight())/TILE_SIZE) == WALL)//){
|| (level->lookup((x_ + sprite_.getWidth())/TILE_SIZE,(y_ + sprite_.getHeight())/TILE_SIZE) == WALL)) {
//printf("%d is too far on y, bounds are 0 and %d\n",y_,L_H*TILE_SIZE);
y_ -= vy_;
}
@ -51,8 +73,8 @@ void Character::move() {
//handle fog of war here
}
void Character::render() {
sprite_.render(x_ - camera_.x, y_ - camera_.y);
void Character::render(SDL_Rect camera) {
sprite_.render(x_ - camera.x, y_ - camera.y);
}
SDL_Rect Character::getCamera() {

43
app/action/Character.h

@ -3,6 +3,8 @@
#include <string>
#include "Texture.h"
#include "Timer.h"
#include "Map.h"
extern const int S_W; //screen dimensions
extern const int S_H;
@ -11,30 +13,63 @@ extern int L_W; //level dimensions
extern int L_H;
extern const int TILE_SIZE;
extern const int MAX_FPS;
extern const int TICKS_PER_FRAME;
class Character {
private:
int name;
int x_,y_,vx_,vy_; //position and velocity. We dont need no second derivatives.
Texture sprite_;
SDL_Rect camera_;
Uint32 speed;
Timer turnTimer;
public:
static const int CHAR_SPEED = 250;
Character(std::string filepath, int x, int y): x_(x), y_(y), vx_(0), vy_(0), camera_({0,0,S_W,S_H}) {
static const int CHAR_SPEED = 10;
Character(){};
Character(int id,std::string filepath, int x, int y): name(id), x_(x), y_(y), vx_(0), vy_(0), camera_({0,0,S_W,S_H}), speed(30) {
sprite_.loadFromFile(filepath);
turnTimer.start();
turnTimer.pause();
};
~Character() {
printf("I am being deallocated\n");
sprite_.free();
};
void handleEvent(SDL_Event& e);
void move();
void move(Quad*);
void render(SDL_Rect);
void render();
bool isTurn() { return turnTimer.getTicks() < 10*speed*MAX_FPS/TICKS_PER_FRAME; };
void resetTurn() {
printf("My (%d) turn is ending at {%d,%d,%d,%d}\n",name,camera_.x,camera_.y,camera_.w,camera_.h);
turnTimer.stop();
vx_ = vy_ = 0;
}
void startTurn() {
printf("My (%d) turn is starting at {%d,%d,%d,%d} with velocity (%d,%d)\n",name,camera_.x,camera_.y,camera_.w,camera_.h,vx_,vy_);
vx_ = vy_ = 0;
turnTimer.start();
turnTimer.pause();
}
SDL_Rect getCamera();
Texture getSprite(){return sprite_;}
};
struct InitiativeNode {
Character* c;
int init;
};

2
app/action/Map.h

@ -69,4 +69,6 @@ public:
int getW(){ return numTilesX; }
int getH(){ return numTilesY; }
Quad* getTiles(){ return tiles; }
};

BIN
app/action/action

BIN
app/action/assets/Cave.png

Before

Width: 1920  |  Height: 1080  |  Size: 113 KiB

After

Width: 1920  |  Height: 1080  |  Size: 47 KiB

BIN
app/action/assets/jj.png

After

Width: 64  |  Height: 64  |  Size: 13 KiB

0
app/action/sp.png → app/action/assets/sp.png

Before

Width: 64  |  Height: 64  |  Size: 14 KiB

After

Width: 64  |  Height: 64  |  Size: 14 KiB

74
app/action/main.cpp

@ -1,4 +1,5 @@
#include <stdio.h>
#include <list>
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
@ -32,44 +33,29 @@ Timer fpsTimer;
Timer capTimer;
int frames_total;
std::list<Character> characters;
int main(int argc, char** argv) {
init();
/*
Quad* tree = new Quad();
int size = 8;
tree->init(size,size);
for(int x = 0; x < size; ++x) {
for(int y = 0; y < size; ++y) {
tree->insert(x,y,static_cast<Tile>((x+y)/2));
}
}
for(int x = 0; x < size; ++x) {
for(int y = 0; y < size; ++y) {
printf("(%d,%d), expected %d and tile is %d\n",x,y,(x+y)/2,tree->lookup(x,y));
}
}
//tree->print(0);
tree->free();
//close();
//exit(1);
*/
Map map;
if(argc > 1) {
std::string s = argv[2];
map.loadFromFile(s);
} else {
map.loadFromFile("assets/Cave.png");
}
map.loadFromFile("assets/Cave.png");
L_W = map.getW();
L_H = map.getH();
//map.print();
//exit(1);
printf("Width: %d, Height: %d\n",L_W,L_H);
Character ch("sp.png",0,0);
Character ch(1,"assets/sp.png",0,0);
characters.push_back(ch);
printf("Constructing char 2\n");
Character ch2(2,"assets/jj.png",128,0);
printf("Pushing back char 2\n");
characters.push_back(ch2);
printf("Char 2 included! \n");
auto active = characters.begin();
SDL_Event e;
bool done = false;
@ -90,19 +76,39 @@ int main(int argc, char** argv) {
break;
}
}
ch.handleEvent(e);
active->handleEvent(e);
}
ch.move();
active->move(map.getTiles());
SDL_RenderClear(renderer);
map.render(ch.getCamera());
ch.render();
map.render(active->getCamera());
active->render(active->getCamera());
for(auto itr = characters.begin(); itr != characters.end(); ++itr) {
if(itr != active) {
itr->render(active->getCamera());
}
}
SDL_RenderPresent(renderer);
float avgFPS = frames_total / (fpsTimer.getTicks() / 1000.0f);
if(!active->isTurn()) {
active->resetTurn();
active++;
if(active == characters.end()){
printf("Back to beginning\n");
active = characters.begin();
}
else{
printf("Next character\n");
}
active->startTurn();
printf("Next turn!\n");
}
//float avgFPS = frames_total / (fpsTimer.getTicks() / 1000.0f);
//printf("FPS: %f\n",avgFPS);
++frames_total;

Loading…
Cancel
Save