ELEC2645 (2019/20)
/
ELEC2645_Project_el18jb
test 1 doc
Engine/Engine.cpp@7:530ca713d2b2, 2020-05-26 (annotated)
- Committer:
- joebarhouch
- Date:
- Tue May 26 01:45:20 2020 +0000
- Revision:
- 7:530ca713d2b2
- Parent:
- 6:00d20886e4f8
- Child:
- 8:d19b30a6cd69
Fully functional game physics with floor collision and jumps
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joebarhouch | 3:e4e1cbf750b6 | 1 | #include "Engine.h" |
joebarhouch | 3:e4e1cbf750b6 | 2 | |
joebarhouch | 7:530ca713d2b2 | 3 | Platform maps[5] = {Platform(0, 15, 20, 3), Platform(64, 15, 20, 3), Platform(0, 40, 20, 3), Platform(64, 40, 20, 3), Platform(20, 30, 30, 3)}; |
joebarhouch | 7:530ca713d2b2 | 4 | int mapSize = sizeof(maps)/sizeof(*maps); |
joebarhouch | 7:530ca713d2b2 | 5 | |
joebarhouch | 7:530ca713d2b2 | 6 | |
joebarhouch | 7:530ca713d2b2 | 7 | ////////////////////// DRAW MAP ////////////////////////// |
joebarhouch | 7:530ca713d2b2 | 8 | void drawMap(N5110 &lcd) |
joebarhouch | 7:530ca713d2b2 | 9 | { |
joebarhouch | 7:530ca713d2b2 | 10 | for (int i = 0; i < mapSize; i++) { |
joebarhouch | 7:530ca713d2b2 | 11 | maps[i].draw(lcd); |
joebarhouch | 7:530ca713d2b2 | 12 | |
joebarhouch | 7:530ca713d2b2 | 13 | //debugs |
joebarhouch | 7:530ca713d2b2 | 14 | //coords = maps[i].get_pos(); |
joebarhouch | 7:530ca713d2b2 | 15 | //printf("x: %i, y: %i,w: %i,h: %i \n",coords.x, coords.y, coords.width, coords.height); |
joebarhouch | 7:530ca713d2b2 | 16 | } |
joebarhouch | 7:530ca713d2b2 | 17 | //debugs |
joebarhouch | 7:530ca713d2b2 | 18 | //printf("-----------------------------------------\n"); |
joebarhouch | 7:530ca713d2b2 | 19 | } |
joebarhouch | 7:530ca713d2b2 | 20 | |
joebarhouch | 7:530ca713d2b2 | 21 | |
joebarhouch | 7:530ca713d2b2 | 22 | |
joebarhouch | 3:e4e1cbf750b6 | 23 | Engine::Engine() |
joebarhouch | 3:e4e1cbf750b6 | 24 | { |
joebarhouch | 3:e4e1cbf750b6 | 25 | } |
joebarhouch | 3:e4e1cbf750b6 | 26 | Engine::~Engine() |
joebarhouch | 3:e4e1cbf750b6 | 27 | { |
joebarhouch | 3:e4e1cbf750b6 | 28 | } |
joebarhouch | 3:e4e1cbf750b6 | 29 | |
joebarhouch | 6:00d20886e4f8 | 30 | |
joebarhouch | 6:00d20886e4f8 | 31 | ////////////////////// INIT //////////////////////////// |
joebarhouch | 3:e4e1cbf750b6 | 32 | void Engine::init() |
joebarhouch | 3:e4e1cbf750b6 | 33 | { |
joebarhouch | 3:e4e1cbf750b6 | 34 | //init coord |
joebarhouch | 3:e4e1cbf750b6 | 35 | _px = WIDTH / 2; |
joebarhouch | 7:530ca713d2b2 | 36 | _py = 5; |
joebarhouch | 6:00d20886e4f8 | 37 | |
joebarhouch | 6:00d20886e4f8 | 38 | |
joebarhouch | 3:e4e1cbf750b6 | 39 | //init call |
joebarhouch | 3:e4e1cbf750b6 | 40 | _p.init(_px, _py); |
joebarhouch | 7:530ca713d2b2 | 41 | |
joebarhouch | 7:530ca713d2b2 | 42 | //physics parameters |
joebarhouch | 7:530ca713d2b2 | 43 | _Ypos = 0; |
joebarhouch | 7:530ca713d2b2 | 44 | _fall = true; |
joebarhouch | 7:530ca713d2b2 | 45 | _collide = false; |
joebarhouch | 3:e4e1cbf750b6 | 46 | } |
joebarhouch | 3:e4e1cbf750b6 | 47 | |
joebarhouch | 6:00d20886e4f8 | 48 | |
joebarhouch | 6:00d20886e4f8 | 49 | |
joebarhouch | 6:00d20886e4f8 | 50 | ////////////////////// INPUT ////////////////////////// |
joebarhouch | 5:928c2eee4109 | 51 | //reads direction and magnitude from the JOYSTICK to control the player |
joebarhouch | 3:e4e1cbf750b6 | 52 | void Engine::read_input(Gamepad &pad) |
joebarhouch | 3:e4e1cbf750b6 | 53 | { |
joebarhouch | 3:e4e1cbf750b6 | 54 | _d = pad.get_direction(); |
joebarhouch | 3:e4e1cbf750b6 | 55 | _mag = pad.get_mag(); |
joebarhouch | 7:530ca713d2b2 | 56 | _jump = pad.A_held(); |
joebarhouch | 3:e4e1cbf750b6 | 57 | } |
joebarhouch | 3:e4e1cbf750b6 | 58 | |
joebarhouch | 7:530ca713d2b2 | 59 | |
joebarhouch | 7:530ca713d2b2 | 60 | |
joebarhouch | 7:530ca713d2b2 | 61 | |
joebarhouch | 7:530ca713d2b2 | 62 | |
joebarhouch | 6:00d20886e4f8 | 63 | ////////////////////// DRAW /////////////////////////// |
joebarhouch | 6:00d20886e4f8 | 64 | //draw both player and map |
joebarhouch | 3:e4e1cbf750b6 | 65 | void Engine::draw(N5110 &lcd) |
joebarhouch | 3:e4e1cbf750b6 | 66 | { |
joebarhouch | 7:530ca713d2b2 | 67 | |
joebarhouch | 7:530ca713d2b2 | 68 | drawMap(lcd); |
joebarhouch | 7:530ca713d2b2 | 69 | |
joebarhouch | 3:e4e1cbf750b6 | 70 | // player |
joebarhouch | 3:e4e1cbf750b6 | 71 | _p.draw(lcd); |
joebarhouch | 7:530ca713d2b2 | 72 | |
joebarhouch | 3:e4e1cbf750b6 | 73 | } |
joebarhouch | 3:e4e1cbf750b6 | 74 | |
joebarhouch | 7:530ca713d2b2 | 75 | |
joebarhouch | 7:530ca713d2b2 | 76 | |
joebarhouch | 7:530ca713d2b2 | 77 | |
joebarhouch | 6:00d20886e4f8 | 78 | ////////////////////// UPDATE ////////////////////////// |
joebarhouch | 5:928c2eee4109 | 79 | //provide the player file with necessary Joystick values |
joebarhouch | 3:e4e1cbf750b6 | 80 | void Engine::update(Gamepad &pad) |
joebarhouch | 3:e4e1cbf750b6 | 81 | { |
joebarhouch | 7:530ca713d2b2 | 82 | |
joebarhouch | 7:530ca713d2b2 | 83 | floorCollide(); |
joebarhouch | 7:530ca713d2b2 | 84 | _p.update(_d,_mag, _Ypos, _fall, _jump); |
joebarhouch | 6:00d20886e4f8 | 85 | } |
joebarhouch | 3:e4e1cbf750b6 | 86 | |
joebarhouch | 7:530ca713d2b2 | 87 | |
joebarhouch | 6:00d20886e4f8 | 88 | /* |
joebarhouch | 7:530ca713d2b2 | 89 | |
joebarhouch | 6:00d20886e4f8 | 90 | |
joebarhouch | 6:00d20886e4f8 | 91 | if (_collision == true) { |
joebarhouch | 6:00d20886e4f8 | 92 | |
joebarhouch | 6:00d20886e4f8 | 93 | //debug |
joebarhouch | 6:00d20886e4f8 | 94 | //printf("collison\n"); |
joebarhouch | 6:00d20886e4f8 | 95 | } else { |
joebarhouch | 6:00d20886e4f8 | 96 | |
joebarhouch | 6:00d20886e4f8 | 97 | //debug |
joebarhouch | 6:00d20886e4f8 | 98 | //printf("no collison\n"); |
joebarhouch | 6:00d20886e4f8 | 99 | } |
joebarhouch | 3:e4e1cbf750b6 | 100 | //enemmyCollide(pad); |
joebarhouch | 3:e4e1cbf750b6 | 101 | } |
joebarhouch | 6:00d20886e4f8 | 102 | */ |
joebarhouch | 6:00d20886e4f8 | 103 | |
joebarhouch | 7:530ca713d2b2 | 104 | |
joebarhouch | 6:00d20886e4f8 | 105 | ////////////////////// FLOOR COLLISION ////////////////////////// |
joebarhouch | 7:530ca713d2b2 | 106 | void Engine::floorCollide() |
joebarhouch | 6:00d20886e4f8 | 107 | { |
joebarhouch | 7:530ca713d2b2 | 108 | //player.x + 8 >= 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 | 109 | _collide = false; |
joebarhouch | 7:530ca713d2b2 | 110 | int a; |
joebarhouch | 6:00d20886e4f8 | 111 | Vector2D player = _p.get_pos(); |
joebarhouch | 7:530ca713d2b2 | 112 | Vector4 coords[mapSize]; |
joebarhouch | 7:530ca713d2b2 | 113 | |
joebarhouch | 7:530ca713d2b2 | 114 | for(int i = 0; i<mapSize; i++) { |
joebarhouch | 7:530ca713d2b2 | 115 | coords[i] = maps[i].get_pos(); |
joebarhouch | 7:530ca713d2b2 | 116 | } |
joebarhouch | 7:530ca713d2b2 | 117 | |
joebarhouch | 6:00d20886e4f8 | 118 | |
joebarhouch | 7:530ca713d2b2 | 119 | if(_collide == false) { |
joebarhouch | 7:530ca713d2b2 | 120 | _fall = true; |
joebarhouch | 7:530ca713d2b2 | 121 | for(int j=0; j < mapSize; j++) { |
joebarhouch | 7:530ca713d2b2 | 122 | if(player.x + 8 >= 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 | 123 | a = j; |
joebarhouch | 7:530ca713d2b2 | 124 | _collide = true; |
joebarhouch | 7:530ca713d2b2 | 125 | //printf("%i, %i\n", player.x, player.y ); |
joebarhouch | 7:530ca713d2b2 | 126 | } |
joebarhouch | 7:530ca713d2b2 | 127 | } |
joebarhouch | 6:00d20886e4f8 | 128 | } |
joebarhouch | 7:530ca713d2b2 | 129 | |
joebarhouch | 7:530ca713d2b2 | 130 | if(_collide == true){ |
joebarhouch | 7:530ca713d2b2 | 131 | _Ypos = coords[a].y - 8; |
joebarhouch | 7:530ca713d2b2 | 132 | _fall = false; |
joebarhouch | 7:530ca713d2b2 | 133 | _collide = false; |
joebarhouch | 7:530ca713d2b2 | 134 | } |
joebarhouch | 7:530ca713d2b2 | 135 | |
joebarhouch | 6:00d20886e4f8 | 136 | } |
joebarhouch | 3:e4e1cbf750b6 | 137 | |
joebarhouch | 5:928c2eee4109 | 138 |