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/Levelobjects.cpp
- Revision:
- 27:e73dd64ef334
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Gameengine/Levelobjects.cpp Tue Apr 09 18:46:14 2019 +0000
@@ -0,0 +1,136 @@
+#include "Levelobjects.h"
+
+Levelobjects::Levelobjects()
+{
+ _turn_flag = 0;
+ _counter =0;
+}
+
+Levelobjects::~Levelobjects()
+{
+}
+
+void Levelobjects::game_init()
+{
+ _level_select = 1;
+ _miner.miner_init(3, 33);
+ //enemy_init(20, 78, 40);
+ enemy_init(22, 20, 40);
+ _lives = 3;
+}
+
+/*void Levelobjects::get_miner_pos()
+{
+ Vector2D p = _miner.get_pos();
+
+ printf("miner x %f \n", p.x);
+ printf("miner y %f \n", p.y);
+}*/
+
+void Levelobjects::read_direction(Gamepad &pad)
+{
+ _d = pad.get_direction();
+}
+
+void Levelobjects::update(N5110 &lcd, Gamepad &pad)
+{
+ t.start();
+ read_direction(pad);
+ //get_miner_pos();
+ _miner.miner_move(_d, lcd);
+ _miner.miner_gravity(lcd);
+ _miner.miner_jump(lcd, pad);
+ _miner.miner_land(lcd);
+ lose_life(lcd);
+ game_over();
+ next_level(lcd);
+}
+
+void Levelobjects::draw(Key _k, N5110 &lcd, Gamepad &pad)
+{
+ if (_level_select == 1) {
+
+ _miner.miner_draw(lcd);
+ _l1.level_platforms(lcd);
+ _l1.soft_blocks(lcd);
+ _l1.solid_blocks(lcd);
+ _l1.keys(lcd, pad);
+ _l1.keys_collected();
+ _l1.traps(lcd);
+ enemy_move(lcd);
+ enemy_collision();
+ }
+ if(_level_select == 2) {
+
+ _miner.miner_draw(lcd);
+ lcd.drawLine(0,47,83,47,1);
+ //printf("level 2 time = %f \n", t.read());
+ }
+}
+
+void Levelobjects::lose_life(N5110 &lcd)
+{
+
+ if (_l1.traps(lcd) == 1 || enemy_collision() == true) {
+ _lives--;
+ _miner.miner_init(3, 33);
+ wait(1);
+ }
+}
+
+bool Levelobjects::game_over()
+{
+ if(_lives == 0 || t.read() > 60) {
+ t.stop();
+ return true;
+ } else {
+ return false;
+ }
+}
+
+void Levelobjects::next_level(N5110 &lcd)
+{
+ if(_l1.level1_exit(lcd) == true) {
+ t.stop();
+ _level_select = 2;
+ _miner.miner_init(3,0);
+ printf("Final score is %f \n ",_lives * (60 - t.read())+ (10 * _l1.keys_collected()));
+ }
+}
+
+void Levelobjects::enemy_init(int x, int y, volatile int d)
+{
+ _x = x;
+ _y = y;
+ _distance = d;
+}
+
+void Levelobjects::enemy_move(N5110 &lcd)
+{
+ lcd.drawSprite(_x,_y,5,3, (int *) enemy);
+ if (_turn_flag == 0) {
+ _x = _x + 0.5;
+ _counter++;
+ }
+ if(_counter == _distance) {
+ _turn_flag = 1;
+ }
+ if (_turn_flag == 1) {
+ _x = _x - 0.5;
+ _counter--;
+ }
+ if (_counter == 0) {
+ _turn_flag = 0;
+ }
+}
+
+bool Levelobjects::enemy_collision()
+{
+ Vector2D p = _miner.get_pos();
+
+ if (p.x < _x+2 && p.x+2 > _x && p.y < _y+4 && p.y+8 > _y) {
+ return true;
+ } else {
+ return false;
+ }
+}
\ No newline at end of file