-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacter.cpp
95 lines (92 loc) · 2.19 KB
/
Character.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "Character.h"
bool Character::SetAnimation(std::string path, SDL_Renderer *r){
animationPath = path;
if(animationPath == ""){
return false;
}
const char* p = animationPath.c_str();
animation = IMG_LoadAnimation(p);
if(animation == nullptr){
return false;
}
for(int i=0; i<animation->count; i++){
SDL_Texture* tex = SDL_CreateTextureFromSurface(r, animation->frames[i]);
sprites.push_back(tex);
}
return true;
}
void Character::Move(int w, int h){
srand(time(0));
int dir = rand()%4;
switch(dir){
case 0:{
if(pos.first+width<w){
pos.first++;
LoR = true;
}
break;
}
case 1:{
if(pos.second+height<h){
pos.second++;
}
break;
}
case 2:{
if(pos.first>width){
pos.first--;
LoR = false;
}
break;
}
case 3:{
if(pos.second>height){
pos.second--;
}
break;
}
}
return;
}
bool Character::InsideBoundaries(int x, int y){
if(x>pos.first&&
x<pos.first+width&&
y>pos.second&&
y<pos.second+height){
return true;
}
return false;
}
void Character::SetHoldPoint(){
x3 = pos.first;
y3 = pos.second;
}
void Character::Drag(int x1, int y1, int x2, int y2){
pos.first = x2-(x1-x3);
pos.second = y2-(y1-y3);
return;
}
void Character::Animate(int now){
if(now - lastTime>=*animation->delays){
frameIndex = (frameIndex+1)%animation->count;
lastTime = now;
}
}
void Character::Poop(std::vector<Character> &p, SDL_Renderer *r){
Character newPoop;
newPoop.SetAnimation("poop.gif", r);
newPoop.pos = pos;
p.push_back(newPoop);
}
void Character::Display(SDL_Renderer* r){
SDL_Rect rect;
rect.x = pos.first;
rect.y = pos.second;
rect.h = height;
rect.w = width;
SDL_RendererFlip flip = SDL_FLIP_NONE;
if(LoR){
flip = SDL_FLIP_HORIZONTAL;
}
SDL_RenderCopyEx(r, sprites[frameIndex], NULL, &rect, 0, NULL, flip);
}