Game for 4180 Lab4
Dependencies: 4DGL-uLCD-SE 4180_lab4_tank_war_game Game_Synchronizer MMA8452 SDFileSystem mbed-rtos mbed wave_player
Dependents: 4180_lab4_tank_war_game
Bullet/bullet.cpp@0:ffed9a3bc797, 2017-03-13 (annotated)
- Committer:
- ychen644
- Date:
- Mon Mar 13 21:23:17 2017 +0000
- Revision:
- 0:ffed9a3bc797
Uploading 4180 Lab4 game to Notebook wiki
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ychen644 | 0:ffed9a3bc797 | 1 | #include "uLCD_4DGL.h" |
ychen644 | 0:ffed9a3bc797 | 2 | #include "bullet.h" |
ychen644 | 0:ffed9a3bc797 | 3 | #include "game_synchronizer.h" |
ychen644 | 0:ffed9a3bc797 | 4 | #include "globals.h" |
ychen644 | 0:ffed9a3bc797 | 5 | #include "math.h" |
ychen644 | 0:ffed9a3bc797 | 6 | #include "playSound.h" |
ychen644 | 0:ffed9a3bc797 | 7 | |
ychen644 | 0:ffed9a3bc797 | 8 | extern Serial pc; |
ychen644 | 0:ffed9a3bc797 | 9 | |
ychen644 | 0:ffed9a3bc797 | 10 | extern Game_Synchronizer sync; |
ychen644 | 0:ffed9a3bc797 | 11 | |
ychen644 | 0:ffed9a3bc797 | 12 | // Initialize the bullet. Don't have to do much here. |
ychen644 | 0:ffed9a3bc797 | 13 | // Keep a pointer to this bullet's tank. |
ychen644 | 0:ffed9a3bc797 | 14 | // Set the speed, and default the bullet to not in_flight. |
ychen644 | 0:ffed9a3bc797 | 15 | Bullet::Bullet(Tank* t) { |
ychen644 | 0:ffed9a3bc797 | 16 | tank = t; |
ychen644 | 0:ffed9a3bc797 | 17 | speed = 30; |
ychen644 | 0:ffed9a3bc797 | 18 | powerups = 2; |
ychen644 | 0:ffed9a3bc797 | 19 | power = 0; |
ychen644 | 0:ffed9a3bc797 | 20 | in_flight = false; |
ychen644 | 0:ffed9a3bc797 | 21 | } |
ychen644 | 0:ffed9a3bc797 | 22 | |
ychen644 | 0:ffed9a3bc797 | 23 | // If in_flight, do nothing. Otherwise, |
ychen644 | 0:ffed9a3bc797 | 24 | // set the in_flight flag, and initialize values needed for |
ychen644 | 0:ffed9a3bc797 | 25 | // the trajectory calculations. (x0, y0), (vx0, vy0), time |
ychen644 | 0:ffed9a3bc797 | 26 | // Hint: tank->barrel_end(...) is useful here. |
ychen644 | 0:ffed9a3bc797 | 27 | void Bullet::shoot(void) { |
ychen644 | 0:ffed9a3bc797 | 28 | if (!in_flight) { |
ychen644 | 0:ffed9a3bc797 | 29 | in_flight = true; |
ychen644 | 0:ffed9a3bc797 | 30 | tank->barrel_end(&x0, &y0); |
ychen644 | 0:ffed9a3bc797 | 31 | x = x0; |
ychen644 | 0:ffed9a3bc797 | 32 | y = y0; |
ychen644 | 0:ffed9a3bc797 | 33 | vx0 = speed*cos(tank->barrel_theta); |
ychen644 | 0:ffed9a3bc797 | 34 | vy0 = speed*sin(tank->barrel_theta); |
ychen644 | 0:ffed9a3bc797 | 35 | vx = vx0; |
ychen644 | 0:ffed9a3bc797 | 36 | vy = vy0; |
ychen644 | 0:ffed9a3bc797 | 37 | bounce = 0; |
ychen644 | 0:ffed9a3bc797 | 38 | time = 0; |
ychen644 | 0:ffed9a3bc797 | 39 | } |
ychen644 | 0:ffed9a3bc797 | 40 | } |
ychen644 | 0:ffed9a3bc797 | 41 | |
ychen644 | 0:ffed9a3bc797 | 42 | void Bullet::changeSky(int SKY_COLOR2) { |
ychen644 | 0:ffed9a3bc797 | 43 | SKY_COLOR = SKY_COLOR2; |
ychen644 | 0:ffed9a3bc797 | 44 | } |
ychen644 | 0:ffed9a3bc797 | 45 | |
ychen644 | 0:ffed9a3bc797 | 46 | // power up increases explosion size |
ychen644 | 0:ffed9a3bc797 | 47 | // you get 2 powerups per round |
ychen644 | 0:ffed9a3bc797 | 48 | void Bullet::powerUp(void) { |
ychen644 | 0:ffed9a3bc797 | 49 | if (powerups > 0) { |
ychen644 | 0:ffed9a3bc797 | 50 | // play sound for powerup |
ychen644 | 0:ffed9a3bc797 | 51 | playSound("/sd/wavfiles/boing2.wav"); |
ychen644 | 0:ffed9a3bc797 | 52 | powerups = powerups - 1; |
ychen644 | 0:ffed9a3bc797 | 53 | power = power + 1; |
ychen644 | 0:ffed9a3bc797 | 54 | } |
ychen644 | 0:ffed9a3bc797 | 55 | } |
ychen644 | 0:ffed9a3bc797 | 56 | |
ychen644 | 0:ffed9a3bc797 | 57 | // If the bullet is in flight, calculate its new position |
ychen644 | 0:ffed9a3bc797 | 58 | // after a time delta dt. |
ychen644 | 0:ffed9a3bc797 | 59 | void Bullet::update_position(float dt) { |
ychen644 | 0:ffed9a3bc797 | 60 | vx = vx; |
ychen644 | 0:ffed9a3bc797 | 61 | vy = vy - 9.81*dt; |
ychen644 | 0:ffed9a3bc797 | 62 | x = floor(x0 + vx*time); |
ychen644 | 0:ffed9a3bc797 | 63 | y = floor(y0 + vy*time - 0.5*9.81*time*time); |
ychen644 | 0:ffed9a3bc797 | 64 | } |
ychen644 | 0:ffed9a3bc797 | 65 | |
ychen644 | 0:ffed9a3bc797 | 66 | void boing(void const *argument) { |
ychen644 | 0:ffed9a3bc797 | 67 | playSound("/sd/wavfiles/boing2.wav"); |
ychen644 | 0:ffed9a3bc797 | 68 | } |
ychen644 | 0:ffed9a3bc797 | 69 | |
ychen644 | 0:ffed9a3bc797 | 70 | int Bullet::time_step(float dt) { |
ychen644 | 0:ffed9a3bc797 | 71 | // If the bullet hasn't hit anything, |
ychen644 | 0:ffed9a3bc797 | 72 | // redraw the bullet at its new location. |
ychen644 | 0:ffed9a3bc797 | 73 | // If it has hit something (obstacle, tank, edge of the screen), |
ychen644 | 0:ffed9a3bc797 | 74 | // set the in_flight flag back to false, explode the nearby area, |
ychen644 | 0:ffed9a3bc797 | 75 | // and return one of the following codes. |
ychen644 | 0:ffed9a3bc797 | 76 | // |
ychen644 | 0:ffed9a3bc797 | 77 | // return codes: |
ychen644 | 0:ffed9a3bc797 | 78 | // BULLET_NO_COLLISION: no collision |
ychen644 | 0:ffed9a3bc797 | 79 | // BULLET_OFF_SCREEN: off the side of the screen |
ychen644 | 0:ffed9a3bc797 | 80 | // Otherwise, return the color you've hit in 16bpp format. |
ychen644 | 0:ffed9a3bc797 | 81 | if (in_flight) { |
ychen644 | 0:ffed9a3bc797 | 82 | int x_old = x; |
ychen644 | 0:ffed9a3bc797 | 83 | int y_old = y; |
ychen644 | 0:ffed9a3bc797 | 84 | // old pixel |
ychen644 | 0:ffed9a3bc797 | 85 | sync.pixel(x_old, y_old, SKY_COLOR); |
ychen644 | 0:ffed9a3bc797 | 86 | sync.update(); |
ychen644 | 0:ffed9a3bc797 | 87 | time = time + dt; |
ychen644 | 0:ffed9a3bc797 | 88 | update_position(dt); |
ychen644 | 0:ffed9a3bc797 | 89 | // new pixel |
ychen644 | 0:ffed9a3bc797 | 90 | int color = sync.read_pixel(x, y); |
ychen644 | 0:ffed9a3bc797 | 91 | if (x_old == x && y_old == y) { |
ychen644 | 0:ffed9a3bc797 | 92 | // no change |
ychen644 | 0:ffed9a3bc797 | 93 | sync.pixel(x, y, tank->tank_color); |
ychen644 | 0:ffed9a3bc797 | 94 | return BULLET_NO_COLLISION; |
ychen644 | 0:ffed9a3bc797 | 95 | } else if (x < 0 || x > 128 || y < 0 || y > 128) { |
ychen644 | 0:ffed9a3bc797 | 96 | // out of bounds |
ychen644 | 0:ffed9a3bc797 | 97 | in_flight = false; |
ychen644 | 0:ffed9a3bc797 | 98 | return BULLET_OFF_SCREEN; |
ychen644 | 0:ffed9a3bc797 | 99 | } else if (sync.pixel_eq(color, SKY_COLOR)) { |
ychen644 | 0:ffed9a3bc797 | 100 | // travelling in the sky |
ychen644 | 0:ffed9a3bc797 | 101 | sync.pixel(x, y, tank->tank_color); |
ychen644 | 0:ffed9a3bc797 | 102 | return BULLET_NO_COLLISION; |
ychen644 | 0:ffed9a3bc797 | 103 | } else if (!sync.pixel_eq(color, SKY_COLOR)) { |
ychen644 | 0:ffed9a3bc797 | 104 | // hit |
ychen644 | 0:ffed9a3bc797 | 105 | if (sync.pixel_eq(color, GND_COLOR)) { |
ychen644 | 0:ffed9a3bc797 | 106 | bounce = bounce + 1; |
ychen644 | 0:ffed9a3bc797 | 107 | // bounce |
ychen644 | 0:ffed9a3bc797 | 108 | if (bounce < 3) { |
ychen644 | 0:ffed9a3bc797 | 109 | vy = vy/2.0; |
ychen644 | 0:ffed9a3bc797 | 110 | sync.pixel(x, y, tank->tank_color); |
ychen644 | 0:ffed9a3bc797 | 111 | return BULLET_NO_COLLISION; |
ychen644 | 0:ffed9a3bc797 | 112 | } |
ychen644 | 0:ffed9a3bc797 | 113 | } |
ychen644 | 0:ffed9a3bc797 | 114 | // hit |
ychen644 | 0:ffed9a3bc797 | 115 | in_flight = false; |
ychen644 | 0:ffed9a3bc797 | 116 | int i = 1; |
ychen644 | 0:ffed9a3bc797 | 117 | // powerup increases explosion size |
ychen644 | 0:ffed9a3bc797 | 118 | for(i = 1; i < 5 + (power * 3); i++) { |
ychen644 | 0:ffed9a3bc797 | 119 | sync.filled_rectangle(x-i, y-i, x+i, y+i, tank->tank_color); |
ychen644 | 0:ffed9a3bc797 | 120 | sync.update(); |
ychen644 | 0:ffed9a3bc797 | 121 | wait(0.1); |
ychen644 | 0:ffed9a3bc797 | 122 | } |
ychen644 | 0:ffed9a3bc797 | 123 | power = 0; |
ychen644 | 0:ffed9a3bc797 | 124 | // make everything the sky afterwards |
ychen644 | 0:ffed9a3bc797 | 125 | sync.filled_rectangle(x-i-1, y-i-1, x+i-1, y+i-1, SKY_COLOR); |
ychen644 | 0:ffed9a3bc797 | 126 | sync.update(); |
ychen644 | 0:ffed9a3bc797 | 127 | // play the sound |
ychen644 | 0:ffed9a3bc797 | 128 | playSound("/sd/wavfiles/splooge.wav"); |
ychen644 | 0:ffed9a3bc797 | 129 | return color; |
ychen644 | 0:ffed9a3bc797 | 130 | } |
ychen644 | 0:ffed9a3bc797 | 131 | } |
ychen644 | 0:ffed9a3bc797 | 132 | return BULLET_NO_COLLISION; |
ychen644 | 0:ffed9a3bc797 | 133 | } |