test 1 doc

Dependencies:   mbed Gamepad2

Committer:
joebarhouch
Date:
Tue May 26 22:15:42 2020 +0000
Revision:
8:d19b30a6cd69
Parent:
7:530ca713d2b2
Child:
9:9830d3a78572
Implemented enemies with AI that walk around with physics

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joebarhouch 3:e4e1cbf750b6 1 #include "Engine.h"
joebarhouch 3:e4e1cbf750b6 2
joebarhouch 8:d19b30a6cd69 3 Platform maps[6] = {Platform(0, 40, 20, 3), Platform(0, 14, 20, 3), Platform(64, 14, 20, 3), Platform(64, 40, 20, 3), Platform(5, 30, 30, 3), Platform(50, 30, 30, 3)};
joebarhouch 7:530ca713d2b2 4 int mapSize = sizeof(maps)/sizeof(*maps);
joebarhouch 8:d19b30a6cd69 5 vector <Enemy> enemies;
joebarhouch 7:530ca713d2b2 6
joebarhouch 7:530ca713d2b2 7
joebarhouch 7:530ca713d2b2 8 ////////////////////// DRAW MAP //////////////////////////
joebarhouch 7:530ca713d2b2 9 void drawMap(N5110 &lcd)
joebarhouch 7:530ca713d2b2 10 {
joebarhouch 7:530ca713d2b2 11 for (int i = 0; i < mapSize; i++) {
joebarhouch 7:530ca713d2b2 12 maps[i].draw(lcd);
joebarhouch 7:530ca713d2b2 13
joebarhouch 7:530ca713d2b2 14 //debugs
joebarhouch 7:530ca713d2b2 15 //coords = maps[i].get_pos();
joebarhouch 7:530ca713d2b2 16 //printf("x: %i, y: %i,w: %i,h: %i \n",coords.x, coords.y, coords.width, coords.height);
joebarhouch 7:530ca713d2b2 17 }
joebarhouch 7:530ca713d2b2 18 //debugs
joebarhouch 7:530ca713d2b2 19 //printf("-----------------------------------------\n");
joebarhouch 7:530ca713d2b2 20 }
joebarhouch 7:530ca713d2b2 21
joebarhouch 7:530ca713d2b2 22
joebarhouch 7:530ca713d2b2 23
joebarhouch 3:e4e1cbf750b6 24 Engine::Engine()
joebarhouch 3:e4e1cbf750b6 25 {
joebarhouch 3:e4e1cbf750b6 26 }
joebarhouch 3:e4e1cbf750b6 27 Engine::~Engine()
joebarhouch 3:e4e1cbf750b6 28 {
joebarhouch 3:e4e1cbf750b6 29 }
joebarhouch 3:e4e1cbf750b6 30
joebarhouch 6:00d20886e4f8 31
joebarhouch 6:00d20886e4f8 32 ////////////////////// INIT ////////////////////////////
joebarhouch 3:e4e1cbf750b6 33 void Engine::init()
joebarhouch 3:e4e1cbf750b6 34 {
joebarhouch 3:e4e1cbf750b6 35 //init coord
joebarhouch 3:e4e1cbf750b6 36 _px = WIDTH / 2;
joebarhouch 7:530ca713d2b2 37 _py = 5;
joebarhouch 6:00d20886e4f8 38
joebarhouch 3:e4e1cbf750b6 39 //init call
joebarhouch 3:e4e1cbf750b6 40 _p.init(_px, _py);
joebarhouch 7:530ca713d2b2 41
joebarhouch 8:d19b30a6cd69 42 //ennemy
joebarhouch 8:d19b30a6cd69 43 enemies.push_back(Enemy(0, 0));
joebarhouch 8:d19b30a6cd69 44 enemies.push_back(Enemy(75, 0));
joebarhouch 8:d19b30a6cd69 45
joebarhouch 8:d19b30a6cd69 46
joebarhouch 7:530ca713d2b2 47 //physics parameters
joebarhouch 7:530ca713d2b2 48 _Ypos = 0;
joebarhouch 7:530ca713d2b2 49 _fall = true;
joebarhouch 8:d19b30a6cd69 50 _c = false;
joebarhouch 8:d19b30a6cd69 51
joebarhouch 3:e4e1cbf750b6 52 }
joebarhouch 3:e4e1cbf750b6 53
joebarhouch 6:00d20886e4f8 54
joebarhouch 6:00d20886e4f8 55
joebarhouch 6:00d20886e4f8 56 ////////////////////// INPUT //////////////////////////
joebarhouch 5:928c2eee4109 57 //reads direction and magnitude from the JOYSTICK to control the player
joebarhouch 3:e4e1cbf750b6 58 void Engine::read_input(Gamepad &pad)
joebarhouch 3:e4e1cbf750b6 59 {
joebarhouch 3:e4e1cbf750b6 60 _d = pad.get_direction();
joebarhouch 3:e4e1cbf750b6 61 _mag = pad.get_mag();
joebarhouch 8:d19b30a6cd69 62 _jump = pad.B_held();
joebarhouch 8:d19b30a6cd69 63 //printf("%s", _jump ? "true\n" : "false\n");
joebarhouch 3:e4e1cbf750b6 64 }
joebarhouch 3:e4e1cbf750b6 65
joebarhouch 7:530ca713d2b2 66
joebarhouch 7:530ca713d2b2 67
joebarhouch 7:530ca713d2b2 68
joebarhouch 7:530ca713d2b2 69
joebarhouch 6:00d20886e4f8 70 ////////////////////// DRAW ///////////////////////////
joebarhouch 6:00d20886e4f8 71 //draw both player and map
joebarhouch 3:e4e1cbf750b6 72 void Engine::draw(N5110 &lcd)
joebarhouch 3:e4e1cbf750b6 73 {
joebarhouch 7:530ca713d2b2 74
joebarhouch 7:530ca713d2b2 75
joebarhouch 3:e4e1cbf750b6 76 // player
joebarhouch 3:e4e1cbf750b6 77 _p.draw(lcd);
joebarhouch 7:530ca713d2b2 78
joebarhouch 8:d19b30a6cd69 79 // map
joebarhouch 8:d19b30a6cd69 80 drawMap(lcd);
joebarhouch 8:d19b30a6cd69 81
joebarhouch 8:d19b30a6cd69 82 //enemies
joebarhouch 8:d19b30a6cd69 83 for(int i = 0; i < enemies.size(); i ++) {
joebarhouch 8:d19b30a6cd69 84 enemies.at(i).draw(lcd);
joebarhouch 8:d19b30a6cd69 85 }
joebarhouch 8:d19b30a6cd69 86
joebarhouch 8:d19b30a6cd69 87
joebarhouch 3:e4e1cbf750b6 88 }
joebarhouch 3:e4e1cbf750b6 89
joebarhouch 7:530ca713d2b2 90
joebarhouch 7:530ca713d2b2 91
joebarhouch 7:530ca713d2b2 92
joebarhouch 6:00d20886e4f8 93 ////////////////////// UPDATE //////////////////////////
joebarhouch 5:928c2eee4109 94 //provide the player file with necessary Joystick values
joebarhouch 3:e4e1cbf750b6 95 void Engine::update(Gamepad &pad)
joebarhouch 3:e4e1cbf750b6 96 {
joebarhouch 7:530ca713d2b2 97 floorCollide();
joebarhouch 8:d19b30a6cd69 98 efloorCollide();
joebarhouch 8:d19b30a6cd69 99 spawnEnemy();
joebarhouch 3:e4e1cbf750b6 100
joebarhouch 8:d19b30a6cd69 101 _p.update(_d,_mag, _Ypos, _fall, _jump);
joebarhouch 7:530ca713d2b2 102
joebarhouch 8:d19b30a6cd69 103 for(int i = 0; i < enemies.size(); i ++) {
joebarhouch 8:d19b30a6cd69 104 enemies.at(i).update(_eYpos, _efall);
joebarhouch 8:d19b30a6cd69 105 //printf("Enemy %i!\n", i);
joebarhouch 8:d19b30a6cd69 106 }
joebarhouch 6:00d20886e4f8 107
joebarhouch 8:d19b30a6cd69 108 /*
joebarhouch 8:d19b30a6cd69 109 if (_c == true) {
joebarhouch 8:d19b30a6cd69 110
joebarhouch 8:d19b30a6cd69 111 //debug
joebarhouch 8:d19b30a6cd69 112 //printf("collison\n");
joebarhouch 8:d19b30a6cd69 113 } else {
joebarhouch 6:00d20886e4f8 114
joebarhouch 8:d19b30a6cd69 115 //debug
joebarhouch 8:d19b30a6cd69 116 //printf("no collison\n");
joebarhouch 8:d19b30a6cd69 117 }
joebarhouch 8:d19b30a6cd69 118 //enemmyCollide(pad);
joebarhouch 6:00d20886e4f8 119 }
joebarhouch 8:d19b30a6cd69 120 */
joebarhouch 3:e4e1cbf750b6 121 }
joebarhouch 7:530ca713d2b2 122
joebarhouch 6:00d20886e4f8 123 ////////////////////// FLOOR COLLISION //////////////////////////
joebarhouch 7:530ca713d2b2 124 void Engine::floorCollide()
joebarhouch 6:00d20886e4f8 125 {
joebarhouch 8:d19b30a6cd69 126
joebarhouch 7:530ca713d2b2 127 int a;
joebarhouch 8:d19b30a6cd69 128 Vector4 coords[mapSize];
joebarhouch 6:00d20886e4f8 129 Vector2D player = _p.get_pos();
joebarhouch 8:d19b30a6cd69 130 //coordinates of platforms
joebarhouch 8:d19b30a6cd69 131 for(int i = 0; i < mapSize; i++) {
joebarhouch 7:530ca713d2b2 132 coords[i] = maps[i].get_pos();
joebarhouch 7:530ca713d2b2 133 }
joebarhouch 7:530ca713d2b2 134
joebarhouch 6:00d20886e4f8 135
joebarhouch 8:d19b30a6cd69 136 if(_c == false) {
joebarhouch 7:530ca713d2b2 137 _fall = true;
joebarhouch 7:530ca713d2b2 138 for(int j=0; j < mapSize; j++) {
joebarhouch 8:d19b30a6cd69 139 if(player.x+5 >= coords[j].x && player.x+1 <= coords[j].x + coords[j].width && player.y+9 >= coords[j].y && player.y+9 <= coords[j].y + coords[j].height) {
joebarhouch 7:530ca713d2b2 140 a = j;
joebarhouch 8:d19b30a6cd69 141 _c = true;
joebarhouch 7:530ca713d2b2 142 //printf("%i, %i\n", player.x, player.y );
joebarhouch 7:530ca713d2b2 143 }
joebarhouch 7:530ca713d2b2 144 }
joebarhouch 6:00d20886e4f8 145 }
joebarhouch 8:d19b30a6cd69 146
joebarhouch 8:d19b30a6cd69 147 if(_c == true) {
joebarhouch 8:d19b30a6cd69 148 _Ypos = coords[a].y - 8;
joebarhouch 8:d19b30a6cd69 149 _fall = false;
joebarhouch 8:d19b30a6cd69 150 _c = false;
joebarhouch 8:d19b30a6cd69 151 }
joebarhouch 8:d19b30a6cd69 152 }
joebarhouch 8:d19b30a6cd69 153
joebarhouch 8:d19b30a6cd69 154
joebarhouch 8:d19b30a6cd69 155 ////////////////////// SPAWN ENEMY /////////////////////////////////////
joebarhouch 8:d19b30a6cd69 156 void Engine::spawnEnemy()
joebarhouch 8:d19b30a6cd69 157 {
joebarhouch 8:d19b30a6cd69 158 for(int i = 0; i < enemies.size(); i ++) {
joebarhouch 8:d19b30a6cd69 159 //printf("%s", enemies.at(i).fell() ? "true\n" : "false\n");
joebarhouch 8:d19b30a6cd69 160 if ( enemies.at(i).fell() == true ) {
joebarhouch 8:d19b30a6cd69 161 enemies.erase(enemies.begin() + i);
joebarhouch 8:d19b30a6cd69 162 enemies.push_back(Enemy(1,1));
joebarhouch 7:530ca713d2b2 163 }
joebarhouch 8:d19b30a6cd69 164 }
joebarhouch 7:530ca713d2b2 165
joebarhouch 6:00d20886e4f8 166 }
joebarhouch 3:e4e1cbf750b6 167
joebarhouch 5:928c2eee4109 168
joebarhouch 8:d19b30a6cd69 169 ////////////////////// ENNEMY FLOOR COLLISION //////////////////////////
joebarhouch 8:d19b30a6cd69 170 void Engine::efloorCollide()
joebarhouch 8:d19b30a6cd69 171 {
joebarhouch 8:d19b30a6cd69 172 vector <Vector2D> epos;
joebarhouch 8:d19b30a6cd69 173 Vector4 coord[mapSize];
joebarhouch 8:d19b30a6cd69 174 int b;
joebarhouch 8:d19b30a6cd69 175 //coordinates of platforms
joebarhouch 8:d19b30a6cd69 176 for(int i = 0; i < mapSize; i++) {
joebarhouch 8:d19b30a6cd69 177 coord[i] = maps[i].get_pos();
joebarhouch 8:d19b30a6cd69 178 }
joebarhouch 8:d19b30a6cd69 179
joebarhouch 8:d19b30a6cd69 180 for(int i = 0; i <enemies.size(); i++) {
joebarhouch 8:d19b30a6cd69 181 epos.push_back(enemies.at(i).get_pos());
joebarhouch 8:d19b30a6cd69 182 //printf("pos of %i: %f, %f\n", i, epos.at(i).x, epos.at(i).y);
joebarhouch 8:d19b30a6cd69 183 }
joebarhouch 8:d19b30a6cd69 184
joebarhouch 8:d19b30a6cd69 185
joebarhouch 8:d19b30a6cd69 186 if(_e == false) {
joebarhouch 8:d19b30a6cd69 187 _efall = true;
joebarhouch 8:d19b30a6cd69 188 for(int i=0; i<mapSize; i++) {
joebarhouch 8:d19b30a6cd69 189 for(int j=0; j < epos.size(); j++) {
joebarhouch 8:d19b30a6cd69 190 if(epos.at(j).x >= coord[i].x && epos.at(j).x+1 <= coord[i].x + coord[i].width && epos.at(j).y+6 >= coord[i].y && epos.at(j).y+6 <= coord[i].y + coord[i].height) {
joebarhouch 8:d19b30a6cd69 191 b = j;
joebarhouch 8:d19b30a6cd69 192 _e = true;
joebarhouch 8:d19b30a6cd69 193 //printf("%f, %f\n", epos.at(j).x,epos.at(j).y );
joebarhouch 8:d19b30a6cd69 194 }
joebarhouch 8:d19b30a6cd69 195 }
joebarhouch 8:d19b30a6cd69 196 }
joebarhouch 8:d19b30a6cd69 197 }
joebarhouch 8:d19b30a6cd69 198
joebarhouch 8:d19b30a6cd69 199 if(_e == true) {
joebarhouch 8:d19b30a6cd69 200 _eYpos = coord[b].y - 8;
joebarhouch 8:d19b30a6cd69 201 _efall = false;
joebarhouch 8:d19b30a6cd69 202 _e = false;
joebarhouch 8:d19b30a6cd69 203 //printf("enemy fall");
joebarhouch 8:d19b30a6cd69 204 }
joebarhouch 8:d19b30a6cd69 205
joebarhouch 8:d19b30a6cd69 206 }
joebarhouch 8:d19b30a6cd69 207