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/GameEngine.cpp
- Revision:
- 3:97cd7b3d89d0
- Parent:
- 2:88019d96e1da
- Child:
- 4:87f3f3e5cdd9
--- a/GameEngine/GameEngine.cpp Wed Apr 24 12:38:53 2019 +0000
+++ b/GameEngine/GameEngine.cpp Sun Apr 28 16:55:24 2019 +0000
@@ -1,7 +1,6 @@
#include "GameEngine.h"
-//enum Action { waiting, moving, attacking, dying };
-//enum Vegetable { Potato, Carrot, Onion };
+
int bulletSymbol[8][4] = {
{ 0,1,1,0 },
@@ -44,9 +43,10 @@
void GameEngine::init(int CrossHairsSpeed, int fps)
{
// initialise the game parameters
+ _game_over = false;
_fps = fps;
- _levelNumber = 3;
- _money = 0.0f;
+ _frame = 0;
+ _levelNumber = 1;
_killCount = 0;
_wall.init(100);
@@ -77,33 +77,23 @@
{
- char money[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
+ char level[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
// so can display a string of a maximum 14 characters in length
// or create formatted strings - ensure they aren't more than 14 characters long
- int length = sprintf(money,"$%.1f",_money); // print formatted data to buffer
+ int length = sprintf(level,"lvl: %i",_levelNumber); // print formatted data to buffer
// it is important the format specifier ensures the length will fit in the buffer
if (length <= 14) { // if string will fit on display (assuming printing at x=0)
- lcd.printString(money,23,0);
+ lcd.printString(level,23,0);
}
//draw wall
_wall.draw(lcd);
- //draw ammo symbol
- // x origin, y origin, rows, cols, sprite
- lcd.drawSprite((WIDTH/4)*3,0,8,4,(int *)bulletSymbol);
-
- // draw the elements in the LCD buffer
- /*
- _enemy.draw(lcd);
- _enemy2.draw(lcd);
- */
-
-
- for (int i=0; i < _levelNumber; i++) {
+ //draw enemies
+ for (int i=0; i < _level.get_number_of_enemies(); i++) {
_enemies[i].draw(lcd);
}
@@ -112,6 +102,20 @@
void GameEngine::update(Gamepad &pad)
{
+ _frame = _frame + 1;
+ if (_frame%(_fps/2) == 0) {
+
+ for (int i=0; i < _level.get_number_of_enemies(); i++) {
+ if(_enemies[i].get_current_action() == attacking) {
+ _wall.take_damage(1);
+ if (_wall.get_wall_health() < 0) {
+ _game_over = true;
+ }
+ }
+ }
+ }
+
+
_level.update(_fps);
_crossHairs.update(_angle,_mag, _fps);
/*
@@ -120,7 +124,7 @@
*/
- for (int i=0; i < _levelNumber; i++) {
+ for (int i=0; i < _level.get_number_of_enemies(); i++) {
_enemies[i].update(_fps);
}
@@ -140,23 +144,19 @@
Vector2D shotLoc = _crossHairs.get_pos();
- for(int i = 0; i < _levelNumber; i++) {
+ for(int i = 0; i < _level.get_number_of_enemies(); i++) {
if (_enemies[i].get_alive()) {
Vector2D enemyLoc = _enemies[i].get_pos();
if (abs(enemyLoc.x - shotLoc.x) < 4) {
if (abs(enemyLoc.y - shotLoc.y) < 4) {
_killCount = _killCount + 1;
_enemies[i].set_alive(false);
- float moneyEarnt = _enemies[i].get_value();
- _money = _money + moneyEarnt;
Action myAc;
myAc = dying;
_enemies[i].set_current_action(myAc);
- if (_killCount == _levelNumber) {
- Vector2D posch = {WIDTH/2, HEIGHT/2};
- _crossHairs.set_pos(posch);
+ if (_killCount == _level.get_number_of_enemies()) {
_killCount = 0;
_levelNumber = _levelNumber + 1;
_enemies = _level.init(_levelNumber);
@@ -169,3 +169,12 @@
}
}
+
+
+bool GameEngine::get_game_over() {
+ return _game_over;
+
+}
+void GameEngine::set_game_over(bool is_game_over) {
+ _game_over = is_game_over;
+}