-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModulePlayer.cpp
More file actions
122 lines (96 loc) · 2.5 KB
/
ModulePlayer.cpp
File metadata and controls
122 lines (96 loc) · 2.5 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "Globals.h"
#include "Application.h"
#include "ModulePlayer.h"
// Reference at https://www.youtube.com/watch?v=OEhmUuehGOA
ModulePlayer::ModulePlayer(Application* app, bool start_enabled) : Module(app, start_enabled)
{
graphics = NULL;
collider = NULL;
current_animation = NULL;
exploding = false;
// idle animation (just the ship)
idle.frames.PushBack({66, 1, 32, 14});
// move upwards
up.frames.PushBack({100, 1, 32, 14});
up.frames.PushBack({132, 0, 32, 14});
up.loop = false;
up.speed = 0.1f;
// Move down
down.frames.PushBack({33, 1, 32, 14});
down.frames.PushBack({0, 1, 32, 14});
down.loop = false;
down.speed = 0.1f;
}
ModulePlayer::~ModulePlayer()
{}
// Load assets
bool ModulePlayer::Start()
{
LOG("Loading player");
graphics = App->textures->Load("rtype/ship.png");
position.x = 150;
position.y = 120;
collider = App->collision->AddCollider({position.x, position.y, 32, 14}, COLLIDER_PLAYER, this);
exploding = false;
return true;
}
// Unload assets
bool ModulePlayer::CleanUp()
{
LOG("Unloading player");
App->textures->Unload(graphics);
return true;
}
// Update: draw background
update_status ModulePlayer::Update()
{
if(exploding == true)
return UPDATE_CONTINUE;
int speed = 1;
if(App->input->GetKey(SDL_SCANCODE_A) == KEY_REPEAT)
{
position.x -= speed;
}
if(App->input->GetKey(SDL_SCANCODE_D) == KEY_REPEAT)
{
position.x += speed;
}
if(App->input->GetKey(SDL_SCANCODE_S) == KEY_REPEAT)
{
position.y += speed;
if(current_animation != &down)
{
down.Reset();
current_animation = &down;
}
}
if(App->input->GetKey(SDL_SCANCODE_W) == KEY_REPEAT)
{
position.y -= speed;
if(current_animation != &up)
{
up.Reset();
current_animation = &up;
}
}
if(App->input->GetKey(SDL_SCANCODE_SPACE) == KEY_UP)
{
App->particles->AddParticle(App->particles->laser, position.x + 28, position.y, COLLIDER_PLAYER_SHOT);
}
if(App->input->GetKey(SDL_SCANCODE_S) == KEY_IDLE && App->input->GetKey(SDL_SCANCODE_W) == KEY_IDLE)
current_animation = &idle;
collider->SetPos(position.x, position.y);
// Draw everything --------------------------------------
App->renderer->Blit(graphics, position.x, position.y, &(current_animation->GetCurrentFrame()));
return UPDATE_CONTINUE;
}
// Collision detection
void ModulePlayer::OnCollision(Collider* c1, Collider* c2)
{
if(exploding == false)
{
App->fade->FadeToBlack(App->scene_space, App->scene_intro);
exploding = true;
App->particles->AddParticle(App->particles->explosion, position.x, position.y);
}
}