Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: GameEngine/engine.cpp
- Revision:
- 11:0e6a221ad8a9
- Parent:
- 10:b939edd9b87c
- Child:
- 12:bc9a43f56261
--- a/GameEngine/engine.cpp Fri May 10 12:00:30 2019 +0000
+++ b/GameEngine/engine.cpp Fri May 10 13:58:51 2019 +0000
@@ -1,8 +1,7 @@
#include "engine.h"
-Engine::Engine(){
-
+Engine::Engine(){
}
@@ -15,19 +14,25 @@
_screen_height = screenHeight;
_screen_width = screenWidth;
+ // set the car parameters to 0.0f
car.set_speed(0.0f);
car.set_car_position(0.0f);
car.set_car_distance(0.0f);
- car.set_gear();
+ car.set_gear(); // this one starts from 1
+ // road curvature is 0.0 i.e it is in the middle (straight line)
_curvature = 0.0f;
+ // set map fields to 0.0 as well
map.set_player_curvature(0.0f);
map.set_timed_curvature(0.0f);
+ // some variables to keep details of the current map and time
_track_distance = 0.0f;
_lap_time = 0.0f;
+
+ // design a map
addSegment(0.0f, 500.0f);
addSegment(-1.0f, 600.0f);
addSegment(1.0f, 600.0f);
@@ -39,6 +44,7 @@
addSegment(-1.0f, 600.0f);
addSegment(0.0f, 700.0f);
+ // find how long is the track
calc_track_distance();
}
@@ -46,12 +52,7 @@
void Engine::update(Gamepad &pad, N5110 &lcd, float elapsedTime) {
- clearScreen(lcd);
-
- drawFrame(lcd); // draw the frame around the picture
-
-
-
+ // split the curvature do it appears slowly
if(fabs(map.get_player_curvature() - map.get_timed_curvature()) >= 0.5f) {
car.set_speed(car.get_speed() - 5.0f * elapsedTime);
@@ -66,6 +67,7 @@
float foffset = 0;
int ntrackSection = 0;
+ // add the time to the overall time
_lap_time += elapsedTime;
// restart the car distance
@@ -84,21 +86,19 @@
// the curvature of the segment we are in
float targetCurvature = track[ntrackSection -1].curvature;
- // small curvature to be displayed
+ // small curvature to be displayed but this time based on speed as well
float curvDiff = (targetCurvature - _curvature) * elapsedTime * car.get_speed();
-
- // slowly changing curvature
+ // slowly changing curvature
_curvature += curvDiff;
// the best curvature we should aim for
+ // other wise the car gets slowed down
map.set_timed_curvature( (_curvature) * elapsedTime * car.get_speed() );
- draw(lcd); // change the pixels of the buffer;
// draw car
car.set_car_position(map.get_player_curvature() - map.get_timed_curvature());
- car.draw(lcd, _screen_width);
- lcd.refresh();
+
}
void Engine::read_input(Gamepad &pad, float elapsedTime) {
@@ -106,6 +106,7 @@
float _mag;
_mag = pad.get_mag();
+ // car moving to the front
if(pad.get_direction() == N) {
car.set_speed(car.get_speed() + 0.5f * elapsedTime * _mag);
car.set_direction(0); //
@@ -114,11 +115,13 @@
car.set_speed(car.get_speed() - 1.8f * elapsedTime);
}
+ // car brake
if(pad.get_direction() == S) {
car.set_speed(car.get_speed() - 5.0f * elapsedTime * _mag);
car.set_direction(0); //
}
-
+
+ // car moving to the diagonally
if(pad.get_direction() == W || pad.get_direction() == NW) {
if(pad.get_direction() == NW) {
map.set_player_curvature(map.get_player_curvature() - 0.7f * elapsedTime);
@@ -129,6 +132,7 @@
car.set_direction(1);
}
+ // car moving to the diagonally
if(pad.get_direction() == E || pad.get_direction() == NE) {
if(pad.get_direction() == NE) {
map.set_player_curvature(map.get_player_curvature() + 0.7f * elapsedTime);
@@ -138,6 +142,8 @@
map.set_player_curvature(map.get_player_curvature() + 1.4f * elapsedTime);
car.set_direction(2);
}
+
+ // change gears
if(pad.check_event(Gamepad::Y_PRESSED)) {
car.upShift();
}
@@ -146,7 +152,7 @@
}
}
-
+// draw the road and the sides we only need half the screen
void Engine::draw(N5110 &lcd) {
for (int y = 0; y < _screen_height / 2; y++) {
@@ -176,9 +182,12 @@
leftSide,
rightSide);
}
+ car.draw(lcd, 84);
+ drawFrame(lcd); // draw the frame around the picture
+
}
}
-
+// colour the pixel accoring to the give boundaries
void Engine::assignPixel(N5110 &lcd,int x, int y, int row, float SideColour, float GrassColour,
int leftGrass,
int rightGrass,
@@ -204,6 +213,7 @@
}
}
+// use sin function to 'paint' the grass
float Engine::defineStripes(N5110 &lcd, Car &car, float frequency, float perspective) {
float colour = sinf(frequency * powf(1.0f - perspective, 3) + car.get_car_distance() * 0.1f);
@@ -213,18 +223,20 @@
return colour;
}
-
+// add segment to the map
void Engine::addSegment(float curvature, float meters) {
// clap the curvature variable
if(curvature < -1.0f) curvature = -1.0f;
if(curvature > 1.0f) curvature = 1.0f;
+ // place a segment on the map
MapSegment segment;
segment.curvature = curvature;
segment.meters = meters;
track.push_back(segment);
}
+// go throught the map and add distance
void Engine::calc_track_distance() {
for (vector<MapSegment>::iterator it = track.begin(); it != track.end(); ++it) {