This code contains the game Starship designed for the STM32F429i-DISC1 board. It requires a keyboard to play.
Dependencies: Starship LCD_DISCO_F429ZI USBHost mbed
Bullet.c@1:527a11035e0b, 2017-11-17 (annotated)
- Committer:
- Oshyrath
- Date:
- Fri Nov 17 02:23:33 2017 +0000
- Revision:
- 1:527a11035e0b
- Parent:
- 0:c9afe145b57b
First Publish of the code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Oshyrath | 0:c9afe145b57b | 1 | #include "Bullet.h" |
Oshyrath | 1:527a11035e0b | 2 | |
Oshyrath | 1:527a11035e0b | 3 | struct Bullet * enemyBulletPTR; |
Oshyrath | 1:527a11035e0b | 4 | struct Bullet * playerBulletPTR; |
Oshyrath | 1:527a11035e0b | 5 | int16_t enemyBulletCount; |
Oshyrath | 1:527a11035e0b | 6 | int16_t playerBulletCount; |
Oshyrath | 1:527a11035e0b | 7 | |
Oshyrath | 1:527a11035e0b | 8 | void addEnemyBullet(int16_t x_pos, int16_t y_pos) |
Oshyrath | 0:c9afe145b57b | 9 | { |
Oshyrath | 0:c9afe145b57b | 10 | struct Bullet * newBullet = (struct Bullet *) malloc(sizeof(struct Bullet)); |
Oshyrath | 0:c9afe145b57b | 11 | newBullet->x_pos = x_pos; |
Oshyrath | 0:c9afe145b57b | 12 | newBullet->y_pos = y_pos; |
Oshyrath | 0:c9afe145b57b | 13 | newBullet->x_vel = 0; |
Oshyrath | 0:c9afe145b57b | 14 | newBullet->y_vel = DEFAULT_BULLET_SPEED; |
Oshyrath | 0:c9afe145b57b | 15 | newBullet->isPlayerBullet = 0; |
Oshyrath | 0:c9afe145b57b | 16 | enemyBulletCount++; |
Oshyrath | 0:c9afe145b57b | 17 | if(enemyBulletCount==1) |
Oshyrath | 0:c9afe145b57b | 18 | { |
Oshyrath | 0:c9afe145b57b | 19 | newBullet->prev = newBullet; |
Oshyrath | 0:c9afe145b57b | 20 | newBullet->next = newBullet; |
Oshyrath | 0:c9afe145b57b | 21 | enemyBulletPTR = newBullet; |
Oshyrath | 0:c9afe145b57b | 22 | return; |
Oshyrath | 0:c9afe145b57b | 23 | } |
Oshyrath | 0:c9afe145b57b | 24 | struct Bullet * tempNext = enemyBulletPTR->next; |
Oshyrath | 0:c9afe145b57b | 25 | enemyBulletPTR->next = newBullet; |
Oshyrath | 0:c9afe145b57b | 26 | newBullet->prev = enemyBulletPTR; |
Oshyrath | 0:c9afe145b57b | 27 | tempNext->prev = newBullet; |
Oshyrath | 0:c9afe145b57b | 28 | newBullet->next = tempNext; |
Oshyrath | 0:c9afe145b57b | 29 | } |
Oshyrath | 1:527a11035e0b | 30 | void addPlayerBullet(int16_t x_pos, int16_t y_pos, int16_t x_vel, int16_t y_vel, int8_t power, int8_t bomb) |
Oshyrath | 0:c9afe145b57b | 31 | { |
Oshyrath | 0:c9afe145b57b | 32 | struct Bullet * newBullet = (struct Bullet *) malloc(sizeof(struct Bullet)); |
Oshyrath | 0:c9afe145b57b | 33 | newBullet->x_pos = x_pos; |
Oshyrath | 0:c9afe145b57b | 34 | newBullet->y_pos = y_pos; |
Oshyrath | 0:c9afe145b57b | 35 | newBullet->x_vel = x_vel; |
Oshyrath | 0:c9afe145b57b | 36 | newBullet->y_vel = y_vel; |
Oshyrath | 0:c9afe145b57b | 37 | newBullet->powerShot = power; |
Oshyrath | 0:c9afe145b57b | 38 | newBullet->isPlayerBullet = 1; |
Oshyrath | 1:527a11035e0b | 39 | newBullet->isBombBullet = bomb; |
Oshyrath | 0:c9afe145b57b | 40 | playerBulletCount++; |
Oshyrath | 0:c9afe145b57b | 41 | if(playerBulletCount==1) |
Oshyrath | 0:c9afe145b57b | 42 | { |
Oshyrath | 0:c9afe145b57b | 43 | newBullet->prev = newBullet; |
Oshyrath | 0:c9afe145b57b | 44 | newBullet->next = newBullet; |
Oshyrath | 0:c9afe145b57b | 45 | playerBulletPTR = newBullet; |
Oshyrath | 0:c9afe145b57b | 46 | return; |
Oshyrath | 0:c9afe145b57b | 47 | } |
Oshyrath | 0:c9afe145b57b | 48 | struct Bullet * tempNext = playerBulletPTR->next; |
Oshyrath | 0:c9afe145b57b | 49 | playerBulletPTR->next = newBullet; |
Oshyrath | 0:c9afe145b57b | 50 | newBullet->prev = playerBulletPTR; |
Oshyrath | 0:c9afe145b57b | 51 | tempNext->prev = newBullet; |
Oshyrath | 0:c9afe145b57b | 52 | newBullet->next = tempNext; |
Oshyrath | 0:c9afe145b57b | 53 | } |
Oshyrath | 0:c9afe145b57b | 54 | void removeBullet(struct Bullet * removePTR) |
Oshyrath | 0:c9afe145b57b | 55 | { |
Oshyrath | 0:c9afe145b57b | 56 | if(removePTR->isPlayerBullet) |
Oshyrath | 0:c9afe145b57b | 57 | { |
Oshyrath | 0:c9afe145b57b | 58 | if(playerBulletCount <= 0) |
Oshyrath | 0:c9afe145b57b | 59 | return; |
Oshyrath | 0:c9afe145b57b | 60 | playerBulletCount--; |
Oshyrath | 1:527a11035e0b | 61 | if(playerBulletCount == 0) |
Oshyrath | 0:c9afe145b57b | 62 | playerBulletPTR = (struct Bullet *) 0; |
Oshyrath | 0:c9afe145b57b | 63 | else |
Oshyrath | 0:c9afe145b57b | 64 | { |
Oshyrath | 0:c9afe145b57b | 65 | playerBulletPTR = removePTR->prev; |
Oshyrath | 0:c9afe145b57b | 66 | struct Bullet * tempNext = removePTR->next; |
Oshyrath | 0:c9afe145b57b | 67 | playerBulletPTR->next = tempNext; |
Oshyrath | 0:c9afe145b57b | 68 | tempNext->prev = playerBulletPTR; |
Oshyrath | 0:c9afe145b57b | 69 | } |
Oshyrath | 0:c9afe145b57b | 70 | } |
Oshyrath | 0:c9afe145b57b | 71 | else |
Oshyrath | 0:c9afe145b57b | 72 | { |
Oshyrath | 0:c9afe145b57b | 73 | if(enemyBulletCount <= 0) |
Oshyrath | 0:c9afe145b57b | 74 | return; |
Oshyrath | 0:c9afe145b57b | 75 | enemyBulletCount--; |
Oshyrath | 1:527a11035e0b | 76 | if(enemyBulletCount == 0) |
Oshyrath | 0:c9afe145b57b | 77 | enemyBulletPTR = (struct Bullet *) 0; |
Oshyrath | 0:c9afe145b57b | 78 | else |
Oshyrath | 0:c9afe145b57b | 79 | { |
Oshyrath | 0:c9afe145b57b | 80 | enemyBulletPTR = removePTR->prev; |
Oshyrath | 0:c9afe145b57b | 81 | struct Bullet * tempNext = removePTR->next; |
Oshyrath | 0:c9afe145b57b | 82 | enemyBulletPTR->next = tempNext; |
Oshyrath | 0:c9afe145b57b | 83 | tempNext->prev = enemyBulletPTR; |
Oshyrath | 0:c9afe145b57b | 84 | } |
Oshyrath | 0:c9afe145b57b | 85 | } |
Oshyrath | 0:c9afe145b57b | 86 | free(removePTR); |
Oshyrath | 0:c9afe145b57b | 87 | } |
Oshyrath | 0:c9afe145b57b | 88 | void clearBullets() |
Oshyrath | 0:c9afe145b57b | 89 | { |
Oshyrath | 0:c9afe145b57b | 90 | while(enemyBulletCount) |
Oshyrath | 0:c9afe145b57b | 91 | removeBullet(enemyBulletPTR); |
Oshyrath | 0:c9afe145b57b | 92 | while(playerBulletCount) |
Oshyrath | 0:c9afe145b57b | 93 | removeBullet(playerBulletPTR); |
Oshyrath | 0:c9afe145b57b | 94 | } |
Oshyrath | 0:c9afe145b57b | 95 | |
Oshyrath | 0:c9afe145b57b | 96 | //Specific to 1 Bullet |
Oshyrath | 0:c9afe145b57b | 97 | void bulletMovement(struct Bullet * bullet) |
Oshyrath | 0:c9afe145b57b | 98 | { |
Oshyrath | 0:c9afe145b57b | 99 | bullet->x_pos+=bullet->x_vel; |
Oshyrath | 0:c9afe145b57b | 100 | bullet->y_pos+=bullet->y_vel; |
Oshyrath | 0:c9afe145b57b | 101 | } |
Oshyrath | 0:c9afe145b57b | 102 | void bulletMovement_ALL() |
Oshyrath | 0:c9afe145b57b | 103 | { |
Oshyrath | 0:c9afe145b57b | 104 | int i; |
Oshyrath | 0:c9afe145b57b | 105 | for(i = playerBulletCount; i; i--) |
Oshyrath | 0:c9afe145b57b | 106 | { |
Oshyrath | 0:c9afe145b57b | 107 | bulletMovement(playerBulletPTR); |
Oshyrath | 0:c9afe145b57b | 108 | playerBulletPTR = playerBulletPTR->next; |
Oshyrath | 0:c9afe145b57b | 109 | } |
Oshyrath | 0:c9afe145b57b | 110 | for(i = enemyBulletCount; i; i--) |
Oshyrath | 0:c9afe145b57b | 111 | { |
Oshyrath | 0:c9afe145b57b | 112 | bulletMovement(enemyBulletPTR); |
Oshyrath | 0:c9afe145b57b | 113 | enemyBulletPTR = enemyBulletPTR->next; |
Oshyrath | 0:c9afe145b57b | 114 | } |
Oshyrath | 0:c9afe145b57b | 115 | } |
Oshyrath | 0:c9afe145b57b | 116 | void removeBulletIfOOB(struct Bullet * bullet) |
Oshyrath | 0:c9afe145b57b | 117 | { |
Oshyrath | 0:c9afe145b57b | 118 | int xp = bullet->x_pos; |
Oshyrath | 0:c9afe145b57b | 119 | int yp = bullet->y_pos; |
Oshyrath | 0:c9afe145b57b | 120 | if(bullet->isPlayerBullet) |
Oshyrath | 0:c9afe145b57b | 121 | { |
Oshyrath | 1:527a11035e0b | 122 | if(!(bullet->isBombBullet)) |
Oshyrath | 1:527a11035e0b | 123 | { |
Oshyrath | 1:527a11035e0b | 124 | if(yp <= -BULLET_SIZE) |
Oshyrath | 1:527a11035e0b | 125 | removeBullet(bullet); |
Oshyrath | 1:527a11035e0b | 126 | return; |
Oshyrath | 1:527a11035e0b | 127 | } |
Oshyrath | 0:c9afe145b57b | 128 | if((xp <= -BULLET_SIZE)||(xp>=LCD_WIDTH)||(yp <= -BULLET_SIZE)||(yp>=LCD_HEIGHT)) |
Oshyrath | 0:c9afe145b57b | 129 | removeBullet(bullet); |
Oshyrath | 0:c9afe145b57b | 130 | } |
Oshyrath | 0:c9afe145b57b | 131 | else |
Oshyrath | 0:c9afe145b57b | 132 | { |
Oshyrath | 0:c9afe145b57b | 133 | if(yp >= LCD_HEIGHT) |
Oshyrath | 0:c9afe145b57b | 134 | removeBullet(bullet); |
Oshyrath | 0:c9afe145b57b | 135 | } |
Oshyrath | 0:c9afe145b57b | 136 | } |
Oshyrath | 0:c9afe145b57b | 137 | void removeBulletIfOOB_ALL() |
Oshyrath | 0:c9afe145b57b | 138 | { |
Oshyrath | 0:c9afe145b57b | 139 | int i; |
Oshyrath | 0:c9afe145b57b | 140 | for(i = enemyBulletCount; i; i--) |
Oshyrath | 0:c9afe145b57b | 141 | { |
Oshyrath | 0:c9afe145b57b | 142 | enemyBulletPTR = enemyBulletPTR->next; |
Oshyrath | 0:c9afe145b57b | 143 | removeBulletIfOOB(enemyBulletPTR); |
Oshyrath | 0:c9afe145b57b | 144 | } |
Oshyrath | 0:c9afe145b57b | 145 | for(i = playerBulletCount; i; i--) |
Oshyrath | 0:c9afe145b57b | 146 | { |
Oshyrath | 0:c9afe145b57b | 147 | playerBulletPTR = playerBulletPTR->next; |
Oshyrath | 0:c9afe145b57b | 148 | removeBulletIfOOB(playerBulletPTR); |
Oshyrath | 0:c9afe145b57b | 149 | } |
Oshyrath | 0:c9afe145b57b | 150 | } |
Oshyrath | 0:c9afe145b57b | 151 | void callMultiFuncBullet() |
Oshyrath | 0:c9afe145b57b | 152 | { |
Oshyrath | 1:527a11035e0b | 153 | removeBulletIfOOB_ALL(); |
Oshyrath | 0:c9afe145b57b | 154 | bulletMovement_ALL(); |
Oshyrath | 0:c9afe145b57b | 155 | } |