Contact me to be added to this repository. Push the creations you make for D&D, and they will be displayed in a nice website. See the website for how to contribute.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

60 lines
1.7 KiB

#include "Character.h"
void Character::handleEvent(SDL_Event& e) {
if(e.type == SDL_KEYDOWN && e.key.repeat == 0) {
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;
}
} else if(e.type == SDL_KEYUP && e.key.repeat == 0) {
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;
}
}
}
//update function
void Character::move() {
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);
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);
y_ -= vy_;
}
//camera time
camera_.x = (x_ + sprite_.getWidth()/2) - S_W/2;
camera_.y = (y_ + sprite_.getHeight()/2) - S_H/2;
if(camera_.x < 0)
camera_.x = 0;
if(camera_.y < 0)
camera_.y = 0;
if(camera_.x > L_W*TILE_SIZE - camera_.w)
camera_.x = L_W*TILE_SIZE - camera_.w;
if(camera_.y > L_H*TILE_SIZE - camera_.h)
camera_.y = L_H*TILE_SIZE - camera_.h;
//handle fog of war here
}
void Character::render() {
sprite_.render(x_ - camera_.x, y_ - camera_.y);
}
SDL_Rect Character::getCamera() {
return camera_;
}