ELEC2645 (2018/19) / Mbed 2 deprecated el17rrrs

Dependencies:   mbed Gamepad N5110 mbed-rtos

Committer:
RexRoshan
Date:
Thu May 09 09:49:35 2019 +0000
Revision:
0:d9cf94b41df3
Documentation has been completed and the code has been slightly modified

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RexRoshan 0:d9cf94b41df3 1 #include "Enemy21.h"
RexRoshan 0:d9cf94b41df3 2
RexRoshan 0:d9cf94b41df3 3 // nothing doing in the constructor and destructor
RexRoshan 0:d9cf94b41df3 4 Enemy21::Enemy21()
RexRoshan 0:d9cf94b41df3 5 {
RexRoshan 0:d9cf94b41df3 6
RexRoshan 0:d9cf94b41df3 7 }
RexRoshan 0:d9cf94b41df3 8
RexRoshan 0:d9cf94b41df3 9 Enemy21::~Enemy21()
RexRoshan 0:d9cf94b41df3 10 {
RexRoshan 0:d9cf94b41df3 11
RexRoshan 0:d9cf94b41df3 12 }
RexRoshan 0:d9cf94b41df3 13
RexRoshan 0:d9cf94b41df3 14 // sprite of the first enemy for stage 2
RexRoshan 0:d9cf94b41df3 15 int enemy21 [7][7] ={
RexRoshan 0:d9cf94b41df3 16
RexRoshan 0:d9cf94b41df3 17 {0,0,0,0,0,1,1},
RexRoshan 0:d9cf94b41df3 18 {0,0,0,0,0,1,1},
RexRoshan 0:d9cf94b41df3 19 {0,0,0,0,1,0,0},
RexRoshan 0:d9cf94b41df3 20 {0,0,0,1,0,0,0},
RexRoshan 0:d9cf94b41df3 21 {0,0,1,0,0,0,0},
RexRoshan 0:d9cf94b41df3 22 {1,1,0,0,0,0,0},
RexRoshan 0:d9cf94b41df3 23 {1,1,0,0,0,0,0},
RexRoshan 0:d9cf94b41df3 24
RexRoshan 0:d9cf94b41df3 25 };
RexRoshan 0:d9cf94b41df3 26
RexRoshan 0:d9cf94b41df3 27 void Enemy21::init(int a,int b,int speed) // initialising the x and y position of the enemy and movement speed of the enemy
RexRoshan 0:d9cf94b41df3 28 {
RexRoshan 0:d9cf94b41df3 29
RexRoshan 0:d9cf94b41df3 30 _a = a; // x position of the first enemy for stage 2
RexRoshan 0:d9cf94b41df3 31 _b = b; // y position of the first enemy for stage 2
RexRoshan 0:d9cf94b41df3 32
RexRoshan 0:d9cf94b41df3 33 _health = 0; // start health from zero
RexRoshan 0:d9cf94b41df3 34
RexRoshan 0:d9cf94b41df3 35 int direction = rand() % 2; // randomise initial direction.
RexRoshan 0:d9cf94b41df3 36
RexRoshan 0:d9cf94b41df3 37 // 2 possibilities. Get random modulo and set movement accordingly
RexRoshan 0:d9cf94b41df3 38 if (direction == 0) {
RexRoshan 0:d9cf94b41df3 39 _movement.x = 0;
RexRoshan 0:d9cf94b41df3 40 _movement.y = speed; // move up
RexRoshan 0:d9cf94b41df3 41 } else {
RexRoshan 0:d9cf94b41df3 42 _movement.x = 0;
RexRoshan 0:d9cf94b41df3 43 _movement.y = -speed; // move down
RexRoshan 0:d9cf94b41df3 44 }
RexRoshan 0:d9cf94b41df3 45 }
RexRoshan 0:d9cf94b41df3 46
RexRoshan 0:d9cf94b41df3 47 void Enemy21::enemy2(N5110 &lcd)
RexRoshan 0:d9cf94b41df3 48 {
RexRoshan 0:d9cf94b41df3 49
RexRoshan 0:d9cf94b41df3 50 // Draws the first enemy for stage 2
RexRoshan 0:d9cf94b41df3 51 lcd.drawSprite(_a,_b,7,7,(int *)enemy21);
RexRoshan 0:d9cf94b41df3 52
RexRoshan 0:d9cf94b41df3 53 }
RexRoshan 0:d9cf94b41df3 54
RexRoshan 0:d9cf94b41df3 55 void Enemy21::update()
RexRoshan 0:d9cf94b41df3 56 {
RexRoshan 0:d9cf94b41df3 57 _a += _movement.x; // updates the x position
RexRoshan 0:d9cf94b41df3 58 _b += _movement.y; // updates the y position
RexRoshan 0:d9cf94b41df3 59
RexRoshan 0:d9cf94b41df3 60 }
RexRoshan 0:d9cf94b41df3 61
RexRoshan 0:d9cf94b41df3 62
RexRoshan 0:d9cf94b41df3 63 Vector2D Enemy21::get_movement()
RexRoshan 0:d9cf94b41df3 64 {
RexRoshan 0:d9cf94b41df3 65 // gets the movement of the enemy
RexRoshan 0:d9cf94b41df3 66 Vector2D m = {_movement.x,_movement.y};
RexRoshan 0:d9cf94b41df3 67 return m;
RexRoshan 0:d9cf94b41df3 68 }
RexRoshan 0:d9cf94b41df3 69
RexRoshan 0:d9cf94b41df3 70 void Enemy21::set_movement(Vector2D m)
RexRoshan 0:d9cf94b41df3 71 {
RexRoshan 0:d9cf94b41df3 72 // sets the movement of the enemy
RexRoshan 0:d9cf94b41df3 73 _movement.x = m.x;
RexRoshan 0:d9cf94b41df3 74 _movement.y = m.y;
RexRoshan 0:d9cf94b41df3 75 }
RexRoshan 0:d9cf94b41df3 76
RexRoshan 0:d9cf94b41df3 77 void Enemy21::add_health()
RexRoshan 0:d9cf94b41df3 78 {
RexRoshan 0:d9cf94b41df3 79 // increments the value of health by 1
RexRoshan 0:d9cf94b41df3 80 _health++;
RexRoshan 0:d9cf94b41df3 81 }
RexRoshan 0:d9cf94b41df3 82
RexRoshan 0:d9cf94b41df3 83 int Enemy21::get_health()
RexRoshan 0:d9cf94b41df3 84 {
RexRoshan 0:d9cf94b41df3 85 // gets the value of health
RexRoshan 0:d9cf94b41df3 86 return _health;
RexRoshan 0:d9cf94b41df3 87 }
RexRoshan 0:d9cf94b41df3 88
RexRoshan 0:d9cf94b41df3 89 Vector2D Enemy21::get_enemy21_pos()
RexRoshan 0:d9cf94b41df3 90 {
RexRoshan 0:d9cf94b41df3 91 //gets the position of the first enemy of stage 2
RexRoshan 0:d9cf94b41df3 92 Vector2D e = {_a,_b};
RexRoshan 0:d9cf94b41df3 93 return e;
RexRoshan 0:d9cf94b41df3 94 }
RexRoshan 0:d9cf94b41df3 95
RexRoshan 0:d9cf94b41df3 96 void Enemy21::set_enemy21_pos(Vector2D e)
RexRoshan 0:d9cf94b41df3 97 {
RexRoshan 0:d9cf94b41df3 98 //sets the position of the first enemy of stage 2
RexRoshan 0:d9cf94b41df3 99 _a = e.x;
RexRoshan 0:d9cf94b41df3 100 _b = e.y;
RexRoshan 0:d9cf94b41df3 101 }