ELEC2645 (2018/19) / Mbed 2 deprecated EL17MCD

Dependencies:   mbed

Committer:
el17mcd
Date:
Tue Apr 30 09:30:40 2019 +0000
Revision:
18:165e3d49daa8
Parent:
17:cb39d9fa08dc
Child:
21:44e87d88afe2
30/04/19

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17mcd 12:9e6d5d0a0c82 1 /* Tank.cpp
el17mcd 16:a2c945279b79 2 Produces a tank object
el17mcd 16:a2c945279b79 3 and dictates it's movement
el17mcd 7:a3ccabdebe2e 4 1.4.19
el17mcd 7:a3ccabdebe2e 5 */
el17mcd 12:9e6d5d0a0c82 6 #include "Tank.h"
el17mcd 11:4e2eb64031a0 7
el17mcd 12:9e6d5d0a0c82 8 Tank::Tank()
el17mcd 7:a3ccabdebe2e 9 {
el17mcd 17:cb39d9fa08dc 10 _speed = 1;
el17mcd 7:a3ccabdebe2e 11 }
el17mcd 7:a3ccabdebe2e 12
el17mcd 12:9e6d5d0a0c82 13 Tank::~Tank()
el17mcd 7:a3ccabdebe2e 14 {
el17mcd 7:a3ccabdebe2e 15
el17mcd 7:a3ccabdebe2e 16 }
el17mcd 15:fa5282fcd134 17 // Accessors
el17mcd 12:9e6d5d0a0c82 18 int Tank::get_position_x()
el17mcd 7:a3ccabdebe2e 19 {
el17mcd 17:cb39d9fa08dc 20 return _position_x;
el17mcd 7:a3ccabdebe2e 21 }
el17mcd 7:a3ccabdebe2e 22
el17mcd 12:9e6d5d0a0c82 23 int Tank::get_position_y()
el17mcd 7:a3ccabdebe2e 24 {
el17mcd 17:cb39d9fa08dc 25 return _position_y;
el17mcd 7:a3ccabdebe2e 26 }
el17mcd 7:a3ccabdebe2e 27
el17mcd 12:9e6d5d0a0c82 28 int Tank::get_hitbox(int i)
el17mcd 7:a3ccabdebe2e 29 {
el17mcd 17:cb39d9fa08dc 30 return _hitbox[i];
el17mcd 7:a3ccabdebe2e 31 }
el17mcd 7:a3ccabdebe2e 32
el17mcd 12:9e6d5d0a0c82 33 int Tank::get_health()
el17mcd 12:9e6d5d0a0c82 34 {
el17mcd 17:cb39d9fa08dc 35 return _health;
el17mcd 12:9e6d5d0a0c82 36 }
el17mcd 15:fa5282fcd134 37 // Mutators
el17mcd 12:9e6d5d0a0c82 38 void Tank::set_position(int x, int y)
el17mcd 10:d4fb12e9e7cd 39 {
el17mcd 17:cb39d9fa08dc 40 _position_x = x;
el17mcd 17:cb39d9fa08dc 41 _position_y = y;
el17mcd 13:feadff02d3f7 42 }
el17mcd 13:feadff02d3f7 43
el17mcd 17:cb39d9fa08dc 44 void Tank::set_movement_limits(int left, int right) // Determines the boundaries within which
el17mcd 17:cb39d9fa08dc 45 { // a tank can move. Prevents it from leaving the
el17mcd 17:cb39d9fa08dc 46 _left_lim = left; // screen or moving through the central obstacle.
el17mcd 17:cb39d9fa08dc 47 _right_lim = right;
el17mcd 10:d4fb12e9e7cd 48 }
el17mcd 10:d4fb12e9e7cd 49
el17mcd 12:9e6d5d0a0c82 50 void Tank::set_health(int h)
el17mcd 7:a3ccabdebe2e 51 {
el17mcd 17:cb39d9fa08dc 52 _health = h;
el17mcd 7:a3ccabdebe2e 53 }
el17mcd 7:a3ccabdebe2e 54
el17mcd 12:9e6d5d0a0c82 55 void Tank::set_speed(int s)
el17mcd 10:d4fb12e9e7cd 56 {
el17mcd 17:cb39d9fa08dc 57 _speed = s;
el17mcd 10:d4fb12e9e7cd 58 }
el17mcd 16:a2c945279b79 59 // Other Methods
el17mcd 17:cb39d9fa08dc 60 void Tank::move_position(int d) // Governs how the tank moves in the horizontal
el17mcd 18:165e3d49daa8 61 { // x direction. The speed in which it moves is related
el17mcd 18:165e3d49daa8 62 int slowness = 9 - _speed; // to framerate (wait_ms(1000/60), therefore in order to
el17mcd 18:165e3d49daa8 63 int i = _move_counter % slowness; // slow its movement it cannot move on every frame.
el17mcd 17:cb39d9fa08dc 64 if (d > 0) {
el17mcd 17:cb39d9fa08dc 65 _move_counter++; // Movement counter increases if right button is pressed.
el17mcd 17:cb39d9fa08dc 66 if (i == 0 && _position_x < _right_lim) { // i reaches zero every (slowness)number of cycles of the game loop
el17mcd 17:cb39d9fa08dc 67 _position_x++; // provided the tanks is not at a limit it will move when i reaches zero.
el17mcd 13:feadff02d3f7 68 }
el17mcd 17:cb39d9fa08dc 69 } else if (d < 0) {
el17mcd 17:cb39d9fa08dc 70 _move_counter--; // Movement counter decreases if left button is pressed.
el17mcd 17:cb39d9fa08dc 71 if (i == 0 && _position_x > _left_lim) {
el17mcd 17:cb39d9fa08dc 72 _position_x--;
el17mcd 17:cb39d9fa08dc 73 }
el17mcd 17:cb39d9fa08dc 74 }
el17mcd 10:d4fb12e9e7cd 75 }
el17mcd 16:a2c945279b79 76
el17mcd 12:9e6d5d0a0c82 77 void Tank::lose_health()
el17mcd 7:a3ccabdebe2e 78 {
el17mcd 17:cb39d9fa08dc 79 _health--;
el17mcd 7:a3ccabdebe2e 80 }
el17mcd 7:a3ccabdebe2e 81
el17mcd 17:cb39d9fa08dc 82 void Tank::generate_hitbox() // Generates a hitbox based on the x and y position of the
el17mcd 18:165e3d49daa8 83 { // tank's bottom left pixel. The screen is a cartesian plane
el17mcd 18:165e3d49daa8 84 int i = 0; // with the bottom left of the screen representing the origin.
el17mcd 18:165e3d49daa8 85 for (int i0 = 0; i0 < 4; i0++) { // Every pixel on the screen has a coreesponding integer value
el17mcd 18:165e3d49daa8 86 for (int i1 = 1; i1 < 11; i1++) { // assigned to it: the pixel at (0,0) being 1 and the pixel
el17mcd 18:165e3d49daa8 87 _hitbox[i] = (i0 + _position_y) * 84 + _position_x + i1; // (83,47) being 4032. A tank's hitbox is 10 pixels wide and
el17mcd 18:165e3d49daa8 88 i++; // 4 pixels high. The tank's hitbox is described by all the pixels
el17mcd 18:165e3d49daa8 89 } // in this 10x4 area inserted into an array.
el17mcd 17:cb39d9fa08dc 90 }
el17mcd 7:a3ccabdebe2e 91 }