Split game code and tests into focused modules
This commit is contained in:
20 files changed
+2611
-2431
No files matched your search
+146
@@ -0,0 +1,146 @@
|
||||
#include "game.h"
|
||||
|
||||
TEST_INTERNAL const struct SpeciesInfo species[SPECIES_COUNT] = {
|
||||
{"GOLDEN", "LV THREE", "WILD GOLDEN CREATURE", "GOLDEN USED BUMP", "NORMAL", GOLDEN_HP, 5, 7},
|
||||
{"FERNFOX", "LV FOUR", "WILD FERNFOX APPEARED", "FERNFOX USED LEAF SWIPE", "GRASS", 18, 4, 7},
|
||||
{"EMBERWING", "LV FOUR", "WILD EMBERWING APPEARED", "EMBERWING USED SPARK", "FIRE", 20, 4, 4},
|
||||
{"PEBBLIT", "LV FIVE", "WILD PEBBLIT APPEARED", "PEBBLIT USED ROLL", "ROCK", 22, 5, 9}
|
||||
};
|
||||
struct BattleState battle;
|
||||
unsigned random_state = 0x6d2b79f5u;
|
||||
int grass_steps;
|
||||
|
||||
TEST_INTERNAL unsigned next_random(void)
|
||||
{
|
||||
if (!random_state) random_state = 0x6d2b79f5u;
|
||||
random_state ^= random_state << 13;
|
||||
random_state ^= random_state >> 17;
|
||||
random_state ^= random_state << 5;
|
||||
return random_state;
|
||||
}
|
||||
|
||||
TEST_INTERNAL int start_battle(enum Species enemy)
|
||||
{
|
||||
if (!partner_owned || battle.phase != BATTLE_OFF || (unsigned)enemy >= SPECIES_COUNT)
|
||||
return 0;
|
||||
battle.phase = BATTLE_TURN;
|
||||
battle.enemy = enemy;
|
||||
battle.sprout_hp = SPROUT_HP;
|
||||
battle.enemy_hp = species[enemy].hp;
|
||||
battle.move = 0;
|
||||
battle.turns = 0;
|
||||
grass_steps = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int near_golden(int x, int y)
|
||||
{
|
||||
return current_room == OUTSIDE && x >= 163 && x <= 205 && y >= 84 && y <= 126;
|
||||
}
|
||||
|
||||
int begin_battle(int x, int y)
|
||||
{
|
||||
if (!near_golden(x, y)) return 0;
|
||||
return start_battle(GOLDEN);
|
||||
}
|
||||
|
||||
int check_grass_encounter(int old_x, int old_y, int x, int y)
|
||||
{
|
||||
if (!partner_owned || !in_tall_grass(x, y)) { grass_steps = 0; return 0; }
|
||||
if (battle.phase != BATTLE_OFF || (old_x == x && old_y == y)) return 0;
|
||||
++grass_steps;
|
||||
/* No encounters while idle, and a 24-pixel breather after every battle. */
|
||||
if (grass_steps < 24 || (grass_steps - 24) % 8) return 0;
|
||||
if ((next_random() & 3u) && grass_steps < 64) return 0;
|
||||
return start_battle((enum Species)(FERNFOX + next_random() % 3u));
|
||||
}
|
||||
|
||||
/* One fresh button press performs one turn. The golden creature replies. */
|
||||
int battle_input(unsigned pressed)
|
||||
{
|
||||
if (battle.phase == BATTLE_OFF || !pressed) return 0;
|
||||
if (battle.phase != BATTLE_TURN) {
|
||||
if (!(pressed & (A | B))) return 0;
|
||||
battle.phase = BATTLE_OFF;
|
||||
return 1;
|
||||
}
|
||||
if (pressed & B) {
|
||||
battle.phase = BATTLE_FLED;
|
||||
return 1;
|
||||
}
|
||||
if (pressed & LEFT) battle.move = 0;
|
||||
if (pressed & RIGHT) battle.move = 1;
|
||||
if (!(pressed & A)) return !!(pressed & (LEFT | RIGHT));
|
||||
battle.enemy_hp -= battle.move ? species[battle.enemy].leaf_damage : 4;
|
||||
++battle.turns;
|
||||
if (battle.enemy_hp <= 0) {
|
||||
battle.enemy_hp = 0;
|
||||
battle.phase = BATTLE_WON;
|
||||
} else {
|
||||
battle.sprout_hp -= species[battle.enemy].attack;
|
||||
if (battle.sprout_hp <= 0) {
|
||||
battle.sprout_hp = 0;
|
||||
battle.phase = BATTLE_LOST;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void health_bar(int x, int y, int hp, int maximum)
|
||||
{
|
||||
rectangle(x, y, 92, 6, RGB(7, 12, 10));
|
||||
rectangle(x + 1, y + 1, 90, 4, RGB(21, 22, 18));
|
||||
u16 color = hp * 4 <= maximum ? RGB(29, 9, 8) :
|
||||
hp * 2 <= maximum ? RGB(29, 23, 7) : RGB(10, 25, 12);
|
||||
rectangle(x + 1, y + 1, hp * 90 / maximum, 4, color);
|
||||
}
|
||||
|
||||
void draw_battle_hud(void)
|
||||
{
|
||||
u16 ink = RGB(5, 8, 11), cream = RGB(30, 30, 26);
|
||||
health_bar(16, 44, battle.enemy_hp, species[battle.enemy].hp);
|
||||
health_bar(136, 97, battle.sprout_hp, SPROUT_HP);
|
||||
rectangle(8, 111, 224, 43, ink);
|
||||
rectangle(10, 113, 220, 39, cream);
|
||||
if (battle.phase == BATTLE_TURN) {
|
||||
large_text(22, 119, "TACKLE", ink);
|
||||
large_text(132, 119, "LEAF TAP", ink);
|
||||
int arrow_x = battle.move ? 122 : 12;
|
||||
rectangle(arrow_x, 119, 2, 7, RGB(7, 17, 11));
|
||||
rectangle(arrow_x + 2, 121, 2, 3, RGB(7, 17, 11));
|
||||
text(16, 132, battle.turns ? species[battle.enemy].reply :
|
||||
"WHAT WILL SPROUT DO", ink);
|
||||
text(16, 143, "LEFT RIGHT CHOOSE A ATTACK B RUN", ink);
|
||||
} else {
|
||||
const char *result = battle.phase == BATTLE_WON ? "YOU WON" :
|
||||
battle.phase == BATTLE_LOST ? "SPROUT NEEDS A REST" : "YOU GOT AWAY SAFELY";
|
||||
large_text(20, 120, result, ink);
|
||||
text(20, 134, "A BACK TO ADVENTURE", ink);
|
||||
text(20, 144, "SPROUT WILL REST AND RECOVER", ink);
|
||||
}
|
||||
}
|
||||
|
||||
void draw_battle(void)
|
||||
{
|
||||
rectangle(0, 0, WIDTH, HEIGHT, RGB(28, 29, 23));
|
||||
status(species[battle.enemy].intro);
|
||||
rectangle(0, 18, WIDTH, 44, RGB(23, 29, 25));
|
||||
rectangle(0, 62, WIDTH, 49, RGB(18, 26, 15));
|
||||
rectangle(155, battle.enemy == GOLDEN ? 61 : 70, 64, 5, RGB(12, 21, 11));
|
||||
rectangle(18, 104, 78, 6, RGB(12, 21, 11));
|
||||
static const u16 gold_palette[] = {
|
||||
0, RGB(9, 8, 6), RGB(23, 15, 5), RGB(30, 23, 7),
|
||||
RGB(31, 28, 14), RGB(31, 31, 28), RGB(29, 13, 10), RGB(17, 8, 7)
|
||||
};
|
||||
if (battle.enemy == GOLDEN) scaled_creature(184, 62, 2, gold_palette);
|
||||
else draw_wild_creature(battle.enemy, 184, 71);
|
||||
draw_sprout(57, 106, 3);
|
||||
rectangle(10, 24, 106, 29, RGB(30, 30, 26));
|
||||
large_text(16, 28, species[battle.enemy].name, RGB(5, 8, 11));
|
||||
text(16, 37, species[battle.enemy].type, RGB(5, 8, 11));
|
||||
text(69, 37, species[battle.enemy].level, RGB(5, 8, 11));
|
||||
rectangle(130, 76, 102, 31, RGB(30, 30, 26));
|
||||
large_text(136, 80, "SPROUT", RGB(5, 8, 11));
|
||||
text(179, 90, "LV FIVE", RGB(5, 8, 11));
|
||||
draw_battle_hud();
|
||||
}
|
||||
Reference in new issue
Block a user