Hao zhang
/
el17h3z
HaoZhang SID: 201199702
Bullet/Bullet.cpp@2:867fdea920c1, 2020-05-14 (annotated)
- Committer:
- zh870524589
- Date:
- Thu May 14 17:45:05 2020 +0000
- Revision:
- 2:867fdea920c1
- Parent:
- 0:45ce241d316b
final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zh870524589 | 0:45ce241d316b | 1 | #include "Bullet.h" |
zh870524589 | 0:45ce241d316b | 2 | #include "iostream" |
zh870524589 | 0:45ce241d316b | 3 | |
zh870524589 | 0:45ce241d316b | 4 | Bullet::Bullet() {} |
zh870524589 | 0:45ce241d316b | 5 | |
zh870524589 | 0:45ce241d316b | 6 | Bullet::Bullet(Gamepad &pad,Vector2D p) |
zh870524589 | 0:45ce241d316b | 7 | { |
zh870524589 | 0:45ce241d316b | 8 | this->m_d = pad.get_direction(); |
zh870524589 | 0:45ce241d316b | 9 | this->_x = p.x; |
zh870524589 | 0:45ce241d316b | 10 | this->_y = p.y; |
zh870524589 | 0:45ce241d316b | 11 | |
zh870524589 | 0:45ce241d316b | 12 | } |
zh870524589 | 0:45ce241d316b | 13 | |
zh870524589 | 0:45ce241d316b | 14 | void Bullet::update(N5110 &lcd,Direction prem_d) |
zh870524589 | 0:45ce241d316b | 15 | { |
zh870524589 | 0:45ce241d316b | 16 | v1.push_back(prem_d); |
zh870524589 | 0:45ce241d316b | 17 | if(m_d == CENTRE) |
zh870524589 | 0:45ce241d316b | 18 | { |
zh870524589 | 0:45ce241d316b | 19 | if (v1[0] == S) { |
zh870524589 | 0:45ce241d316b | 20 | this->_y+=2; |
zh870524589 | 0:45ce241d316b | 21 | lcd.setPixel(_x+2,_y+4,true); |
zh870524589 | 0:45ce241d316b | 22 | |
zh870524589 | 0:45ce241d316b | 23 | |
zh870524589 | 0:45ce241d316b | 24 | }else if (v1[0] == N) { |
zh870524589 | 0:45ce241d316b | 25 | this->_y-=2; |
zh870524589 | 0:45ce241d316b | 26 | lcd.setPixel(_x+2,_y,true); |
zh870524589 | 0:45ce241d316b | 27 | |
zh870524589 | 0:45ce241d316b | 28 | } else if (v1[0] == E || v1[0] == SE ||v1[0] == NE) { |
zh870524589 | 0:45ce241d316b | 29 | this->_x+=2; |
zh870524589 | 0:45ce241d316b | 30 | lcd.setPixel(_x+4,_y+2,true); |
zh870524589 | 0:45ce241d316b | 31 | |
zh870524589 | 0:45ce241d316b | 32 | |
zh870524589 | 0:45ce241d316b | 33 | |
zh870524589 | 0:45ce241d316b | 34 | } else if (v1[0] == W || v1[0] == SW || v1[0] == NW) { |
zh870524589 | 0:45ce241d316b | 35 | this->_x-=2; |
zh870524589 | 0:45ce241d316b | 36 | lcd.setPixel(_x,_y+2,true); |
zh870524589 | 0:45ce241d316b | 37 | } |
zh870524589 | 0:45ce241d316b | 38 | } else if (m_d == S) { |
zh870524589 | 0:45ce241d316b | 39 | this->_y+=2; |
zh870524589 | 0:45ce241d316b | 40 | lcd.setPixel(_x+2,_y+4,true); |
zh870524589 | 0:45ce241d316b | 41 | |
zh870524589 | 0:45ce241d316b | 42 | } else if (m_d == N) { |
zh870524589 | 0:45ce241d316b | 43 | this->_y-=2; |
zh870524589 | 0:45ce241d316b | 44 | lcd.setPixel(_x+2,_y,true); |
zh870524589 | 0:45ce241d316b | 45 | |
zh870524589 | 0:45ce241d316b | 46 | } else if (m_d == E || m_d == SE || m_d == NE) { |
zh870524589 | 0:45ce241d316b | 47 | this->_x+=2; |
zh870524589 | 0:45ce241d316b | 48 | lcd.setPixel(_x+4,_y+2,true); |
zh870524589 | 0:45ce241d316b | 49 | |
zh870524589 | 0:45ce241d316b | 50 | } else if (m_d == W || m_d == SW || m_d == NW) { |
zh870524589 | 0:45ce241d316b | 51 | this->_x-=2; |
zh870524589 | 0:45ce241d316b | 52 | lcd.setPixel(_x,_y+2,true); |
zh870524589 | 0:45ce241d316b | 53 | } |
zh870524589 | 0:45ce241d316b | 54 | } |
zh870524589 | 0:45ce241d316b | 55 | |
zh870524589 | 0:45ce241d316b | 56 | void Bullet::et_update(N5110 &lcd,Direction e_d) |
zh870524589 | 0:45ce241d316b | 57 | { |
zh870524589 | 0:45ce241d316b | 58 | v2.push_back(e_d); |
zh870524589 | 0:45ce241d316b | 59 | if (v2[0]== S) { |
zh870524589 | 0:45ce241d316b | 60 | this->_y+=2; |
zh870524589 | 0:45ce241d316b | 61 | lcd.setPixel(_x+2,_y+4,true); |
zh870524589 | 0:45ce241d316b | 62 | |
zh870524589 | 0:45ce241d316b | 63 | } else if (v2[0]== N) { |
zh870524589 | 0:45ce241d316b | 64 | this->_y-=2; |
zh870524589 | 0:45ce241d316b | 65 | lcd.setPixel(_x+2,_y,true); |
zh870524589 | 0:45ce241d316b | 66 | |
zh870524589 | 0:45ce241d316b | 67 | } else if (v2[0] == E || v2[0] == SE || v2[0] == NE) { |
zh870524589 | 0:45ce241d316b | 68 | this->_x+=2; |
zh870524589 | 0:45ce241d316b | 69 | lcd.setPixel(_x+4,_y+2,true); |
zh870524589 | 0:45ce241d316b | 70 | |
zh870524589 | 0:45ce241d316b | 71 | } else if (v2[0] == W || v2[0] == SW || v2[0] == NW) { |
zh870524589 | 0:45ce241d316b | 72 | this->_x-=2; |
zh870524589 | 0:45ce241d316b | 73 | lcd.setPixel(_x,_y+2,true); |
zh870524589 | 0:45ce241d316b | 74 | } |
zh870524589 | 0:45ce241d316b | 75 | } |
zh870524589 | 0:45ce241d316b | 76 | |
zh870524589 | 0:45ce241d316b | 77 | |
zh870524589 | 0:45ce241d316b | 78 | Vector2D Bullet::get_pos() |
zh870524589 | 0:45ce241d316b | 79 | { |
zh870524589 | 0:45ce241d316b | 80 | Vector2D p = {_x,_y}; |
zh870524589 | 0:45ce241d316b | 81 | return p; |
zh870524589 | 0:45ce241d316b | 82 | } |
zh870524589 | 0:45ce241d316b | 83 | |
zh870524589 | 0:45ce241d316b | 84 | void Bullet::set_pos(int x,int y) |
zh870524589 | 0:45ce241d316b | 85 | { |
zh870524589 | 0:45ce241d316b | 86 | this->_x = x; |
zh870524589 | 0:45ce241d316b | 87 | this->_y = y; |
zh870524589 | 0:45ce241d316b | 88 | |
zh870524589 | 0:45ce241d316b | 89 | } |
zh870524589 | 0:45ce241d316b | 90 | |
zh870524589 | 0:45ce241d316b | 91 | |
zh870524589 | 0:45ce241d316b | 92 | bool Bullet::destroy() |
zh870524589 | 0:45ce241d316b | 93 | { |
zh870524589 | 0:45ce241d316b | 94 | //check if the bullet go out of the screen |
zh870524589 | 0:45ce241d316b | 95 | if(this->_x<0||this->_x>84||this->_y>48||this->_y<0) |
zh870524589 | 0:45ce241d316b | 96 | return true; |
zh870524589 | 0:45ce241d316b | 97 | |
zh870524589 | 0:45ce241d316b | 98 | else |
zh870524589 | 0:45ce241d316b | 99 | return false; |
zh870524589 | 0:45ce241d316b | 100 | } |
zh870524589 | 0:45ce241d316b | 101 | |
zh870524589 | 0:45ce241d316b | 102 | bool Bullet::destroyT(Vector2D t_pos) |
zh870524589 | 0:45ce241d316b | 103 | { |
zh870524589 | 0:45ce241d316b | 104 | if((this->_x >= t_pos.x && this->_x<= t_pos.x+2) && (this->_y >= t_pos.y &&this->_y <=t_pos.y+2) |
zh870524589 | 0:45ce241d316b | 105 | ||(this->_x +2 >= t_pos.x && this->_x <= t_pos.x) && (this->_y >= t_pos.y && this->_y <=t_pos.y+2) |
zh870524589 | 0:45ce241d316b | 106 | || (this->_x >= t_pos.x && this->_x <= t_pos.x+2) && (this->_y +2>= t_pos.y && this->_y <=t_pos.y) |
zh870524589 | 0:45ce241d316b | 107 | ||(this->_x +2 >= t_pos.x && this->_x<= t_pos.x) &&(this->_y +2>= t_pos.y && this->_y <=t_pos.y)) |
zh870524589 | 0:45ce241d316b | 108 | return true; |
zh870524589 | 0:45ce241d316b | 109 | |
zh870524589 | 0:45ce241d316b | 110 | else |
zh870524589 | 0:45ce241d316b | 111 | return false; |
zh870524589 | 0:45ce241d316b | 112 | } |
zh870524589 | 0:45ce241d316b | 113 | |
zh870524589 | 0:45ce241d316b | 114 | void Bullet::bullet_collison(list<Bullet*> &lstBullets,list<Bullet*>&mtbullet) |
zh870524589 | 0:45ce241d316b | 115 | { |
zh870524589 | 0:45ce241d316b | 116 | |
zh870524589 | 0:45ce241d316b | 117 | for(list<Bullet*>::iterator it = lstBullets.begin(); it != lstBullets.end();it++){ |
zh870524589 | 0:45ce241d316b | 118 | Vector2D other_pos = (*it)->get_pos(); |
zh870524589 | 0:45ce241d316b | 119 | for(list<Bullet*>::iterator mit = mtbullet.begin(); mit != mtbullet.end();mit++){ |
zh870524589 | 0:45ce241d316b | 120 | Vector2D m_pos = (*mit)->get_pos(); |
zh870524589 | 0:45ce241d316b | 121 | |
zh870524589 | 0:45ce241d316b | 122 | if(m_pos.x ==other_pos.x && m_pos.y ==other_pos.y) |
zh870524589 | 0:45ce241d316b | 123 | { |
zh870524589 | 0:45ce241d316b | 124 | (*it)->set_pos(-12,-12); |
zh870524589 | 0:45ce241d316b | 125 | (*mit)->set_pos(-12,-12); |
zh870524589 | 0:45ce241d316b | 126 | |
zh870524589 | 0:45ce241d316b | 127 | }else if ((v2[0]== S || v2[0]== N)&&(m_pos.x ==other_pos.x )){ |
zh870524589 | 0:45ce241d316b | 128 | |
zh870524589 | 0:45ce241d316b | 129 | if(m_pos.y ==other_pos.y+1|| m_pos.y ==other_pos.y+2) |
zh870524589 | 0:45ce241d316b | 130 | { |
zh870524589 | 0:45ce241d316b | 131 | (*it)->set_pos(-12,-12); |
zh870524589 | 0:45ce241d316b | 132 | (*mit)->set_pos(-12,-12); |
zh870524589 | 0:45ce241d316b | 133 | |
zh870524589 | 0:45ce241d316b | 134 | } else if ( m_pos.y ==other_pos.y-1||m_pos.y ==other_pos.y-2) { |
zh870524589 | 0:45ce241d316b | 135 | |
zh870524589 | 0:45ce241d316b | 136 | (*it)->set_pos(-12,-12); |
zh870524589 | 0:45ce241d316b | 137 | (*mit)->set_pos(-12,-12); |
zh870524589 | 0:45ce241d316b | 138 | } |
zh870524589 | 0:45ce241d316b | 139 | } else if ((v2[0]== W ||v2[0]== E)&& (m_pos.y ==other_pos.y)){ |
zh870524589 | 0:45ce241d316b | 140 | if(m_pos.x ==other_pos.x+1|| m_pos.x ==other_pos.x+2) { |
zh870524589 | 0:45ce241d316b | 141 | (*it)->set_pos(-12,-12); |
zh870524589 | 0:45ce241d316b | 142 | (*mit)->set_pos(-12,-12); |
zh870524589 | 0:45ce241d316b | 143 | } else if ( m_pos.x ==other_pos.x-1||m_pos.x ==other_pos.x-2){ |
zh870524589 | 0:45ce241d316b | 144 | |
zh870524589 | 0:45ce241d316b | 145 | (*it)->set_pos(-12,-12); |
zh870524589 | 0:45ce241d316b | 146 | (*mit)->set_pos(-12,-12); |
zh870524589 | 0:45ce241d316b | 147 | } |
zh870524589 | 0:45ce241d316b | 148 | } |
zh870524589 | 0:45ce241d316b | 149 | } |
zh870524589 | 0:45ce241d316b | 150 | } |
zh870524589 | 0:45ce241d316b | 151 | } |