-
Notifications
You must be signed in to change notification settings - Fork 1
/
Systems.c
95 lines (72 loc) · 2.75 KB
/
Systems.c
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 "Systems.h"
#include "DeltaTime.h"
#include "Math.h"
#include "Input.h"
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
#define PLAYER_SPEED 250.f
#define DEBUG_MODE 1
void velocitySystem(Position *_position, const Velocity *_velocity) {
if(_velocity == NULL) return;
_position->x += _velocity->vx * timer->deltaTime;
_position->y += _velocity->vy * timer->deltaTime;
}
void drawSystem(SDL_Renderer* _renderer, const Position* _position, const Rotation* _rotation, const Sprite* _sprite) {
if(_sprite == NULL) return;
SDL_Rect rect;
rect.x = (int)_position->x;
rect.y = (int)_position->y;
rect.w = _sprite->width;
rect.h = _sprite->height;
SDL_RenderCopyEx(_renderer, _sprite->texture, NULL, &rect, _rotation->angle, NULL, SDL_FLIP_NONE);
#ifdef DEBUG_MODE
if(debugFont == NULL){
debugFont = loadFont(_renderer, "debug.ttf", 16);
}
SDL_SetRenderDrawColor(_renderer, 255, 255, 0, 255);
SDL_RenderDrawRect(_renderer, &rect);
char temp[64];
snprintf(temp, sizeof(temp),"X: %d, Y: %d\n", rect.x, rect.y);
FC_Draw(debugFont, _renderer, (float)rect.x, (float)rect.y - 12, temp);
SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 255);
#endif
}
void collisionSystem(EntityData* _data, const Collider* _collider, const Position* _position, const Dimension* _dimension, Velocity* _velocity) {
if(_collider->type == NO_COLLIDE || _velocity == NULL) return;
for(uint32_t i = 0; i < _data->highestId; ++i){
//Skip if collider is the same.
if(&_data->collider[i] != _collider && _data->collider[i].type != NO_COLLIDE){
Position secondPosition = _data->positions[i];
Dimension secondDimension = _data->dimensions[i];
Position nextPosition;
nextPosition.x = _position->x + (_velocity->vx * timer->deltaTime);
nextPosition.y = _position->y + (_velocity->vy * timer->deltaTime);
if(intersectRect(&nextPosition, _dimension, &secondPosition, &secondDimension)){
_velocity->vx = 0;
_velocity->vy = 0;
return;
}
}
}
}
void inputSystem(EntityData* _data, const InputListener* _input, Velocity* _velocity){
if(!_input->listens) return;
if(input.keyPressed == SDLK_w){
_velocity->vy = -PLAYER_SPEED;
}
if(input.keyPressed == SDLK_d){
_velocity->vx = PLAYER_SPEED;
}
if(input.keyPressed == SDLK_s){
_velocity->vy = PLAYER_SPEED;
}
if(input.keyPressed == SDLK_a){
_velocity->vx = -PLAYER_SPEED;
}
if(input.keyReleased == SDLK_a || input.keyReleased == SDLK_d){
_velocity->vx = 0;
}
if(input.keyReleased == SDLK_w || input.keyReleased == SDLK_s){
_velocity->vy = 0;
}
}