forked from jose-torrealba/Sticky-Keys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
215 lines (191 loc) · 8.32 KB
/
Copy pathmain.cpp
File metadata and controls
215 lines (191 loc) · 8.32 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* @class StickyKeys
* @authors Jose Torrealba, Cole Chang, Cameron Railton, Adam Salaymeh
* @brief This represents the actual engine of the StickyKeys game
* @details This file/class is a representation of the engine to our StickyKeys game. It is where the player, keyTile and level classes all come together.
* One player object, five level objects and three keyTile objects are all created and continuously updated in this class. The three main functions:
* OnUserCreate, OnUserUpdate & OnUserDestroy are all inherited from the olcPixelGameEngine header file and determine what code gets run on initial
* bootup, continuously throughout the game, and when the window closes, respectively.
*/
#define OLC_PGE_APPLICATION
#define OLC_PGEX_SOUND
#include "olcPixelGameEngine.h"
#include "olcPGEX_Sound.h"
#include "level.h"
#include "player.h"
#include "keyTile.h"
#include <string>
#include <vector>
//Create the actual Game Engine
class StickyKeys : public olc::PixelGameEngine
{
public:
/**
* Default constructor for our GameEngine object
*/
StickyKeys()
{
sAppName = "Sticky Keys Engine";
}
private:
int levelCount;
Level levelOne = Level(64, 16, 16, 16, "/home/jtorreal/CLionProjects/StickyKeys/Levels/level1.txt");
Level levelTwo = Level(64, 16, 16, 16, "/home/jtorreal/CLionProjects/StickyKeys/Levels/level2.txt");
Level levelThree = Level(64, 16, 16, 16, "/home/jtorreal/CLionProjects/StickyKeys/Levels/level3.txt");
Level levelFour = Level(64, 16, 16, 16, "/home/jtorreal/CLionProjects/StickyKeys/Levels/level4.txt");
Level levelFive = Level(64, 16, 16, 16, "/home/jtorreal/CLionProjects/StickyKeys/Levels/level5.txt");
std::vector<Level> levelList;
Level* currentLevel;
Player player = Player(1, 1, 0, 0);
KeyTile leftTile = KeyTile(20, 50, 'l');
KeyTile rightTile = KeyTile(leftTile.getXPos()+16+20, 50, 'r');
KeyTile jumpTile = KeyTile(rightTile.getXPos()+16+20, 50, 'j');
std::string currentMap;
float cameraX = 0.0f;
float cameraY = 0.0f;
int jumpSound;
int backSound;
int deathSound;
int placeKeySound;
int recallSound;
public:
/**
* @brief OnUserCreate
* @details This function runs only once when the window is created. It initializes all of our levels and audio samples to be used later on in the program
* @return true to signify this process has been completed
*/
bool OnUserCreate() override
{
levelCount = 0;
levelList.push_back(levelOne);
levelList.push_back(levelTwo);
levelList.push_back(levelThree);
levelList.push_back(levelFour);
levelList.push_back(levelFive);
//initializing format and sound samples
olc::SOUND::InitialiseAudio(44100, 1, 8, 512);
backSound = olc::SOUND::LoadAudioSample("/home/jtorreal/CLionProjects/StickyKeys/Music/GameBackground.wav");
jumpSound = olc::SOUND::LoadAudioSample("/home/jtorreal/CLionProjects/StickyKeys/Music/Jump_03.wav");
deathSound = olc::SOUND::LoadAudioSample("/home/jtorreal/CLionProjects/StickyKeys/Music/Roblox-death-sound (online-audio-converter.com).wav");
placeKeySound = olc::SOUND::LoadAudioSample("/home/jtorreal/CLionProjects/StickyKeys/Music/mixkit-player-jumping-in-a-video-game-2043.wav");
recallSound = olc::SOUND::LoadAudioSample("/home/jtorreal/CLionProjects/StickyKeys/Music/mixkit-explainer-video-game-alert-sweep-236.wav");
olc::SOUND::PlaySample(backSound, true);
// Called once at the start, so create things here
return true;
}
/**
* @brief OnUserUpdate
* @details Called every frame and deals with all processes that require updating every frame or continuous checks such as drawing the player, processing user input, drawing the level and keyTiles, etc.
* @param fElapsedTime fElapsedTime a time per frame value meant to be included in certain operations to allow equal performance on all machines
* @return true to signify this process has been completed
*/
bool OnUserUpdate(float fElapsedTime) override
{
if(levelCount == 0){
FillRect(0,0,ScreenWidth(), ScreenHeight(), olc::DARK_RED);
DrawString(45, 75, "Sticky Keys", olc::GREEN, 2);
DrawString(55, 130, "Hit 'Enter' to Play", olc::BLACK);
DrawString(55, 150, "Hit 'ESC' to Quit", olc::BLACK);
if(GetKey(olc::ENTER).bPressed){
levelCount++;
}
if(GetKey(olc::ESCAPE).bPressed){
exit(0);
}
}
else if(levelCount == 6){
FillRect(0,0,ScreenWidth(), ScreenHeight(), olc::DARK_RED);
DrawString(55, 120, "THE END!", olc::GREEN, 2);
if(GetKey(olc::ENTER).bPressed || GetKey(olc::ESCAPE).bPressed){
exit(0);
}
}
else {
currentLevel = &levelList.at(levelCount-1);
currentMap = currentLevel->getLevelMap();
SetPixelMode(olc::Pixel::ALPHA);
//Handle Input
if (IsFocused()) {
player.processInput(this, currentLevel, fElapsedTime);
if (GetKey(olc::Key::R).bPressed) {
leftTile.setIsPlaced(false);
leftTile.setIsClicked(false);
rightTile.setIsPlaced(false);
rightTile.setIsClicked(false);
jumpTile.setIsPlaced(false);
jumpTile.setIsClicked(false);
currentLevel->setFixTiles(true);
olc::SOUND::PlaySample(recallSound);
}
if ((GetKey(olc::Key::UP).bPressed) || (GetKey(olc::Key::SPACE).bPressed) ||
(GetKey(olc::Key::W).bPressed) && !jumpTile.isPlaced()) {
olc::SOUND::PlaySample(jumpSound);
}
if (player.getDeathStatus() || player.isAtGoal()) {
leftTile.setIsPlaced(false);
leftTile.setIsClicked(false);
rightTile.setIsPlaced(false);
rightTile.setIsClicked(false);
jumpTile.setIsPlaced(false);
jumpTile.setIsClicked(false);
currentLevel->setFixTiles(true);
}
if(player.getDeathStatus()){
player.death();
olc::SOUND::PlaySample(deathSound);
}
if(player.isAtGoal()){
player.win();
levelCount++;
//play win sound
}
}
//Camera Properties
cameraX = player.getXPos();
cameraY = player.getYPos();
currentLevel->draw(cameraX, cameraY, this);
float offsetX = currentLevel->getOffsetX();
float offsetY = currentLevel->getOffsetY();
//Draw Left Keyboard Key
leftTile.draw(this);
rightTile.draw(this);
jumpTile.draw(this);
//Check to see if mouse clicks on keyboard key
if (IsFocused()) {
if (GetMouse(0).bPressed) {
int mouseClickX = GetMouseX();
int mouseClickY = GetMouseY();
leftTile.processInput(mouseClickX, mouseClickY, &player, currentLevel, offsetX, offsetY);
rightTile.processInput(mouseClickX, mouseClickY, &player, currentLevel, offsetX, offsetY);
jumpTile.processInput(mouseClickX, mouseClickY, &player, currentLevel, offsetX, offsetY);
}
}
//Draw player
player.draw(currentLevel, this);
SetPixelMode(olc::Pixel::NORMAL);
}
return true;
}
/**
* @brief OnUserDestroy
* @details Called once when the window/application is terminated. Allows for any open instances of code such as Audio to be properly shut down
* @return true to signify this process has been completed
*/
bool OnUserDestroy() override
{
olc::SOUND::DestroyAudio();
return true;
}
};
/**
* @brief main
* @details The main function where we create and run our game engine object
* @return 0 to signify that our game has created and ran properly
*/
int main()
{
StickyKeys demo;
if (demo.Construct(256, 256, 8, 8))
demo.Start();
return 0;
}