From a2a528bf6f2a1069b61aa0325cac411678fb8126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Tue, 22 Oct 2019 16:48:27 +0200 Subject: [PATCH] Work on AI --- assets.h | 21 +++++++++++++++++++++ constants.h | 4 ++-- main.c | 41 +++++++++++++++++++++++++++++++++++++---- settings.h | 5 +++++ 4 files changed, 65 insertions(+), 6 deletions(-) diff --git a/assets.h b/assets.h index 558ea07..508d28a 100644 --- a/assets.h +++ b/assets.h @@ -493,6 +493,27 @@ SFG_PROGRAM_MEMORY uint8_t SFG_monsterSprites[][SFG_TEXTURE_STORE_SIZE] = 5,185,172,16,0,0,0,2,38,136,134,34,80,80,0,0,0,88,167,65,0,0,0,0,182,136,134,98, 80,0,0,0,0,5,185,145,17,17,16,0,34,102,102,102,32,0,0,0,0,0,87,153,52,51,49,0,2, 98,34,102,107,0,0,0,0,0,1,116,71,65,16,0,2,37,82,43,32,0,0,0,0,0,0,17,17,16,0 + }, + { // 2, spider walking +175,0,4,5,6,3,223,1,10,7,2,18,62,95,120,63,0,0,17,17,0,0,0,0,0,0,0,1,17,17,0,0, +0,1,50,211,16,0,0,0,0,0,0,21,51,50,16,0,0,19,26,161,49,0,0,0,0,0,17,146,35,33,0, +0,0,29,173,90,33,0,0,0,0,1,148,68,81,16,0,0,0,18,165,218,209,0,16,0,0,25,67,49, +16,0,0,0,0,19,26,161,49,16,17,0,1,148,51,16,0,0,0,0,0,1,61,35,119,113,84,16,25, +68,56,0,0,0,0,0,0,23,81,23,87,83,51,16,17,21,129,0,0,0,0,0,1,50,55,114,130,67, +68,65,0,1,50,16,0,0,0,0,1,37,39,81,68,67,51,50,0,1,85,81,17,17,17,0,1,50,215,17, +204,67,35,35,16,25,153,148,68,83,50,16,0,17,18,68,111,108,50,34,16,1,68,68,68, +82,37,81,0,1,68,67,105,246,98,34,33,0,133,88,136,136,136,129,0,1,23,117,198,150, +102,34,34,238,227,37,181,17,0,0,0,21,135,87,82,102,252,98,46,123,91,50,81,0,0,0, +1,37,24,136,114,38,159,101,235,181,35,181,112,0,0,0,1,37,24,136,114,38,159,101, +235,181,35,181,112,0,0,0,0,21,135,87,82,102,252,98,46,123,83,123,33,0,0,0,0,1, +23,117,198,150,102,34,34,238,235,50,133,17,17,0,0,1,68,67,105,246,98,34,33,0, +131,40,85,85,34,16,0,17,18,68,111,108,50,34,16,1,148,33,17,17,17,0,1,50,215,17, +204,67,35,35,16,25,50,16,0,0,0,0,1,37,39,81,68,67,51,50,17,148,72,136,128,0,0,0, +1,50,55,114,130,67,68,65,20,68,73,148,136,128,0,0,0,23,81,23,87,83,51,16,1,17, +84,68,83,56,129,0,0,1,61,35,119,113,84,16,0,5,21,68,82,34,81,0,0,19,26,161,49, +16,17,0,0,20,66,87,18,37,16,0,0,18,165,218,209,0,16,0,0,1,84,52,65,17,81,17,0, +29,173,90,33,0,0,0,0,0,17,19,52,82,37,16,0,19,26,161,49,0,0,0,0,0,0,1,19,82,81, +0,0,1,50,211,16,0,0,0,0,0,0,0,1,17,16,0,0,0,17,17,0,0,0,0,0,0,0,0,0,0,0,0 } }; diff --git a/constants.h b/constants.h index 2fc83e0..4bc5c3a 100644 --- a/constants.h +++ b/constants.h @@ -58,13 +58,13 @@ Rate at which AI will be updated, which also affects how fast enemies will appear. */ -#define SFG_AI_FPS 2 +#define SFG_AI_FPS 4 /** Says a probability (0 - 255) of the AI changing its state during one update step. */ -#define SFG_AI_RANDOM_CHANGE_PROBABILITY 20 +#define SFG_AI_RANDOM_CHANGE_PROBABILITY 40 /** Speed of rocket projectile, in squares per second. diff --git a/main.c b/main.c index 587618f..b89048a 100755 --- a/main.c +++ b/main.c @@ -329,6 +329,13 @@ typedef struct #define SFG_EXPLOSION_DURATION_FRAMES 1 #endif +#define SFG_SPRITE_ANIMATION_FRAME_DURATION \ + (SFG_FPS / SFG_SPRITE_ANIMATION_SPEED) + +#if SFG_SPRITE_ANIMATION_FRAME_DURATION == 0 + #define SFG_SPRITE_ANIMATION_FRAME_DURATION 1 +#endif + /* GLOBAL VARIABLES =============================================================================== @@ -336,6 +343,8 @@ typedef struct uint8_t SFG_currentRandom; +uint8_t SFG_spriteAnimationFrame; + struct { RCL_Camera camera; @@ -1018,6 +1027,8 @@ void SFG_setAndInitLevel(const SFG_Level *level) SFG_currentLevel.timeStart = SFG_getTimeMs(); SFG_currentLevel.frameStart = SFG_gameFrame; + + SFG_spriteAnimationFrame = 0; SFG_initPlayer(); } @@ -1068,9 +1079,10 @@ void SFG_monsterPerformAI(SFG_MonsterRecord *monster) if (SFG_random() < SFG_AI_RANDOM_CHANGE_PROBABILITY) { // sometimes randomly change state - state = SFG_MONSTER_STATE_IDLE; + state = (SFG_random() % 4 == 0) ? + SFG_MONSTER_STATE_IDLE : SFG_MONSTER_STATE_ATTACKING; } - if (state == SFG_MONSTER_STATE_IDLE) + else if (state == SFG_MONSTER_STATE_IDLE) { switch (SFG_random() % 8) { @@ -1085,6 +1097,10 @@ void SFG_monsterPerformAI(SFG_MonsterRecord *monster) default: break; } } + else if (state == SFG_MONSTER_STATE_ATTACKING) + { + state = SFG_MONSTER_STATE_IDLE; + } else { if (state == SFG_MONSTER_STATE_GOING_E || @@ -1323,6 +1339,10 @@ void SFG_gameStep() for (uint8_t i = 0; i < SFG_KEY_COUNT; ++i) SFG_keyStates[i] = (SFG_keyStates[i] << 1) | SFG_keyPressed(i); + if ((SFG_currentLevel.frameStart - SFG_gameFrame) % + SFG_SPRITE_ANIMATION_FRAME_DURATION == 0) + SFG_spriteAnimationFrame++; + int8_t recomputeDirection = 0; RCL_Vector2D moveOffset; @@ -2052,7 +2072,9 @@ void SFG_draw() { SFG_MonsterRecord m = SFG_currentLevel.monsterRecords[i]; - if (m.stateType != SFG_MONSTER_STATE_INACTIVE) + uint8_t state = m.stateType & SFG_MONSTER_MASK_STATE; + + if (state != SFG_MONSTER_STATE_INACTIVE) { RCL_Vector2D worldPosition; @@ -2069,10 +2091,21 @@ void SFG_draw() SFG_player.camera); if (p.depth > 0) - SFG_drawScaledSprite(SFG_monsterSprites[ (SFG_gameFrame >> 5) & 0x01 ], + { + const uint8_t *s; + + if (state == SFG_MONSTER_STATE_IDLE) + s = SFG_monsterSprites[0]; + else if (state == SFG_MONSTER_STATE_ATTACKING) + s = SFG_monsterSprites[1]; + else + s = SFG_monsterSprites[SFG_spriteAnimationFrame & 0x01 ? 0 : 2]; + + SFG_drawScaledSprite(s, p.position.x * SFG_RAYCASTING_SUBSAMPLE,p.position.y, RCL_perspectiveScale(SFG_GAME_RESOLUTION_Y,p.depth), p.depth / (RCL_UNITS_PER_SQUARE * 2),p.depth); + } } } diff --git a/settings.h b/settings.h index 9dca7ed..6f7c22b 100644 --- a/settings.h +++ b/settings.h @@ -130,4 +130,9 @@ */ #define SFG_EXPLOSION_DURATION 70 +/** + Specifies how quick some sprite animations are, in frames per second. +*/ +#define SFG_SPRITE_ANIMATION_SPEED 4 + #endif // guard