-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayMode.hpp
101 lines (82 loc) · 2.63 KB
/
PlayMode.hpp
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
#include "PPU466.hpp"
#include "Mode.hpp"
#include <glm/glm.hpp>
#include <vector>
#include <deque>
#include <random> // for std::mt19937
#include <chrono> // for std::chrono
#include <iostream>
// gravity and button acceleration values
constexpr float GravAccel = -100.0f;
constexpr float ThrusterAccel = 200.0f;
constexpr float RCSAccel = 100.0f;
// barrier struct
struct Barrier {
// place at which barrier is located
uint8_t x=0;
// bottom of opening
uint8_t y=0;
// height of opening
uint8_t w=0;
};
// obstacle course formed by multiple barriers
struct Course {
// list of barriers
std::vector<Barrier> barriers;
// construct barriers, specified by tile coordinate
Course(const uint8_t num_barriers=4) {
// clear barriers
barriers.clear();
// seed for random generator
// random generator code from https://www.learncpp.com/cpp-tutorial/generating-random-numbers-using-mersenne-twister/
std::mt19937 mt{ static_cast<unsigned int>(
std::chrono::steady_clock::now().time_since_epoch().count()
) };
// leave room for 4 tiles at left and right of screen to allow maneuver space
uint8_t course_start = 0;
uint8_t course_end = PPU466::BackgroundHeight / 2;
for (uint8_t i = 0; i < num_barriers; i++) {
// generate random barrier
Barrier b;
// generate x coordinates
b.y = course_start + (course_end - course_start) / (num_barriers + 1) * (i + 1) + 2;
// generate random y coordinates, leaving room for max 8 tile opening
b.x = mt() % ((PPU466::BackgroundWidth / 2) - 8);
// generate random height, leaving room for min 4 and max 6 tile opening
b.w = (mt() % 4) + 4;
// add barrier to course
barriers.push_back(b);
}
};
};
struct PlayMode : Mode {
PlayMode();
virtual ~PlayMode();
//functions called by main loop:
virtual bool handle_event(SDL_Event const &, glm::uvec2 const &window_size) override;
virtual void update(float elapsed) override;
virtual void draw(glm::uvec2 const &drawable_size) override;
//----- game state -----
// default background
std::array<uint16_t, PPU466::BackgroundWidth * PPU466::BackgroundHeight> default_bg;
// obstacle course
Course course;
//input tracking:
struct Button {
uint8_t downs = 0;
uint8_t pressed = 0;
} left, right, down, up;
//player position:
glm::vec2 player_at = glm::vec2(0.0f);
glm::vec2 player_vel = glm::vec2(0.0f);
// level
uint8_t level = 1;
// course generation
void generate_background_and_course(bool new_course = true);
// reset level
void reset_level(bool level_incr = false, bool new_course = true);
// check for collision with course
bool check_collision();
//----- drawing handled by PPU466 -----
PPU466 ppu;
};