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 FATFileSystem
CaMove/CaMove.cpp@17:7d4d8905b608, 2019-05-08 (annotated)
- Committer:
- rottenegg
- Date:
- Wed May 08 00:30:09 2019 +0000
- Revision:
- 17:7d4d8905b608
- Parent:
- 15:3d29fb195958
- Child:
- 18:14e5391beccf
All Libraries: Huge Bug due to previous changes that causes unexpected crash.; WDplayer: Tuned to meet 20 lines per function criteria;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rottenegg | 4:34bf3587cf42 | 1 | #include "CaMove.h" |
rottenegg | 8:e3a76a808a4c | 2 | #include "AniPaths.h" |
rottenegg | 5:860087ff295e | 3 | //Constructor |
rottenegg | 5:860087ff295e | 4 | CaMove::CaMove(PinName Button, |
rottenegg | 5:860087ff295e | 5 | PinName Pot_h, |
rottenegg | 5:860087ff295e | 6 | PinName Pot_v) |
rottenegg | 5:860087ff295e | 7 | : //Creating Class Objects |
rottenegg | 5:860087ff295e | 8 | _check(new InterruptIn(Button)), |
rottenegg | 5:860087ff295e | 9 | _hoz(new AnalogIn(Pot_h)), |
rottenegg | 5:860087ff295e | 10 | _ver(new AnalogIn(Pot_v)) |
rottenegg | 5:860087ff295e | 11 | { |
rottenegg | 5:860087ff295e | 12 | _check->mode(PullDown); |
rottenegg | 5:860087ff295e | 13 | _check->rise(callback(this,&CaMove::intercheck)); |
rottenegg | 4:34bf3587cf42 | 14 | }; |
rottenegg | 5:860087ff295e | 15 | //Destructor |
rottenegg | 4:34bf3587cf42 | 16 | CaMove::~CaMove() { |
rottenegg | 5:860087ff295e | 17 | delete _check; |
rottenegg | 5:860087ff295e | 18 | delete _hoz; |
rottenegg | 5:860087ff295e | 19 | delete _ver; |
rottenegg | 4:34bf3587cf42 | 20 | }; |
rottenegg | 4:34bf3587cf42 | 21 | |
rottenegg | 5:860087ff295e | 22 | void CaMove::init(int x,int y,Direction D) { |
rottenegg | 5:860087ff295e | 23 | _h = x; |
rottenegg | 5:860087ff295e | 24 | _v = y; |
rottenegg | 5:860087ff295e | 25 | _itr = 0; |
rottenegg | 5:860087ff295e | 26 | _fc = D; |
rottenegg | 5:860087ff295e | 27 | _trg = false; |
rottenegg | 5:860087ff295e | 28 | _treg = -1; |
rottenegg | 5:860087ff295e | 29 | }; |
rottenegg | 5:860087ff295e | 30 | |
rottenegg | 17:7d4d8905b608 | 31 | void CaMove::move(Bitmap &lcd) { |
rottenegg | 5:860087ff295e | 32 | //If statements to check joystick movement and has in bluilt collsion detection |
rottenegg | 5:860087ff295e | 33 | //collsion detection check 4 sides for black pixels |
rottenegg | 9:ac396c818874 | 34 | bool c1 = (lcd.getPixel((_h-1),(_v+12)) == 0 && lcd.getPixel((_h-1),(_v+1)) == 0) ; |
rottenegg | 9:ac396c818874 | 35 | bool c2 = (lcd.getPixel((_h+10),(_v+12)) == 0 && lcd.getPixel((_h+10),(_v+1)) == 0); |
rottenegg | 9:ac396c818874 | 36 | bool c3 = (lcd.getPixel((_h+1),(_v-1)) == 0 && lcd.getPixel((_h+8),(_v-1)) == 0); |
rottenegg | 9:ac396c818874 | 37 | bool c4 = (lcd.getPixel((_h+1),(_v+14)) == 0 && lcd.getPixel((_h+8),(_v+14)) == 0); |
rottenegg | 9:ac396c818874 | 38 | if (_hoz->read() > 0.6f && c1) { |
rottenegg | 4:34bf3587cf42 | 39 | _h = _h - 2; |
rottenegg | 5:860087ff295e | 40 | _fc = Lt; |
rottenegg | 4:34bf3587cf42 | 41 | } |
rottenegg | 9:ac396c818874 | 42 | if (_hoz->read() < 0.4f && c2) { |
rottenegg | 4:34bf3587cf42 | 43 | _h = _h + 2; |
rottenegg | 5:860087ff295e | 44 | _fc = Rt; |
rottenegg | 4:34bf3587cf42 | 45 | } |
rottenegg | 9:ac396c818874 | 46 | if (_ver->read() > 0.6f && c3) { |
rottenegg | 4:34bf3587cf42 | 47 | _v = _v - 2; |
rottenegg | 5:860087ff295e | 48 | _fc = Fd; |
rottenegg | 4:34bf3587cf42 | 49 | } |
rottenegg | 9:ac396c818874 | 50 | if (_ver->read() < 0.4f && c4) { |
rottenegg | 4:34bf3587cf42 | 51 | _v = _v + 2; |
rottenegg | 5:860087ff295e | 52 | _fc = Bd; |
rottenegg | 4:34bf3587cf42 | 53 | } |
rottenegg | 4:34bf3587cf42 | 54 | if (_itr == 3) { |
rottenegg | 4:34bf3587cf42 | 55 | _itr = -1; |
rottenegg | 4:34bf3587cf42 | 56 | } |
rottenegg | 4:34bf3587cf42 | 57 | _itr++; |
rottenegg | 9:ac396c818874 | 58 | this->rend(lcd); |
rottenegg | 9:ac396c818874 | 59 | } |
rottenegg | 9:ac396c818874 | 60 | |
rottenegg | 9:ac396c818874 | 61 | |
rottenegg | 17:7d4d8905b608 | 62 | void CaMove::rend(Bitmap &lcd) { |
rottenegg | 5:860087ff295e | 63 | //Switch case allows charater to move diagonally and retains its facing direction when stationary |
rottenegg | 5:860087ff295e | 64 | switch (_fc) { |
rottenegg | 4:34bf3587cf42 | 65 | case Lt: |
rottenegg | 17:7d4d8905b608 | 66 | lcd.renderBMP(L[_itr],_h,_v); |
rottenegg | 4:34bf3587cf42 | 67 | break; |
rottenegg | 4:34bf3587cf42 | 68 | |
rottenegg | 4:34bf3587cf42 | 69 | case Rt: |
rottenegg | 17:7d4d8905b608 | 70 | lcd.renderBMP(R[_itr],_h,_v); |
rottenegg | 4:34bf3587cf42 | 71 | break; |
rottenegg | 4:34bf3587cf42 | 72 | |
rottenegg | 4:34bf3587cf42 | 73 | case Fd: |
rottenegg | 17:7d4d8905b608 | 74 | lcd.renderBMP(F[_itr],_h,_v); |
rottenegg | 4:34bf3587cf42 | 75 | break; |
rottenegg | 4:34bf3587cf42 | 76 | |
rottenegg | 4:34bf3587cf42 | 77 | case Bd: |
rottenegg | 17:7d4d8905b608 | 78 | lcd.renderBMP(B[_itr],_h,_v); |
rottenegg | 4:34bf3587cf42 | 79 | break; |
rottenegg | 4:34bf3587cf42 | 80 | } |
rottenegg | 4:34bf3587cf42 | 81 | }; |
rottenegg | 4:34bf3587cf42 | 82 | |
rottenegg | 4:34bf3587cf42 | 83 | |
rottenegg | 5:860087ff295e | 84 | bool CaMove::in_screen() { |
rottenegg | 9:ac396c818874 | 85 | if (_h + 5 >= 84 || (_h + 5) <= 0 || _v + 7 >= 48 || (_v + 8) <= 0) { |
rottenegg | 5:860087ff295e | 86 | return false; |
rottenegg | 5:860087ff295e | 87 | } else { |
rottenegg | 4:34bf3587cf42 | 88 | return true; |
rottenegg | 4:34bf3587cf42 | 89 | } |
rottenegg | 4:34bf3587cf42 | 90 | }; |
rottenegg | 5:860087ff295e | 91 | //Sets values into vector |
rottenegg | 5:860087ff295e | 92 | void CaMove::set_region(int xmin, int ymin, int xl, int yl) { |
rottenegg | 5:860087ff295e | 93 | _vreg.push_back((xmin + xl)); |
rottenegg | 5:860087ff295e | 94 | _vreg.push_back(xmin); |
rottenegg | 5:860087ff295e | 95 | _vreg.push_back((ymin + yl)); |
rottenegg | 5:860087ff295e | 96 | _vreg.push_back(ymin); |
rottenegg | 4:34bf3587cf42 | 97 | }; |
rottenegg | 4:34bf3587cf42 | 98 | |
rottenegg | 4:34bf3587cf42 | 99 | void CaMove::delete_regions() { |
rottenegg | 5:860087ff295e | 100 | _vreg.clear(); |
rottenegg | 4:34bf3587cf42 | 101 | }; |
rottenegg | 4:34bf3587cf42 | 102 | |
rottenegg | 4:34bf3587cf42 | 103 | void CaMove::intercheck() { |
rottenegg | 5:860087ff295e | 104 | _trg = false; |
rottenegg | 5:860087ff295e | 105 | if (_vreg.size() > 0) { |
rottenegg | 5:860087ff295e | 106 | for(int Vvalue = 0; Vvalue < _vreg.size(); Vvalue = Vvalue +4) { |
rottenegg | 5:860087ff295e | 107 | //Function only will check the facing side for interative regions |
rottenegg | 5:860087ff295e | 108 | //If Statements check that sides position with all x- y region ranges to see if charater has hit that region. |
rottenegg | 5:860087ff295e | 109 | bool LowerH = (_h - 1) <= _vreg[Vvalue] && (_h-1) >= _vreg[Vvalue + 1]; |
rottenegg | 5:860087ff295e | 110 | bool UpperH = (_h + 10) <= _vreg[Vvalue] && (_h + 10) >= _vreg[Vvalue + 1]; |
rottenegg | 5:860087ff295e | 111 | bool MidH = (_h + 5) <= _vreg[Vvalue] && (_h + 5) >= _vreg[Vvalue + 1]; |
rottenegg | 5:860087ff295e | 112 | bool LowerV = (_v - 1) >= _vreg[Vvalue + 3] && (_v - 1) <= _vreg[Vvalue + 2]; |
rottenegg | 5:860087ff295e | 113 | bool UpperV = (_v+14) >= _vreg[Vvalue + 3] && (_v + 14) <= _vreg[Vvalue + 2]; |
rottenegg | 5:860087ff295e | 114 | bool MidV = (_v+7) >= _vreg[Vvalue + 3] && (_v + 7) <= _vreg[Vvalue + 2]; |
rottenegg | 5:860087ff295e | 115 | //The region number is found depending on its position in the vector |
rottenegg | 5:860087ff295e | 116 | //e.g elements 0 to 3 are in region 0 and elements 4 to 7 are in region 1 |
rottenegg | 5:860087ff295e | 117 | switch (_fc) { |
rottenegg | 4:34bf3587cf42 | 118 | case Lt: //L |
rottenegg | 4:34bf3587cf42 | 119 | if (LowerH && MidV) { |
rottenegg | 5:860087ff295e | 120 | _trg = true; |
rottenegg | 5:860087ff295e | 121 | _treg = Vvalue / 4; |
rottenegg | 4:34bf3587cf42 | 122 | }; |
rottenegg | 4:34bf3587cf42 | 123 | break; |
rottenegg | 4:34bf3587cf42 | 124 | |
rottenegg | 4:34bf3587cf42 | 125 | case Rt: //R |
rottenegg | 4:34bf3587cf42 | 126 | if (UpperH && MidV) { |
rottenegg | 5:860087ff295e | 127 | _trg = true; |
rottenegg | 5:860087ff295e | 128 | _treg = Vvalue / 4; |
rottenegg | 4:34bf3587cf42 | 129 | } |
rottenegg | 4:34bf3587cf42 | 130 | break; |
rottenegg | 4:34bf3587cf42 | 131 | |
rottenegg | 4:34bf3587cf42 | 132 | case Fd: //F |
rottenegg | 4:34bf3587cf42 | 133 | if (MidH && LowerV) { |
rottenegg | 5:860087ff295e | 134 | _trg = true; |
rottenegg | 5:860087ff295e | 135 | _treg = Vvalue / 4; |
rottenegg | 4:34bf3587cf42 | 136 | } |
rottenegg | 4:34bf3587cf42 | 137 | break; |
rottenegg | 4:34bf3587cf42 | 138 | |
rottenegg | 4:34bf3587cf42 | 139 | case Bd: //B |
rottenegg | 4:34bf3587cf42 | 140 | if (MidH && UpperV) { |
rottenegg | 5:860087ff295e | 141 | _trg = true; |
rottenegg | 5:860087ff295e | 142 | _treg = Vvalue / 4; |
rottenegg | 4:34bf3587cf42 | 143 | } |
rottenegg | 4:34bf3587cf42 | 144 | break; |
rottenegg | 4:34bf3587cf42 | 145 | } |
rottenegg | 4:34bf3587cf42 | 146 | } |
rottenegg | 4:34bf3587cf42 | 147 | } |
rottenegg | 5:860087ff295e | 148 | }; |
rottenegg | 5:860087ff295e | 149 | //Functions revert to default values evrytime they are called. |
rottenegg | 5:860087ff295e | 150 | bool CaMove::is_trg() { |
rottenegg | 5:860087ff295e | 151 | bool temp = _trg; |
rottenegg | 5:860087ff295e | 152 | _trg = false; |
rottenegg | 4:34bf3587cf42 | 153 | return temp; |
rottenegg | 4:34bf3587cf42 | 154 | |
rottenegg | 4:34bf3587cf42 | 155 | }; |
rottenegg | 5:860087ff295e | 156 | |
rottenegg | 5:860087ff295e | 157 | int CaMove::get_treg() { |
rottenegg | 5:860087ff295e | 158 | int temp = _treg; |
rottenegg | 5:860087ff295e | 159 | _treg = -1; |
rottenegg | 5:860087ff295e | 160 | return temp; |
rottenegg | 5:860087ff295e | 161 | } |
rottenegg | 5:860087ff295e | 162 | |
rottenegg | 9:ac396c818874 | 163 | //AI Components |
rottenegg | 9:ac396c818874 | 164 | void CaMove::AIinit() { |
rottenegg | 9:ac396c818874 | 165 | enabled = false; |
rottenegg | 10:4fda7b01484a | 166 | _ch = 0; |
rottenegg | 10:4fda7b01484a | 167 | _cv = 0; |
rottenegg | 9:ac396c818874 | 168 | }; |
rottenegg | 9:ac396c818874 | 169 | |
rottenegg | 9:ac396c818874 | 170 | void CaMove::spawn(int x, int y) { |
rottenegg | 9:ac396c818874 | 171 | enabled = true; |
rottenegg | 9:ac396c818874 | 172 | _ch = x; |
rottenegg | 9:ac396c818874 | 173 | _cv = y; |
rottenegg | 9:ac396c818874 | 174 | } |
rottenegg | 9:ac396c818874 | 175 | |
rottenegg | 17:7d4d8905b608 | 176 | void CaMove::chase(Bitmap &lcd, int girl) { |
rottenegg | 10:4fda7b01484a | 177 | int increment; |
rottenegg | 14:165fff6d1854 | 178 | switch (girl) { |
rottenegg | 14:165fff6d1854 | 179 | case 2: |
rottenegg | 13:95323e0f2fcb | 180 | increment = 2; |
rottenegg | 14:165fff6d1854 | 181 | break; |
rottenegg | 14:165fff6d1854 | 182 | case 1: |
rottenegg | 14:165fff6d1854 | 183 | increment = 0; |
rottenegg | 14:165fff6d1854 | 184 | break; |
rottenegg | 15:3d29fb195958 | 185 | case 4: |
rottenegg | 15:3d29fb195958 | 186 | increment = 5; |
rottenegg | 15:3d29fb195958 | 187 | break; |
rottenegg | 14:165fff6d1854 | 188 | default: |
rottenegg | 10:4fda7b01484a | 189 | increment = 3; |
rottenegg | 14:165fff6d1854 | 190 | break; |
rottenegg | 10:4fda7b01484a | 191 | } |
rottenegg | 9:ac396c818874 | 192 | if (enabled) { |
rottenegg | 9:ac396c818874 | 193 | if (_cv < _v) { |
rottenegg | 10:4fda7b01484a | 194 | _cv = _cv + increment; |
rottenegg | 9:ac396c818874 | 195 | } else if (_cv > _v) { |
rottenegg | 10:4fda7b01484a | 196 | _cv = _cv - increment; |
rottenegg | 13:95323e0f2fcb | 197 | } |
rottenegg | 13:95323e0f2fcb | 198 | if (_ch < _h) { |
rottenegg | 13:95323e0f2fcb | 199 | _ch = _ch + increment; |
rottenegg | 15:3d29fb195958 | 200 | } else if (_ch > _h) { |
rottenegg | 13:95323e0f2fcb | 201 | _ch = _ch - increment; |
rottenegg | 9:ac396c818874 | 202 | } |
rottenegg | 14:165fff6d1854 | 203 | switch (girl) { |
rottenegg | 14:165fff6d1854 | 204 | case 2: |
rottenegg | 17:7d4d8905b608 | 205 | lcd.renderBMP(AIC[_itr],_ch,_cv); |
rottenegg | 14:165fff6d1854 | 206 | break; |
rottenegg | 15:3d29fb195958 | 207 | case 4: |
rottenegg | 17:7d4d8905b608 | 208 | lcd.renderBMP(AIC[_itr],_ch,_cv); // change later |
rottenegg | 14:165fff6d1854 | 209 | break; |
rottenegg | 14:165fff6d1854 | 210 | default: |
rottenegg | 17:7d4d8905b608 | 211 | lcd.renderBMP(AIM[_itr],_ch,_cv); |
rottenegg | 14:165fff6d1854 | 212 | break; |
rottenegg | 10:4fda7b01484a | 213 | } |
rottenegg | 9:ac396c818874 | 214 | } |
rottenegg | 9:ac396c818874 | 215 | } |
rottenegg | 9:ac396c818874 | 216 | |
rottenegg | 9:ac396c818874 | 217 | bool CaMove::is_caught() { |
rottenegg | 10:4fda7b01484a | 218 | if (abs(_ch - _h) < 10 && abs(_cv - _v) < 14 && enabled) { |
rottenegg | 9:ac396c818874 | 219 | return true; |
rottenegg | 9:ac396c818874 | 220 | } else { |
rottenegg | 9:ac396c818874 | 221 | return false; |
rottenegg | 9:ac396c818874 | 222 | } |
rottenegg | 9:ac396c818874 | 223 | } |
rottenegg | 9:ac396c818874 | 224 |