Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Enemy.cpp
00001 #include "Enemy.h" 00002 00003 00004 00005 00006 00007 //enemy walking version A for animation 00008 int enemyWalkA[8][5] = { 00009 { 0,0,1,0,0 }, 00010 { 0,1,0,1,0 }, 00011 { 0,0,1,0,0 }, 00012 { 1,1,1,0,0 }, 00013 { 0,0,1,0,0 }, 00014 { 0,1,1,1,0 }, 00015 { 0,1,0,1,1 }, 00016 { 1,1,0,0,0 }, 00017 }; 00018 00019 //enemy in normal standing state 00020 int enemyNormal[8][5] = { 00021 { 0,0,1,0,0 }, 00022 { 0,1,0,1,0 }, 00023 { 0,0,1,0,0 }, 00024 { 0,1,1,0,0 }, 00025 { 0,0,1,0,0 }, 00026 { 0,0,1,0,0 }, 00027 { 0,0,1,0,0 }, 00028 { 0,1,1,0,0 }, 00029 }; 00030 00031 //enemy dead state 00032 int enemyDead[5][8] = { 00033 { 0,0,0,0,0,0,0,0 }, 00034 { 0,1,0,0,0,0,0,0 }, 00035 { 1,0,1,1,1,1,1,1 }, 00036 { 0,1,0,0,0,0,0,0 }, 00037 { 0,0,0,0,0,0,0,0 }, 00038 }; 00039 00040 //enemy attacking state 1 for animation 00041 int enemyAttack[8][5] = { 00042 { 1,0,1,0,0 }, 00043 { 1,1,0,1,0 }, 00044 { 1,0,1,0,0 }, 00045 { 0,1,1,0,0 }, 00046 { 0,0,1,0,0 }, 00047 { 0,0,1,0,0 }, 00048 { 0,0,1,0,0 }, 00049 { 0,1,1,0,0 }, 00050 }; 00051 00052 //enemy attacking state 2 for animation 00053 int enemyAttack2[8][5] = { 00054 { 0,0,1,0,0 }, 00055 { 0,1,0,1,0 }, 00056 { 0,0,1,0,0 }, 00057 { 1,1,1,0,0 }, 00058 { 0,0,1,0,0 }, 00059 { 0,0,1,0,0 }, 00060 { 0,0,1,0,0 }, 00061 { 0,1,1,0,0 }, 00062 }; 00063 00064 Enemy::Enemy() 00065 { 00066 00067 } 00068 00069 Enemy::~Enemy() 00070 { 00071 00072 } 00073 00074 void Enemy::init(float timeToAttack, float speed) 00075 { 00076 //set current action as waiting 00077 currentAction = waiting; 00078 00079 00080 _x = WIDTH; //set initial position off far right of screen 00081 _attack = false; //set attack flag as false 00082 _alive = true; //set alive flag true 00083 _timeToAttack = timeToAttack; //set time to attack 00084 00085 int yStart = (rand() % (HEIGHT-16)) + 8; // randomise initial y coordinate with offsets so enemies appear fully on screen. 00086 _y = yStart; 00087 00088 00089 _speed = speed; //set enemies speed 00090 00091 } 00092 00093 void Enemy::draw(N5110 &lcd) 00094 { 00095 00096 //switch state decides which version of the sprite to draw based on the the current action 00097 switch(currentAction) 00098 { 00099 case waiting : 00100 { 00101 break; 00102 } 00103 case moving: 00104 { 00105 //animation tick keeps track in ms of time 00106 //modulus by 100 is found then if number is less than half way it will draw one sprite version else it draws another 00107 //creates animation sprite changes every 50ms 00108 if ((int)_animationTick%100 < 50) 00109 { 00110 lcd.drawSprite(_x,_y,8,5,(int *)enemyWalkA); 00111 } else { 00112 lcd.drawSprite(_x,_y,8,5,(int *)enemyNormal); 00113 } 00114 break; 00115 } 00116 case attacking : 00117 { 00118 //animation tick keeps track in ms of time 00119 //modulus by 1000 is found then if number is less than half way it will draw one sprite version else it draws another 00120 //creates animation sprite changes every 500ms 00121 if ((int)_animationTick%1000 < 500) 00122 { 00123 lcd.drawSprite(_x,_y,8,5,(int *)enemyAttack2); 00124 } else { 00125 lcd.drawSprite(_x,_y,8,5,(int *)enemyAttack); 00126 } 00127 break; 00128 } 00129 case dying : 00130 { 00131 //animation tick keeps track in ms of time 00132 //after 1 second it no longer will draw the sprite 00133 if (_animationTick < 1000) 00134 { 00135 lcd.drawSprite(_x-3,_y+5,5,8,(int *)enemyDead); 00136 } 00137 00138 break; 00139 } 00140 } 00141 } 00142 00143 void Enemy::update(int fps) 00144 { 00145 //update animation tick which is used in draw method 00146 _animationTick = _animationTick + (1000.0/fps); 00147 00148 //update the enemy based on which state it is in 00149 switch(currentAction) 00150 { 00151 case waiting : 00152 { 00153 break; 00154 } 00155 case moving: 00156 { 00157 _x-= _speed; //update x position 00158 //code to stop enemy when wall is reached 00159 float xOfWall = (_y - 76)/(-2.8); //equation of bottom line of wall y=mx+c re-arranged 00160 //when wall is reached set state of enemy to attacking 00161 if(_x < xOfWall) { 00162 _x = xOfWall; 00163 currentAction = attacking; 00164 } 00165 break; 00166 } 00167 case attacking : 00168 { 00169 break; 00170 } 00171 case dying : 00172 { 00173 break; 00174 } 00175 } 00176 } 00177 00178 00179 00180 Vector2D Enemy::get_pos() 00181 { 00182 Vector2D p = {_x,_y}; 00183 return p; 00184 } 00185 00186 void Enemy::set_pos(Vector2D p) 00187 { 00188 _x = p.x; 00189 _y = p.y; 00190 } 00191 00192 00193 float Enemy::get_timeToAttack() 00194 { 00195 return _timeToAttack; 00196 } 00197 00198 00199 void Enemy::set_current_action(Action act) { 00200 _animationTick = 0.0; //resets the animation tick when new action begins 00201 currentAction = act; 00202 } 00203 00204 Action Enemy::get_current_action() { 00205 return currentAction; 00206 } 00207 00208 00209 void Enemy::set_alive(bool alive) 00210 { 00211 _alive = alive; 00212 } 00213 00214 bool Enemy::get_alive() 00215 { 00216 return _alive; 00217 } 00218 00219 00220 00221
Generated on Mon Nov 11 2024 22:48:00 by
