-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModulePlayer.cpp
More file actions
132 lines (104 loc) · 3.12 KB
/
ModulePlayer.cpp
File metadata and controls
132 lines (104 loc) · 3.12 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
123
124
125
126
127
128
129
130
131
132
#include "Globals.h"
#include "Application.h"
#include "ModuleTextures.h"
#include "ModuleInput.h"
#include "ModuleParticles.h"
#include "ModuleRender.h"
#include "ModuleCollision.h"
#include "ModuleFadeToBlack.h"
#include "ModulePlayer.h"
// Reference at https://www.youtube.com/watch?v=OEhmUuehGOA
ModulePlayer::ModulePlayer()
{
// idle animation (just the ship)
idle.PushBack({66, 1, 32, 14});
// move upwards
up.PushBack({100, 1, 32, 14});
up.PushBack({132, 0, 32, 14});
up.loop = false;
up.speed = 0.1f;
// Move down
down.PushBack({33, 1, 32, 14});
down.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");
destroyed = false;
position.x = 150;
position.y = 120;
col = App->collision->AddCollider({position.x, position.y, 32, 16}, COLLIDER_PLAYER, this);
return true;
}
// Unload assets
bool ModulePlayer::CleanUp()
{
LOG("Unloading player");
App->textures->Unload(graphics);
if (col != nullptr)
col->to_delete = true;
return true;
}
// Update: draw background
update_status ModulePlayer::Update()
{
position.x += 1; // Automatic movement
int speed = 1;
if(App->input->keyboard[SDL_SCANCODE_A] == KEY_STATE::KEY_REPEAT)
{
position.x -= speed;
}
if(App->input->keyboard[SDL_SCANCODE_D] == KEY_STATE::KEY_REPEAT)
{
position.x += speed;
}
if(App->input->keyboard[SDL_SCANCODE_S] == KEY_STATE::KEY_REPEAT)
{
position.y += speed;
if(current_animation != &down)
{
down.Reset();
current_animation = &down;
}
}
if(App->input->keyboard[SDL_SCANCODE_W] == KEY_STATE::KEY_REPEAT)
{
position.y -= speed;
if(current_animation != &up)
{
up.Reset();
current_animation = &up;
}
}
if(App->input->keyboard[SDL_SCANCODE_SPACE] == KEY_STATE::KEY_DOWN)
{
App->particles->AddParticle(App->particles->laser, position.x + 20, position.y, COLLIDER_PLAYER_SHOT);
}
if(App->input->keyboard[SDL_SCANCODE_S] == KEY_STATE::KEY_IDLE
&& App->input->keyboard[SDL_SCANCODE_W] == KEY_STATE::KEY_IDLE)
current_animation = &idle;
col->SetPos(position.x, position.y);
// Draw everything --------------------------------------
if(destroyed == false)
App->render->Blit(graphics, position.x, position.y, &(current_animation->GetCurrentFrame()));
return UPDATE_CONTINUE;
}
void ModulePlayer::OnCollision(Collider* c1, Collider* c2)
{
if(c1 == col && destroyed == false && App->fade->IsFading() == false)
{
App->fade->FadeToBlack((Module*)App->scene_space, (Module*)App->scene_intro);
App->particles->AddParticle(App->particles->explosion, position.x, position.y, COLLIDER_NONE, 150);
App->particles->AddParticle(App->particles->explosion, position.x + 8, position.y + 11, COLLIDER_NONE, 220);
App->particles->AddParticle(App->particles->explosion, position.x - 7, position.y + 12, COLLIDER_NONE, 670);
App->particles->AddParticle(App->particles->explosion, position.x + 5, position.y - 5, COLLIDER_NONE, 480);
App->particles->AddParticle(App->particles->explosion, position.x - 4, position.y - 4, COLLIDER_NONE, 350);
destroyed = true;
}
}