Stick_Runner
Dependencies: FXOS8700CQ Gamepad N5110 SDFileSystem mbed
Fork of Stick_Runner by
Character/Character.cpp@3:0c690f1c04d8, 2017-05-03 (annotated)
- Committer:
- el15ss
- Date:
- Wed May 03 09:27:04 2017 +0000
- Revision:
- 3:0c690f1c04d8
- Parent:
- 2:98a41609c827
- Child:
- 4:2fdafb53eac2
Added DOxygen commenting to all files
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el15ss | 0:12cfe63faa6a | 1 | #include "Character.h" |
el15ss | 0:12cfe63faa6a | 2 | |
el15ss | 0:12cfe63faa6a | 3 | |
el15ss | 0:12cfe63faa6a | 4 | |
el15ss | 0:12cfe63faa6a | 5 | void Character::init() |
el15ss | 0:12cfe63faa6a | 6 | { |
el15ss | 3:0c690f1c04d8 | 7 | //Initializing the X and Y co ordinates of the character |
el15ss | 3:0c690f1c04d8 | 8 | |
el15ss | 1:db9ff66f67c8 | 9 | charPosX = 42; |
el15ss | 1:db9ff66f67c8 | 10 | charPosY = 75; |
el15ss | 3:0c690f1c04d8 | 11 | |
el15ss | 3:0c690f1c04d8 | 12 | //variable to store the status of the character |
el15ss | 1:db9ff66f67c8 | 13 | charStatus = true; |
el15ss | 2:98a41609c827 | 14 | |
el15ss | 0:12cfe63faa6a | 15 | } |
el15ss | 0:12cfe63faa6a | 16 | |
el15ss | 0:12cfe63faa6a | 17 | void Character::draw(N5110 &lcd) |
el15ss | 0:12cfe63faa6a | 18 | { |
el15ss | 2:98a41609c827 | 19 | |
el15ss | 1:db9ff66f67c8 | 20 | |
el15ss | 3:0c690f1c04d8 | 21 | //Drawing the character |
el15ss | 1:db9ff66f67c8 | 22 | lcd.setPixel(charPosX,charPosY); |
el15ss | 1:db9ff66f67c8 | 23 | lcd.setPixel(charPosX-1,charPosY); |
el15ss | 1:db9ff66f67c8 | 24 | lcd.setPixel(charPosX+1,charPosY); |
el15ss | 1:db9ff66f67c8 | 25 | lcd.setPixel(charPosX,charPosY+1); |
el15ss | 1:db9ff66f67c8 | 26 | lcd.setPixel(charPosX-1,charPosY+1); |
el15ss | 1:db9ff66f67c8 | 27 | lcd.setPixel(charPosX+1,charPosY+1); |
el15ss | 1:db9ff66f67c8 | 28 | lcd.setPixel(charPosX,charPosY+2); |
el15ss | 1:db9ff66f67c8 | 29 | lcd.setPixel(charPosX-1,charPosY+2); |
el15ss | 1:db9ff66f67c8 | 30 | lcd.setPixel(charPosX+1,charPosY+2); |
el15ss | 1:db9ff66f67c8 | 31 | lcd.setPixel(charPosX,charPosY+3); |
el15ss | 1:db9ff66f67c8 | 32 | lcd.setPixel(charPosX,charPosY+4); |
el15ss | 1:db9ff66f67c8 | 33 | lcd.setPixel(charPosX,charPosY+5); |
el15ss | 1:db9ff66f67c8 | 34 | lcd.setPixel(charPosX-1,charPosY+4); |
el15ss | 1:db9ff66f67c8 | 35 | lcd.setPixel(charPosX+1,charPosY+4); |
el15ss | 1:db9ff66f67c8 | 36 | lcd.setPixel(charPosX-2,charPosY+5); |
el15ss | 1:db9ff66f67c8 | 37 | lcd.setPixel(charPosX+2,charPosY+5); |
el15ss | 1:db9ff66f67c8 | 38 | lcd.setPixel(charPosX,charPosY+6); |
el15ss | 1:db9ff66f67c8 | 39 | lcd.setPixel(charPosX-1,charPosY+7); |
el15ss | 1:db9ff66f67c8 | 40 | lcd.setPixel(charPosX+1,charPosY+7); |
el15ss | 1:db9ff66f67c8 | 41 | lcd.setPixel(charPosX-2,charPosY+8); |
el15ss | 1:db9ff66f67c8 | 42 | lcd.setPixel(charPosX+2,charPosY+8); |
el15ss | 0:12cfe63faa6a | 43 | |
el15ss | 0:12cfe63faa6a | 44 | |
el15ss | 0:12cfe63faa6a | 45 | |
el15ss | 0:12cfe63faa6a | 46 | } |
el15ss | 0:12cfe63faa6a | 47 | |
el15ss | 0:12cfe63faa6a | 48 | |
el15ss | 0:12cfe63faa6a | 49 | |
el15ss | 1:db9ff66f67c8 | 50 | void Character::updateCharacter(Direction d,float mag) |
el15ss | 0:12cfe63faa6a | 51 | { |
el15ss | 0:12cfe63faa6a | 52 | _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future |
el15ss | 0:12cfe63faa6a | 53 | |
el15ss | 0:12cfe63faa6a | 54 | // update y value depending on direction of movement |
el15ss | 0:12cfe63faa6a | 55 | // North is decrement as origin is at the top-left so decreasing moves up |
el15ss | 1:db9ff66f67c8 | 56 | if (d == N) |
el15ss | 1:db9ff66f67c8 | 57 | { |
el15ss | 1:db9ff66f67c8 | 58 | charPosY-=_speed; |
el15ss | 1:db9ff66f67c8 | 59 | } |
el15ss | 1:db9ff66f67c8 | 60 | |
el15ss | 1:db9ff66f67c8 | 61 | else if (d == NW) |
el15ss | 1:db9ff66f67c8 | 62 | { |
el15ss | 1:db9ff66f67c8 | 63 | charPosX-=_speed; |
el15ss | 1:db9ff66f67c8 | 64 | charPosY-=_speed; |
el15ss | 1:db9ff66f67c8 | 65 | } |
el15ss | 1:db9ff66f67c8 | 66 | |
el15ss | 1:db9ff66f67c8 | 67 | else if (d == NE) |
el15ss | 1:db9ff66f67c8 | 68 | { |
el15ss | 1:db9ff66f67c8 | 69 | charPosX+=_speed; |
el15ss | 1:db9ff66f67c8 | 70 | charPosY-=_speed; |
el15ss | 1:db9ff66f67c8 | 71 | } |
el15ss | 1:db9ff66f67c8 | 72 | |
el15ss | 1:db9ff66f67c8 | 73 | else if (d == S) |
el15ss | 1:db9ff66f67c8 | 74 | { |
el15ss | 1:db9ff66f67c8 | 75 | charPosY+=_speed; |
el15ss | 0:12cfe63faa6a | 76 | } |
el15ss | 1:db9ff66f67c8 | 77 | |
el15ss | 1:db9ff66f67c8 | 78 | else if (d == SW) |
el15ss | 1:db9ff66f67c8 | 79 | { |
el15ss | 1:db9ff66f67c8 | 80 | charPosX-=_speed; |
el15ss | 1:db9ff66f67c8 | 81 | charPosY+=_speed; |
el15ss | 1:db9ff66f67c8 | 82 | } |
el15ss | 1:db9ff66f67c8 | 83 | |
el15ss | 1:db9ff66f67c8 | 84 | else if (d == SE) |
el15ss | 1:db9ff66f67c8 | 85 | { |
el15ss | 1:db9ff66f67c8 | 86 | charPosX+=_speed; |
el15ss | 1:db9ff66f67c8 | 87 | charPosY+=_speed; |
el15ss | 1:db9ff66f67c8 | 88 | } |
el15ss | 1:db9ff66f67c8 | 89 | |
el15ss | 1:db9ff66f67c8 | 90 | else if (d == E) |
el15ss | 1:db9ff66f67c8 | 91 | { |
el15ss | 1:db9ff66f67c8 | 92 | charPosX+=_speed; |
el15ss | 1:db9ff66f67c8 | 93 | } |
el15ss | 1:db9ff66f67c8 | 94 | |
el15ss | 1:db9ff66f67c8 | 95 | else if (d == W) |
el15ss | 1:db9ff66f67c8 | 96 | { |
el15ss | 1:db9ff66f67c8 | 97 | charPosX-=_speed; |
el15ss | 0:12cfe63faa6a | 98 | } |
el15ss | 0:12cfe63faa6a | 99 | |
el15ss | 0:12cfe63faa6a | 100 | // check the y origin to ensure that the Character doesn't go off screen |
el15ss | 0:12cfe63faa6a | 101 | |
el15ss | 1:db9ff66f67c8 | 102 | if(charPosX-2<0) |
el15ss | 0:12cfe63faa6a | 103 | { |
el15ss | 1:db9ff66f67c8 | 104 | charPosX = 2; |
el15ss | 1:db9ff66f67c8 | 105 | } |
el15ss | 1:db9ff66f67c8 | 106 | |
el15ss | 1:db9ff66f67c8 | 107 | |
el15ss | 1:db9ff66f67c8 | 108 | if(charPosX+2>WIDTH-1) |
el15ss | 0:12cfe63faa6a | 109 | { |
el15ss | 1:db9ff66f67c8 | 110 | charPosX = WIDTH -5; |
el15ss | 0:12cfe63faa6a | 111 | } |
el15ss | 1:db9ff66f67c8 | 112 | |
el15ss | 1:db9ff66f67c8 | 113 | |
el15ss | 1:db9ff66f67c8 | 114 | if (charPosY < 1) |
el15ss | 1:db9ff66f67c8 | 115 | { |
el15ss | 1:db9ff66f67c8 | 116 | charPosY = 1; |
el15ss | 1:db9ff66f67c8 | 117 | } |
el15ss | 1:db9ff66f67c8 | 118 | |
el15ss | 1:db9ff66f67c8 | 119 | if (charPosY+8 > HEIGHT - 1) |
el15ss | 1:db9ff66f67c8 | 120 | { |
el15ss | 1:db9ff66f67c8 | 121 | charPosY = HEIGHT-10; |
el15ss | 0:12cfe63faa6a | 122 | } |
el15ss | 0:12cfe63faa6a | 123 | } |
el15ss | 0:12cfe63faa6a | 124 | |
el15ss | 0:12cfe63faa6a | 125 | |
el15ss | 3:0c690f1c04d8 | 126 | //Returns the postion (x,y) of the character on the screen |
el15ss | 1:db9ff66f67c8 | 127 | Vector2D Character::getCharacterPos() { |
el15ss | 1:db9ff66f67c8 | 128 | Vector2D p = {charPosX,charPosY}; |
el15ss | 0:12cfe63faa6a | 129 | return p; |
el15ss | 0:12cfe63faa6a | 130 | } |
el15ss | 2:98a41609c827 | 131 | |
el15ss | 3:0c690f1c04d8 | 132 | |
el15ss | 3:0c690f1c04d8 | 133 | //Function to check if the character has been hit by a obstacle and update the status if yes |
el15ss | 2:98a41609c827 | 134 | void Character::characterStatus(Vector2D p) |
el15ss | 2:98a41609c827 | 135 | { |
el15ss | 3:0c690f1c04d8 | 136 | //Using the dimensions of the character we check if there any pixels near it |
el15ss | 2:98a41609c827 | 137 | if(((charPosX-5<p.x)&&(charPosX+5>p.x))&&((charPosY-5<p.y)&&(charPosY+5>p.y))) |
el15ss | 2:98a41609c827 | 138 | { |
el15ss | 3:0c690f1c04d8 | 139 | //Updateing the status |
el15ss | 2:98a41609c827 | 140 | charStatus = false; |
el15ss | 2:98a41609c827 | 141 | } |
el15ss | 2:98a41609c827 | 142 | } |
el15ss | 2:98a41609c827 | 143 | |
el15ss | 3:0c690f1c04d8 | 144 | //Function to return the status of the character |
el15ss | 2:98a41609c827 | 145 | bool Character::getCharacterStatus() |
el15ss | 2:98a41609c827 | 146 | { |
el15ss | 3:0c690f1c04d8 | 147 | //Used to determine whether the character has been and hit and the game is over |
el15ss | 1:db9ff66f67c8 | 148 | return charStatus; |
el15ss | 0:12cfe63faa6a | 149 | |
el15ss | 2:98a41609c827 | 150 | } |
el15ss | 0:12cfe63faa6a | 151 | |
el15ss | 0:12cfe63faa6a | 152 |