-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
169 lines (147 loc) · 4.34 KB
/
game.cpp
File metadata and controls
169 lines (147 loc) · 4.34 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "game.h"
#include "surface.h"
#include "MenuManager.h"
#include "Player.h"
#include "Location.h"
#include "LevelManager.h"
#include <iostream>
#include <algorithm>
#include <SDL.h>
namespace Tmpl8
{
// Constructor //
Game::Game()
:background(new Surface("assets/Background/background.png"), 1)
{}
// Set all levels to open, only being able to fire once through menumanager.
void Game::UnLockLevels()
{
for (int i = 0; i < level.numLevels; i++)
{
if (level.GetLevelState(i + 1) == LevelManager::LevelState::Closed)
{ level.SetLevelState(i + 1, LevelManager::LevelState::Open); }
}
unlockAllLevels = true;
}
// Game init
void Game::Init()
{
// Menu //
menu = new MenuManager;
}
// Game shutdown
void Game::Shutdown()
{
// Thanks to MAX in the 3dgep.com discord for sharing this code snippet.
// https://discord.com/channels/515453022097244160/686661689894240277/1095673953734901761
SDL_Event user_event;
user_event.type = SDL_QUIT;
SDL_PushEvent(&user_event);
/////////////////////////////
}
// Main Game Function //
void Game::Tick(float deltaTime)
{
// Multiply deltaTime by 0.001 to get deltaTime in seconds, much easier to work with.
// By using std::min, you prevent long frame pauses messing up the physics.
deltaTime = std::min(deltaTime * 0.001f, 0.05f);
const float screenHeight = static_cast<float>(screen->GetHeight());
// Reset window for new frame
screen->Clear(0);
background.Draw(screen, 0, 0);
// Update menu
menu->Tick(*this, level, player, timer);
switch (state)
{
case MENU:
// Draw Functions //
menu->Draw(screen, *this, level, timer);
break;
case PLAYING:
// Game Logic //
if (menu->GetMenuState() == MenuManager::MenuState::Playing)
{
// Define moving direction from keyboard inputs
if (movingLeft && movingRight || (!movingLeft && !movingRight)) { delta_loc = { 0.0f, 0.0f }; }
else if (movingLeft) { delta_loc = { -1.0f, 0.0f }; }
else if (movingRight) { delta_loc = { 1.0f, 0.0f }; }
player.Move(deltaTime, delta_loc, level, *menu);
// Update Camera Offset
camera.Tick(screen, player.GetLoc(), level.GetCurrentLevel());
// Update Timer
timer.Tick(deltaTime, level, player, *menu);
}
// Update tile animations
if (animations) { level.UpdateAnimations(deltaTime); }
// Draw Functions //
level.DrawLevel(screen, camera.GetOffset());
if (animations && level.GetIsDead()) { player.DeathFX(screen, deltaTime, camera.GetOffset()); }
if (animations && player.GetBounceFX()) { player.BounceFX(screen, deltaTime, camera.GetOffset()); }
player.Draw(screen, camera.GetOffset());
menu->Draw(screen, *this, level, timer);
break;
}
/* ======================== DEBUG ======================== */
// std::cout << "Frametimer: " << deltaTime << std::endl; // Frametimer
// std::cout << "MouseX: " << mousex << " MouseY: " << mousey << std::endl; // Mouse Position
// std::cout << "DeltaLoc: " << delta_loc.x << "," << delta_loc.y << std::endl; // Delta Location
/* ======================================================= */
}
// MOUSE FUNCTIONS //
// Changes mouseDown to false if left mouse button is not being pressed.
void Game::MouseUp(int button)
{
if (button == 1) { Game::mouseDown = false; }
}
// Changes mouseDown to true if left mouse button is being pressed.
void Game::MouseDown(int button)
{
if (button == 1) { Game::mouseDown = true; }
}
// Changes mousex and mousey to (current) absolute mouse position.
void Game::MouseMove(int x, int y) { mousex = x, mousey = y; }
// KEYBOARD FUNCTIONS //
// https://wiki.libsdl.org/SDL2/SDL_Scancode
// Checks if a key is released to stop moving in that direction.
void Game::KeyUp(int key)
{
switch (key)
{
case(SDL_SCANCODE_LEFT):
movingLeft = false;
break;
case(SDL_SCANCODE_A):
movingLeft = false;
break;
case(SDL_SCANCODE_RIGHT):
movingRight = false;
break;
case(SDL_SCANCODE_D):
movingRight = false;
break;
default:
break;
}
}
// Checks if a key is pressed to start moving in that direction.
void Game::KeyDown(int key)
{
switch (key)
{
case(SDL_SCANCODE_LEFT):
movingLeft = true;
break;
case(SDL_SCANCODE_A):
movingLeft = true;
break;
case(SDL_SCANCODE_RIGHT):
movingRight = true;
break;
case(SDL_SCANCODE_D):
movingRight = true;
break;
default:
break;
}
}
};