From 663bd8d990c5a07f6fe6a47d296420a3b38f3c7a Mon Sep 17 00:00:00 2001 From: Arne van Iterson Date: Thu, 19 Dec 2019 21:00:20 +0100 Subject: [PATCH] Collision working but very very very slow --- .gitignore | 1 + obj/main.src | 845 +++++++++++++++++++++++++++++++++++++----------- src/baddie.h | 12 +- src/bullet.h | 12 +- src/collision.h | 40 +++ src/main.c | 10 +- src/player.h | 4 +- 7 files changed, 720 insertions(+), 204 deletions(-) create mode 100644 src/collision.h diff --git a/.gitignore b/.gitignore index 636ed96..33bc4bb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vscode/* +.vs/* bin/* src/gfx/convpng.* screenshot.png diff --git a/obj/main.src b/obj/main.src index dcceea7..31cdda8 100644 --- a/obj/main.src +++ b/obj/main.src @@ -8,17 +8,24 @@ _bulletAmount: DW 5 DB 0 _baddieAmount: - DW 32 + DW 12 DB 0 SEGMENT DATA _spriteActive: DW 0 DB 0 +_gamestate: + DW 0 + DB 0 _speed: DF 0.5 SEGMENT BSS _i: DS 3 +_t: + DS 4*1 +_dt: + DS 4*1 _player: DS 16 SEGMENT CODE @@ -120,7 +127,7 @@ L_3: ;G_1 IX-9 3 variable ;G_2 IX-6 3 variable ;G_3 IX-3 3 variable -;bulletsAlive IX+6 3 parameter +;spriteActive IX+6 3 parameter ; Stack Frame Size: 27 (bytes) @@ -131,7 +138,7 @@ L_3: _bulletSpawned: DS 3 _bullets: - DS 45 + DS 85 SEGMENT CODE _spawnBullet: LD HL,-6 @@ -141,7 +148,7 @@ _spawnBullet: JR L_8 L_6: LD HL,(_i) - LD A,9 + LD A,17 CALL __imul_b LD (IX+-3),HL LD BC,_bullets @@ -186,7 +193,7 @@ L_10: ;**************************** _spawnBullet *************************** ;Name Addr/Register Size Type -;_bullets STATIC 45 variable +;_bullets STATIC 85 variable ;_i STATIC 3 variable ;G_5 IX-3 3 variable ;posY IX+12 4 parameter @@ -198,42 +205,90 @@ L_10: _updateBullet: - LD HL,-12 + LD HL,-30 CALL __frameset LD HL,(IX+6) - LD A,9 + LD A,17 CALL __imul_b LD (IX+-3),HL LD BC,_bullets ADD HL,BC - LD (IX+-6),HL - LD IY,(IX+-6) + LD (IX+-9),HL + LD IY,(IX+-9) LD A,(IY+0) OR A,A - JR NZ,L_14 - LD IY,(IX+-6) + JR NZ,L_17 + LD IY,(IX+-9) LD HL,(IY+1) LD E,(IY+4) LD BC,11010048 LD A,67 CALL __fcmp - JP P,L_12 - LD BC,_bullets + JP P,L_15 + LD BC,(IX+-3) + LD (IX+-12),BC LD HL,(IX+-3) + LD BC,_bullets ADD HL,BC - LD (IX+-12),HL - LD IY,(IX+-12) + LD (IX+-18),HL + LD IY,(IX+-18) LEA BC,IY+1 - LD (IX+-9),BC + LD (IX+-15),BC LD HL,(_speed) LD A,(_speed+3) - LD IY,(IX+-9) + LD IY,(IX+-15) LD BC,(IY+0) LD E,A LD A,(IY+3) CALL __fadd LD (IY+0),BC LD (IY+3),A + LD HL,(IY+0) + LD E,(IY+3) + LD IY,(IX+-18) + LD BC,(IY+9) + LD A,(IY+12) + CALL __fsub + CALL __ftol + PUSH BC + CALL _abs + POP BC + LD BC,1 + OR A,A + SBC HL,BC + JP P,L_12 + LD BC,(IX+-3) + LD (IX+-12),BC + LD HL,(IX+-3) + LD BC,_bullets + ADD HL,BC + LD (IX+-21),HL + LD IY,(IX+-21) + LD HL,(IY+5) + LD E,(IY+8) + LD BC,(IY+13) + LD A,(IY+16) + CALL __fsub + CALL __ftol + PUSH BC + CALL _abs + POP BC + LD BC,1 + OR A,A + SBC HL,BC + CALL __setflag + JP M,L_13 +L_12: + LD BC,(IX+-12) + LD (IX+-6),BC + LD HL,BC + LD BC,_bullets + ADD HL,BC + LD (IX+-27),HL + LD IY,(IX+-27) + LEA BC,IY+1 + LD (IX+-24),BC + LD IY,(IX+-24) LD BC,(IY+4) LD A,(IY+7) CALL __ftol @@ -248,17 +303,31 @@ _updateBullet: POP BC POP BC POP BC + LD HL,(IX+-6) + LD BC,_bullets + ADD HL,BC + LD (IX+-30),HL + LD IY,(IX+-30) + LD BC,(IY+1) + LD A,(IY+4) + LD (IY+9),BC + LD (IY+12),A + LD BC,(IY+5) + LD A,(IY+8) + LD (IY+13),BC + LD (IY+16),A +L_13: LD HL,1 - JR L_15 -L_12: - LD IY,(IX+-6) + JR L_18 +L_15: + LD IY,(IX+-9) LD (IY+0),1 LD HL,1 - JR L_15 -L_14: + JR L_18 +L_17: OR A,A SBC HL,HL -L_15: +L_18: LD SP,IX POP IX RET @@ -268,14 +337,17 @@ L_15: ;Name Addr/Register Size Type ;_bullet_data IMPORT 258 variable ;_gfx_Sprite IMPORT ----- function +;_abs IMPORT ----- function ;_speed STATIC 4 variable -;_bullets STATIC 45 variable -;G_6 IX-6 3 variable +;_bullets STATIC 85 variable +;G_9 IX-12 3 variable +;G_6 IX-9 3 variable +;G_10 IX-6 3 variable ;G_7 IX-3 3 variable ;i IX+6 3 parameter -; Stack Frame Size: 21 (bytes) +; Stack Frame Size: 39 (bytes) ; Spill Code: 0 (instruction) @@ -283,17 +355,17 @@ L_15: _baddieSpawned: DS 3 _baddies: - DS 288 + DS 204 SEGMENT CODE _spawnBaddie: LD HL,-6 CALL __frameset LD BC,0 LD (_i),BC - JR L_20 -L_18: + JR L_23 +L_21: LD HL,(_i) - LD A,9 + LD A,17 CALL __imul_b LD (IX+-3),HL LD BC,_baddies @@ -301,7 +373,7 @@ L_18: LD IY,HL LD A,(IY+0) CP A,1 - JR NZ,L_19 + JR NZ,L_22 LD HL,(IX+-3) ADD HL,BC LD (IX+-6),HL @@ -316,20 +388,21 @@ L_18: LD (IY+8),A LD (IY+0),0 LD HL,1 - JR L_22 -L_19: + JR L_25 +L_22: LD BC,(_i) INC BC LD (_i),BC -L_20: - LD BC,32 +L_23: + LD BC,12 LD HL,(_i) OR A,A SBC HL,BC - JP M,L_18 + CALL __setflag + JP M,L_21 OR A,A SBC HL,HL -L_22: +L_25: LD SP,IX POP IX RET @@ -337,9 +410,9 @@ L_22: ;**************************** _spawnBaddie *************************** ;Name Addr/Register Size Type -;_baddies STATIC 288 variable +;_baddies STATIC 204 variable ;_i STATIC 3 variable -;G_9 IX-3 3 variable +;G_12 IX-3 3 variable ;posY IX+12 4 parameter ;posX IX+6 4 parameter @@ -349,43 +422,91 @@ L_22: _updateBaddie: - LD HL,-12 + LD HL,-30 CALL __frameset LD HL,(IX+6) - LD A,9 + LD A,17 CALL __imul_b LD (IX+-3),HL LD BC,_baddies ADD HL,BC - LD (IX+-6),HL - LD IY,(IX+-6) + LD (IX+-9),HL + LD IY,(IX+-9) LD A,(IY+0) OR A,A - JR NZ,L_26 - LD IY,(IX+-6) + JR NZ,L_32 + LD IY,(IX+-9) LD BC,(IY+1) LD A,(IY+4) OR A,A SBC HL,HL LD E,194 CALL __fcmp - JP P,L_24 - LD BC,_baddies + JP P,L_30 + LD BC,(IX+-3) + LD (IX+-12),BC LD HL,(IX+-3) + LD BC,_baddies ADD HL,BC - LD (IX+-12),HL - LD IY,(IX+-12) + LD (IX+-18),HL + LD IY,(IX+-18) LEA BC,IY+1 - LD (IX+-9),BC + LD (IX+-15),BC LD HL,(_speed) LD A,(_speed+3) - LD IY,(IX+-9) + LD IY,(IX+-15) LD BC,(IY+0) LD E,A LD A,(IY+3) CALL __fsub LD (IY+0),BC LD (IY+3),A + LD HL,(IY+0) + LD E,(IY+3) + LD IY,(IX+-18) + LD BC,(IY+9) + LD A,(IY+12) + CALL __fsub + CALL __ftol + PUSH BC + CALL _abs + POP BC + LD BC,1 + OR A,A + SBC HL,BC + JP P,L_27 + LD BC,(IX+-3) + LD (IX+-12),BC + LD HL,(IX+-3) + LD BC,_baddies + ADD HL,BC + LD (IX+-21),HL + LD IY,(IX+-21) + LD HL,(IY+5) + LD E,(IY+8) + LD BC,(IY+13) + LD A,(IY+16) + CALL __fsub + CALL __ftol + PUSH BC + CALL _abs + POP BC + LD BC,1 + OR A,A + SBC HL,BC + CALL __setflag + JP M,L_28 +L_27: + LD BC,(IX+-12) + LD (IX+-6),BC + LD HL,BC + LD BC,_baddies + ADD HL,BC + LD (IX+-27),HL + LD IY,(IX+-27) + LEA BC,IY+1 + LD (IX+-24),BC + LD IY,(IX+-24) LD BC,(IY+4) LD A,(IY+7) CALL __ftol @@ -400,17 +521,31 @@ _updateBaddie: POP BC POP BC POP BC + LD HL,(IX+-6) + LD BC,_baddies + ADD HL,BC + LD (IX+-30),HL + LD IY,(IX+-30) + LD BC,(IY+1) + LD A,(IY+4) + LD (IY+9),BC + LD (IY+12),A + LD BC,(IY+5) + LD A,(IY+8) + LD (IY+13),BC + LD (IY+16),A +L_28: LD HL,1 - JR L_27 -L_24: - LD IY,(IX+-6) + JR L_33 +L_30: + LD IY,(IX+-9) LD (IY+0),1 LD HL,1 - JR L_27 -L_26: + JR L_33 +L_32: OR A,A SBC HL,HL -L_27: +L_33: LD SP,IX POP IX RET @@ -420,14 +555,316 @@ L_27: ;Name Addr/Register Size Type ;_baddie_data IMPORT 1026 variable ;_gfx_Sprite IMPORT ----- function +;_abs IMPORT ----- function ;_speed STATIC 4 variable -;_baddies STATIC 288 variable -;G_11 IX-6 3 variable -;G_12 IX-3 3 variable +;_baddies STATIC 204 variable +;G_17 IX-12 3 variable +;G_14 IX-9 3 variable +;G_18 IX-6 3 variable +;G_15 IX-3 3 variable ;i IX+6 3 parameter -; Stack Frame Size: 21 (bytes) +; Stack Frame Size: 39 (bytes) +; Spill Code: 0 (instruction) + + + SEGMENT DATA +_distance: + DF 0.0 + SEGMENT BSS +_difference: + DS 8 +_a: + DS 3 + SEGMENT CODE +_checkCollision: + LD HL,-42 + CALL __frameset + LD BC,0 + LD (_i),BC + JR L_47 +L_45: + LD BC,0 + LD (_a),BC + JR L_41 +L_39: + LD HL,(_i) + LD A,17 + CALL __imul_b + LD BC,_baddies + ADD HL,BC + LD (IX+-12),HL + LD HL,(_a) + LD A,17 + CALL __imul_b + LD BC,_bullets + ADD HL,BC + LD (IX+-9),HL + LD IY,(IX+-12) + LD BC,(IY+1) + LD A,(IY+4) + LD HL,8388608 + LD E,65 + CALL __fadd + LD (IX+-25),BC + LD IY,(IX+-9) + LD BC,(IY+1) + LD (IX+-26),A + LD A,(IY+4) + OR A,A + SBC HL,HL + LD E,65 + CALL __fadd + LD E,A + LD HL,BC + LD BC,(IX+-25) + LD A,(IX+-26) + CALL __fsub + LD (_difference),BC + LD (_difference+3),A + LD IY,(IX+-12) + LD BC,(IY+5) + LD A,(IY+8) + LD HL,8388608 + LD E,65 + CALL __fadd + LD (IX+-29),BC + LD IY,(IX+-9) + LD BC,(IY+5) + LD (IX+-30),A + LD A,(IY+8) + OR A,A + SBC HL,HL + LD E,65 + CALL __fadd + LD E,A + LD HL,BC + LD BC,(IX+-29) + LD A,(IX+-30) + CALL __fsub + LD IY,_difference + LD (IY+4),BC + LD (IY+7),A + LD C,64 + LD B,0 + PUSH BC + LD A,(_difference+3) + LD C,A + LD DE,0 + PUSH DE + PUSH BC + LD BC,(_difference) + PUSH BC + CALL _pow + LD (IX+-21),HL + LD H,0 + POP BC + POP BC + POP BC + POP BC + LD (IX+-22),E + LD C,64 + LD B,0 + PUSH BC + LD BC,0 + PUSH BC + LD IY,_difference + LD BC,(IY+4) + LD A,(IY+7) + LD L,A + PUSH HL + PUSH BC + CALL _pow + POP BC + POP BC + POP BC + POP BC + LD A,(IX+-22) + LD BC,(IX+-21) + CALL __fadd + LD DE,BC + LD C,A + LD B,0 + PUSH BC + PUSH DE + CALL _sqrt + POP BC + POP BC + LD (_distance),HL + LD A,E + LD (_distance+3),A + LD HL,(_i) + LD A,17 + CALL __imul_b + LD BC,_baddies + ADD HL,BC + LD (IX+-3),HL + LD IY,(IX+-3) + LD A,(IY+0) + OR A,A + JR NZ,L_40 + LD HL,(_a) + LD A,17 + CALL __imul_b + LD BC,_bullets + ADD HL,BC + LD (IX+-6),HL + LD IY,(IX+-6) + LD A,(IY+0) + OR A,A + JR NZ,L_40 + LD HL,3145728 + LD E,65 + LD BC,(_distance) + LD A,(_distance+3) + CALL __fcmp + JP M,L_40 + LD A,1 + LD IY,(IX+-6) + LD (IY+0),A + LD IY,(IX+-3) + LD (IY+0),A +L_40: + LD BC,(_a) + INC BC + LD (_a),BC +L_41: + LD BC,4 + LD HL,(_a) + OR A,A + SBC HL,BC + JP M,L_39 + LD HL,(_i) + LD A,17 + CALL __imul_b + LD BC,_baddies + ADD HL,BC + LD (IX+-18),HL + LD BC,_player + LD (IX+-15),BC + LD IY,(IX+-18) + LD BC,(IY+1) + LD A,(IY+4) + LD HL,8388608 + LD E,65 + CALL __fadd + LD (IX+-37),BC + LD IY,(IX+-15) + LD BC,(IY+0) + LD (IX+-38),A + LD A,(IY+3) + LD HL,8388608 + LD E,65 + CALL __fadd + LD E,A + LD HL,BC + LD BC,(IX+-37) + LD A,(IX+-38) + CALL __fsub + LD (_difference),BC + LD (_difference+3),A + LD IY,(IX+-18) + LD BC,(IY+5) + LD A,(IY+8) + LD HL,8388608 + LD E,65 + CALL __fadd + LD (IX+-41),BC + LD IY,(IX+-15) + LD BC,(IY+4) + LD (IX+-42),A + LD A,(IY+7) + LD HL,8388608 + LD E,65 + CALL __fadd + LD E,A + LD HL,BC + LD BC,(IX+-41) + LD A,(IX+-42) + CALL __fsub + LD IY,_difference + LD (IY+4),BC + LD (IY+7),A + LD C,64 + LD B,0 + PUSH BC + LD A,(_difference+3) + LD C,A + LD DE,0 + PUSH DE + PUSH BC + LD BC,(_difference) + PUSH BC + CALL _pow + LD (IX+-33),HL + LD H,0 + POP BC + POP BC + POP BC + POP BC + LD (IX+-34),E + LD C,64 + LD B,0 + PUSH BC + LD BC,0 + PUSH BC + LD IY,_difference + LD BC,(IY+4) + LD A,(IY+7) + LD L,A + PUSH HL + PUSH BC + CALL _pow + POP BC + POP BC + POP BC + POP BC + LD A,(IX+-34) + LD BC,(IX+-33) + CALL __fadd + LD DE,BC + LD C,A + LD B,0 + PUSH BC + PUSH DE + CALL _sqrt + POP BC + POP BC + LD (_distance),HL + LD A,E + LD (_distance+3),A + LD BC,(_i) + INC BC + LD (_i),BC +L_47: + LD BC,11 + LD HL,(_i) + OR A,A + SBC HL,BC + CALL __setflag + JP M,L_45 + LD SP,IX + POP IX + RET + + +;**************************** _checkCollision *************************** +;Name Addr/Register Size Type +;_player STATIC 16 variable +;_distance STATIC 4 variable +;_pow IMPORT ----- function +;_sqrt IMPORT ----- function +;_difference STATIC 8 variable +;_bullets STATIC 85 variable +;_baddies STATIC 204 variable +;_a STATIC 3 variable +;_i STATIC 3 variable +;G_21 IX-6 3 variable +;G_22 IX-3 3 variable + + +; Stack Frame Size: 48 (bytes) ; Spill Code: 0 (instruction) @@ -437,7 +874,7 @@ _handleInput: CALL _kb_Scan LD A,(16056350) AND A,1 - JR Z,L_32 + JR Z,L_53 LD BC,_player LD (IX+-3),BC LD IY,(IX+-3) @@ -446,7 +883,7 @@ _handleInput: LD BC,5242880 LD A,67 CALL __fcmp - JP P,L_32 + JP P,L_53 LD HL,(_speed) LD A,(_speed+3) LD IY,(IX+-3) @@ -456,10 +893,10 @@ _handleInput: CALL __fadd LD (IY+4),BC LD (IY+7),A -L_32: +L_53: LD A,(16056350) AND A,8 - JR Z,L_35 + JR Z,L_56 LD BC,_player LD (IX+-6),BC LD IY,(IX+-6) @@ -469,7 +906,7 @@ L_32: SBC HL,HL LD E,0 CALL __fcmp - JP P,L_35 + JP P,L_56 LD HL,(_speed) LD A,(_speed+3) LD IY,(IX+-6) @@ -479,10 +916,10 @@ L_32: CALL __fsub LD (IY+4),BC LD (IY+7),A -L_35: +L_56: LD A,(16056350) AND A,2 - JR Z,L_38 + JR Z,L_59 LD BC,_player LD (IX+-9),BC LD IY,(IX+-9) @@ -492,7 +929,7 @@ L_35: SBC HL,HL LD E,0 CALL __fcmp - JP P,L_38 + JP P,L_59 LD HL,(_speed) LD A,(_speed+3) LD IY,(IX+-9) @@ -502,10 +939,10 @@ L_35: CALL __fsub LD (IY+0),BC LD (IY+3),A -L_38: +L_59: LD A,(16056350) AND A,4 - JR Z,L_41 + JR Z,L_62 LD BC,_player LD (IX+-12),BC LD IY,(IX+-12) @@ -514,7 +951,7 @@ L_38: LD BC,9437184 LD A,67 CALL __fcmp - JP P,L_41 + JP P,L_62 LD HL,(_speed) LD A,(_speed+3) LD IY,(IX+-12) @@ -524,13 +961,13 @@ L_38: CALL __fadd LD (IY+0),BC LD (IY+3),A -L_41: +L_62: LD A,(16056338) AND A,32 - JR Z,L_43 + JR Z,L_64 LD HL,(_bulletSpawned) CALL __icmpzero - JR NZ,L_43 + JR NZ,L_64 LD BC,_player LD (IX+-15),BC LD IY,(IX+-15) @@ -565,19 +1002,19 @@ L_41: POP BC LD BC,1 LD (_bulletSpawned),BC -L_43: +L_64: LD A,(16056338) CP A,32 - JR Z,L_46 + JR Z,L_67 LD BC,0 LD (_bulletSpawned),BC -L_46: +L_67: LD A,(16056338) AND A,64 - JR Z,L_48 + JR Z,L_69 LD HL,(_baddieSpawned) CALL __icmpzero - JR NZ,L_48 + JR NZ,L_69 CALL _random LD BC,209 CALL __iremu @@ -600,10 +1037,10 @@ L_46: POP BC LD BC,1 LD (_baddieSpawned),BC -L_48: +L_69: LD A,(16056338) CP A,64 - JR Z,L_49 + JR Z,L_70 LD BC,0 LD (_baddieSpawned),BC ; 1 // Libraries lol @@ -623,63 +1060,63 @@ L_48: ; 15 ; 16 // set global vars ; 17 const int bulletAmount = 5; -; 18 const int baddieAmount = 32; +; 18 const int baddieAmount = 12; ; 19 int spriteActive = 0; -; 20 float speed = 0.5; -; 21 int i; -; 22 -; 23 // Include entity headers -; 24 #include "player.h" -; 25 #include "bullet.h" -; 26 #include "baddie.h" -; 27 #include "controls.h" -; 28 -; 29 void setup() -; 30 { -; 31 boot_Set48MHzModeI(); +; 20 int gamestate = 0; +; 21 float speed = 0.5; +; 22 int i; +; 23 float t; +; 24 float dt; +; 25 +; 26 // Include entity headers +; 27 #include "player.h" +; 28 #include "bullet.h" +; 29 #include "baddie.h" +; 30 #include "collision.h" +; 31 #include "controls.h" ; 32 -; 33 /* Initialize the 8bpp graphics */ -; 34 gfx_Begin(); -; 35 dbg_sprintf(dbgout, "This is the start of a CEmu debugging test\n"); -; 36 dbg_Debugger(); -; 37 -; 38 /* Set up the palette for our sprites */ -; 39 gfx_SetPalette(sprites_pal, sizeof_sprites_pal, 0); -; 40 -; 41 // Clear screen -; 42 gfx_FillScreen(01); -; 43 -; 44 // Player starting position -; 45 player.pos.x = 10; -; 46 player.pos.y = 136; -; 47 player.oldPos.x = 10; -; 48 player.oldPos.y = 10; -; 49 -; 50 // Reset all bullets -; 51 for (i = 0; i < bulletAmount; i++) -; 52 { -; 53 bullets[i].dead = true; -; 54 bullets[i].pos.x = 0; -; 55 bullets[i].pos.y = 0; -; 56 } -; 57 -; 58 // Reset all baddies -; 59 for (i = 0; i < baddieAmount; i++) -; 60 { -; 61 baddies[i].dead = true; -; 62 baddies[i].pos.x = 0; -; 63 baddies[i].pos.y = 0; -; 64 } -; 65 } -; 66 -; 67 void main(void) -; 68 { -; 69 setup(); -; 70 do -; 71 { -; 72 -; 73 // Handle user input -L_49: +; 33 void setup() +; 34 { +; 35 boot_Set48MHzModeI(); +; 36 +; 37 /* Initialize the 8bpp graphics */ +; 38 gfx_Begin(); +; 39 +; 40 /* Set up the palette for our sprites */ +; 41 gfx_SetPalette(sprites_pal, sizeof_sprites_pal, 0); +; 42 +; 43 // Clear screen +; 44 gfx_FillScreen(01); +; 45 +; 46 // Player starting position +; 47 player.pos.x = 10; +; 48 player.pos.y = 136; +; 49 player.oldPos.x = 10; +; 50 player.oldPos.y = 10; +; 51 +; 52 // Reset all bullets +; 53 for (i = 0; i < bulletAmount; i++) +; 54 { +; 55 bullets[i].dead = true; +; 56 bullets[i].pos.x = 0; +; 57 bullets[i].pos.y = 0; +; 58 } +; 59 +; 60 // Reset all baddies +; 61 for (i = 0; i < baddieAmount; i++) +; 62 { +; 63 baddies[i].dead = true; +; 64 baddies[i].pos.x = 0; +; 65 baddies[i].pos.y = 0; +; 66 } +; 67 } +; 68 +; 69 void main(void) +; 70 { +; 71 setup(); +; 72 do +; 73 { +L_70: LD SP,IX POP IX RET @@ -693,10 +1130,10 @@ L_49: ;_speed STATIC 4 variable ;_player STATIC 16 variable ;_kb_Scan IMPORT ----- function -;G_18 IX-12 3 variable -;G_17 IX-9 3 variable -;G_16 IX-6 3 variable -;G_15 IX-3 3 variable +;G_27 IX-12 3 variable +;G_26 IX-9 3 variable +;G_25 IX-6 3 variable +;G_24 IX-3 3 variable ; Stack Frame Size: 27 (bytes) @@ -742,10 +1179,10 @@ _setup: LD (IY+7),A LD BC,0 LD (_i),BC - JR L_55 -L_53: + JR L_74 +L_72: LD HL,(_i) - LD A,9 + LD A,17 CALL __imul_b LD BC,_bullets ADD HL,BC @@ -760,18 +1197,18 @@ L_53: LD BC,(_i) INC BC LD (_i),BC -L_55: +L_74: LD BC,5 LD HL,(_i) OR A,A SBC HL,BC - JP M,L_53 + JP M,L_72 LD BC,0 LD (_i),BC - JR L_59 -L_57: + JR L_78 +L_76: LD HL,(_i) - LD A,9 + LD A,17 CALL __imul_b LD BC,_baddies ADD HL,BC @@ -786,13 +1223,13 @@ L_57: LD BC,(_i) INC BC LD (_i),BC -L_59: - LD BC,32 +L_78: + LD BC,12 LD HL,(_i) OR A,A SBC HL,BC CALL __setflag - JP M,L_57 + JP M,L_76 LD SP,IX POP IX RET @@ -800,8 +1237,8 @@ L_59: ;**************************** _setup *************************** ;Name Addr/Register Size Type -;_baddies STATIC 288 variable -;_bullets STATIC 45 variable +;_baddies STATIC 204 variable +;_bullets STATIC 85 variable ;_i STATIC 3 variable ;_player STATIC 16 variable ;_gfx_FillScreen IMPORT ----- function @@ -817,18 +1254,20 @@ L_59: _main: CALL _setup -L_74: -; 74 handleInput(); +L_93: +; 74 +; 75 // Handle user input +; 76 handleInput(); CALL _handleInput -; 75 -; 76 // Move all alive bullets -; 77 for (i = 0; i < bulletAmount; i++) +; 77 +; 78 // Move all alive bullets +; 79 for (i = 0; i < bulletAmount; i++) LD BC,0 LD (_i),BC - JR L_66 -L_64: -; 78 { -; 79 if (updateBullet(i) == 1) + JR L_85 +L_83: +; 80 { +; 81 if (updateBullet(i) == 1) LD BC,(_i) PUSH BC CALL _updateBullet @@ -836,32 +1275,32 @@ L_64: LD BC,1 OR A,A SBC HL,BC - JR NZ,L_65 -; 80 { -; 81 spriteActive = 1; + JR NZ,L_84 +; 82 { +; 83 spriteActive = 1; LD (_spriteActive),BC -; 82 } -; 83 } -L_65: +; 84 } +; 85 } +L_84: LD BC,(_i) INC BC LD (_i),BC -L_66: +L_85: LD BC,5 LD HL,(_i) OR A,A SBC HL,BC CALL __setflag - JP M,L_64 -; 84 -; 85 // Move all alive baddies -; 86 for (i = 0; i < baddieAmount; i++) + JP M,L_83 +; 86 +; 87 // Move all alive baddies +; 88 for (i = 0; i < baddieAmount; i++) LD BC,0 LD (_i),BC - JR L_71 -L_69: -; 87 { -; 88 if (updateBaddie(i) == 1) + JR L_90 +L_88: +; 89 { +; 90 if (updateBaddie(i) == 1) LD BC,(_i) PUSH BC CALL _updateBaddie @@ -869,39 +1308,42 @@ L_69: LD BC,1 OR A,A SBC HL,BC - JR NZ,L_70 -; 89 { -; 90 spriteActive = 1; + JR NZ,L_89 +; 91 { +; 92 spriteActive = 1; LD (_spriteActive),BC -; 91 } -; 92 -; 93 } -L_70: +; 93 } +; 94 +; 95 } +L_89: LD BC,(_i) INC BC LD (_i),BC -L_71: - LD BC,32 +L_90: + LD BC,12 LD HL,(_i) OR A,A SBC HL,BC CALL __setflag - JP M,L_69 -; 94 -; 95 // Redraw player if position has changed or if a bullet has been fired -; 96 updatePlayer(spriteActive); + JP M,L_88 +; 96 +; 97 checkCollision(); + CALL _checkCollision +; 98 +; 99 // Redraw player if position has changed or if a bullet has been fired +; 100 updatePlayer(spriteActive); LD BC,(_spriteActive) PUSH BC CALL _updatePlayer POP BC -; 97 -; 98 } while (kb_Data[6] != kb_Enter); +; 101 +; 102 } while (kb_Data[6] != kb_Enter); LD A,(16056348) CP A,1 - JR NZ,L_74 -; 99 -; 100 // Close the graphics -; 101 gfx_End(); + JR NZ,L_93 +; 103 +; 104 // Close the graphics +; 105 gfx_End(); CALL _gfx_End RET @@ -928,6 +1370,8 @@ L_71: XREF _gfx_End:ROM XREF _gfx_Begin:ROM XREF _abs:ROM + XREF _sqrt:ROM + XREF _pow:ROM XREF _boot_Set48MHzModeI:ROM XREF _random:ROM XREF __iremu:ROM @@ -943,6 +1387,10 @@ L_71: XDEF _main XDEF _setup XDEF _handleInput + XDEF _checkCollision + XDEF _a + XDEF _difference + XDEF _distance XDEF _updateBaddie XDEF _spawnBaddie XDEF _baddies @@ -953,8 +1401,11 @@ L_71: XDEF _bulletSpawned XDEF _updatePlayer XDEF _player + XDEF _dt + XDEF _t XDEF _i XDEF _speed + XDEF _gamestate XDEF _spriteActive XDEF _baddieAmount XDEF _bulletAmount diff --git a/src/baddie.h b/src/baddie.h index 3203354..2346494 100644 --- a/src/baddie.h +++ b/src/baddie.h @@ -7,6 +7,11 @@ struct Baddie { float x; float y; } pos; + struct oldPos + { + float x; + float y; + } oldPos; } baddies[baddieAmount]; int spawnBaddie(float posX, float posY) @@ -31,7 +36,12 @@ int updateBaddie(int i) if (!(baddies[i].pos.x <= (0 - 32))) { baddies[i].pos.x -= speed; - gfx_Sprite(baddie, baddies[i].pos.x, baddies[i].pos.y); + if ((abs(baddies[i].oldPos.x - baddies[i].pos.x) >= 1) || (abs(baddies[i].oldPos.y - baddies[i].pos.y) >= 1)) + { + gfx_Sprite(baddie, baddies[i].pos.x, baddies[i].pos.y); + baddies[i].oldPos.x = baddies[i].pos.x; + baddies[i].oldPos.y = baddies[i].pos.y; + } return 1; } else diff --git a/src/bullet.h b/src/bullet.h index b7fd92a..68c06f7 100644 --- a/src/bullet.h +++ b/src/bullet.h @@ -9,6 +9,11 @@ struct Bullet float x; float y; } pos; + struct oldPos + { + float x; + float y; + } oldPos; } bullets[bulletAmount]; int spawnBullet(float posX, float posY) @@ -33,7 +38,12 @@ int updateBullet(int i) if (!(bullets[i].pos.x >= (LCD_WIDTH + 16))) { bullets[i].pos.x += speed; - gfx_Sprite(bullet, bullets[i].pos.x, bullets[i].pos.y); + if ((abs(bullets[i].oldPos.x - bullets[i].pos.x) >= 1) || (abs(bullets[i].oldPos.y - bullets[i].pos.y) >= 1)) + { + gfx_Sprite(bullet, bullets[i].pos.x, bullets[i].pos.y); + bullets[i].oldPos.x = bullets[i].pos.x; + bullets[i].oldPos.y = bullets[i].pos.y; + } return 1; } else diff --git a/src/collision.h b/src/collision.h new file mode 100644 index 0000000..9fc05b9 --- /dev/null +++ b/src/collision.h @@ -0,0 +1,40 @@ +// Variables for collision detection +float distance = 0; +float difference[2]; +int a; + +// Check collisions +void checkCollision() { + for (i = 0; i < (baddieAmount - 1); i++) + { + // Collision baddie x bullet + for (a = 0; a < (bulletAmount - 1); a++) + { + //// Difference x + difference[0] = (baddies[i].pos.x + (32 / 2)) - (bullets[a].pos.x + (16 / 2)); + //// Difference y + difference[1] = (baddies[i].pos.y + (32 / 2)) - (bullets[a].pos.y + (16 / 2)); + + // Calculate diffence + distance = sqrt(pow(difference[0], 2) + pow(difference[1], 2)); + if (!baddies[i].dead && !bullets[a].dead && distance <= 11) + { + bullets[a].dead = true; + baddies[i].dead = true; + } + } + + // Collision baddie x player + //// Difference x + difference[0] = (baddies[i].pos.x + (32 / 2)) - (player.pos.x + (32 / 2)); + //// Difference y + difference[1] = (baddies[i].pos.y + (32 / 2)) - (player.pos.y + (32 / 2)); + + // Calculate diffence + distance = sqrt(pow(difference[0], 2) + pow(difference[1], 2)); + if (!baddies[i].dead && distance <= 15) + { + // Gameover + } + } +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index d3a15d9..a4f4fb1 100644 --- a/src/main.c +++ b/src/main.c @@ -15,15 +15,19 @@ // set global vars const int bulletAmount = 5; -const int baddieAmount = 32; +const int baddieAmount = 12; int spriteActive = 0; +int gamestate = 0; float speed = 0.5; int i; +float t; +float dt; // Include entity headers #include "player.h" #include "bullet.h" #include "baddie.h" +#include "collision.h" #include "controls.h" void setup() @@ -32,8 +36,6 @@ void setup() /* Initialize the 8bpp graphics */ gfx_Begin(); - dbg_sprintf(dbgout, "This is the start of a CEmu debugging test\n"); - dbg_Debugger(); /* Set up the palette for our sprites */ gfx_SetPalette(sprites_pal, sizeof_sprites_pal, 0); @@ -92,6 +94,8 @@ void main(void) } + checkCollision(); + // Redraw player if position has changed or if a bullet has been fired updatePlayer(spriteActive); diff --git a/src/player.h b/src/player.h index 44f7dff..b9230c1 100644 --- a/src/player.h +++ b/src/player.h @@ -13,9 +13,9 @@ struct Player } oldPos; } player; -void updatePlayer(int bulletsAlive) +void updatePlayer(int spriteActive) { - if ((abs(player.oldPos.x - player.pos.x) >= 1) || (abs(player.oldPos.y - player.pos.y) >= 1) || bulletsAlive == 1) + if ((abs(player.oldPos.x - player.pos.x) >= 1) || (abs(player.oldPos.y - player.pos.y) >= 1) || spriteActive == 1) { gfx_Sprite(spaceship, player.pos.x, player.pos.y); player.oldPos.x = player.pos.x;