From 371093b95dfe24de7cfb3dbc45673a3050d78a97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Mon, 21 Oct 2019 15:21:22 +0200 Subject: [PATCH] Continue projectiles --- constants.h | 5 +++++ main.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/constants.h b/constants.h index 5b793b4..5586081 100644 --- a/constants.h +++ b/constants.h @@ -66,4 +66,9 @@ */ #define SFG_AI_RANDOM_CHANGE_PROBABILITY 20 +/** + Speed of rocket projectile, in squares per second. +*/ +#define SFG_ROCKET_SPEED 16 + #endif // guard diff --git a/main.c b/main.c index fc9c055..ef03d5c 100755 --- a/main.c +++ b/main.c @@ -149,6 +149,14 @@ void SFG_init(); #define SFG_PLAYER_MOVE_UNITS_PER_FRAME 1 #endif + +#define SFG_ROCKER_MOVE_UNITS_PER_FRAME \ + ((SFG_ROCKET_SPEED * RCL_UNITS_PER_SQUARE) / SFG_FPS) + +#if SFG_ROCKER_MOVE_UNITS_PER_FRAME == 0 + #define SFG_ROCKER_MOVE_UNITS_PER_FRAME 1 +#endif + #define SFG_GRAVITY_SPEED_INCREASE_PER_FRAME \ ((SFG_GRAVITY_ACCELERATION * RCL_UNITS_PER_SQUARE) / (SFG_FPS * SFG_FPS)) @@ -1164,8 +1172,11 @@ p.position[0] = SFG_player.camera.position.x; p.position[1] = SFG_player.camera.position.y; p.position[2] = SFG_player.camera.height; -p.direction[0] = 1; -p.direction[1] = 0; + +RCL_Vector2D dir = RCL_angleToDirection(SFG_player.camera.direction); + +p.direction[0] = (dir.x * SFG_ROCKER_MOVE_UNITS_PER_FRAME) / RCL_UNITS_PER_SQUARE; +p.direction[1] = (dir.y * SFG_ROCKER_MOVE_UNITS_PER_FRAME) / RCL_UNITS_PER_SQUARE; p.direction[2] = 0; printf("%d\n",SFG_createProjectile(p)); @@ -1354,6 +1365,52 @@ printf("%d\n",SFG_createProjectile(p)); SFG_player.squarePosition[1] = SFG_player.camera.position.y / RCL_UNITS_PER_SQUARE; + // update projectiles: + + for (int8_t i = 0; i < SFG_currentLevel.projectileRecordCount; ++i) + { + SFG_ProjectileRecord *p = &(SFG_currentLevel.projectileRecords[i]); + + RCL_Unit pos[3]; // we have to convert from uint16_t because under/overflows + + uint8_t collides = 0; + + for (uint8_t i = 0; i < 3; ++i) + { + pos[i] = p->position[i]; + pos[i] += p->direction[i]; + + if ( + (pos[i] < 0) || + (pos[i] >= (SFG_MAP_SIZE * RCL_UNITS_PER_SQUARE))) + { + collides = 1; + break; + } + } + + if (collides) + { + SFG_LOG("projectile collides"); + + // remove the projectile + + for (uint8_t j = i; j < SFG_currentLevel.projectileRecordCount - 1; ++j) + SFG_currentLevel.projectileRecords[j] = + SFG_currentLevel.projectileRecords[j + 1]; + + SFG_currentLevel.projectileRecordCount--; + + i--; + } + else + { + p->position[0] = pos[0]; + p->position[1] = pos[1]; + p->position[2] = pos[2]; + } + } + // handle door: if (SFG_currentLevel.doorRecordCount > 0) // has to be here