Create GBA adventure with walkable map and NPC dialogue
This commit is contained in:
commit
b4017d5c1a
10 files changed
+623
No files matched your search
+253
@@ -0,0 +1,253 @@
|
||||
/* Our first GBA game. No downloaded game art or existing ROM is used. */
|
||||
typedef unsigned short u16;
|
||||
|
||||
#define RGB(r, g, b) ((u16)((r) | ((g) << 5) | ((b) << 10)))
|
||||
#define DISPLAY (*(volatile u16 *)0x04000000)
|
||||
#define VCOUNT (*(volatile u16 *)0x04000006)
|
||||
#define KEYS (*(volatile u16 *)0x04000130)
|
||||
#define SCREEN ((volatile u16 *)0x06000000)
|
||||
#define WIDTH 240
|
||||
#define HEIGHT 160
|
||||
|
||||
enum { A = 1, START = 8, RIGHT = 16, LEFT = 32, UP = 64, DOWN = 128 };
|
||||
|
||||
/* These are good first things to change! Colors use numbers from 0 to 31. */
|
||||
static const u16 grass = RGB(12, 23, 11);
|
||||
static const u16 shirt = RGB(5, 14, 29);
|
||||
|
||||
enum { NPC_X = 146, NPC_Y = 106 };
|
||||
|
||||
/* Change these lines to write Mila's conversation! Use CAPITAL letters. */
|
||||
static const char *const dialogue[][2] = {
|
||||
{"HELLO I AM MILA", "WELCOME TO OUR VILLAGE"},
|
||||
{"THAT GOLDEN CREATURE IS FRIENDLY", "WALK OVER AND SAY HELLO"}
|
||||
};
|
||||
enum { DIALOGUE_PAGES = sizeof(dialogue) / sizeof(dialogue[0]) };
|
||||
|
||||
static int near_npc(int x, int y)
|
||||
{
|
||||
return x >= NPC_X - 24 && x <= NPC_X + 24 &&
|
||||
y >= NPC_Y - 20 && y <= NPC_Y + 20;
|
||||
}
|
||||
|
||||
static int next_dialogue_page(int page)
|
||||
{
|
||||
return page + 1 < DIALOGUE_PAGES ? page + 1 : -1;
|
||||
}
|
||||
|
||||
static void rectangle(int x, int y, int w, int h, u16 color)
|
||||
{
|
||||
for (int row = y; row < y + h; ++row) {
|
||||
for (int col = x; col < x + w; ++col) {
|
||||
if (col >= 0 && col < WIDTH && row >= 0 && row < HEIGHT)
|
||||
SCREEN[row * WIDTH + col] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Tiny original 3-by-5 alphabet: one row of bits per letter. */
|
||||
static const unsigned char alphabet[26][5] = {
|
||||
{2,5,7,5,5}, {6,5,6,5,6}, {3,4,4,4,3}, {6,5,5,5,6},
|
||||
{7,4,6,4,7}, {7,4,6,4,4}, {3,4,5,5,3}, {5,5,7,5,5},
|
||||
{7,2,2,2,7}, {1,1,1,5,2}, {5,5,6,5,5}, {4,4,4,4,7},
|
||||
{5,7,7,5,5}, {5,7,7,7,5}, {2,5,5,5,2}, {6,5,6,4,4},
|
||||
{2,5,5,3,1}, {6,5,6,5,5}, {3,4,2,1,6}, {7,2,2,2,2},
|
||||
{5,5,5,5,7}, {5,5,5,5,2}, {5,5,7,7,5}, {5,5,2,5,5},
|
||||
{5,5,2,2,2}, {7,1,2,4,7}
|
||||
};
|
||||
|
||||
static void text(int x, int y, const char *message, u16 color)
|
||||
{
|
||||
for (; *message; ++message, x += 4) {
|
||||
if (*message < 'A' || *message > 'Z') continue;
|
||||
const unsigned char *letter = alphabet[*message - 'A'];
|
||||
for (int row = 0; row < 5; ++row)
|
||||
for (int col = 0; col < 3; ++col)
|
||||
if (letter[row] & (4 >> col))
|
||||
rectangle(x + col, y + row, 1, 1, color);
|
||||
}
|
||||
}
|
||||
|
||||
/* Calculate the scenery at one pixel so we can erase the old player. */
|
||||
static u16 scenery(int x, int y)
|
||||
{
|
||||
if (y < 18 || y >= 142) return RGB(3, 7, 10);
|
||||
if (x < 8 || x >= 232 || y < 26 || y >= 136) {
|
||||
if ((x + y) % 9 < 2) return RGB(5, 13, 8);
|
||||
return RGB(6, 16, 8);
|
||||
}
|
||||
/* A sandy walking path. */
|
||||
u16 color = grass;
|
||||
if ((x > 96 && x < 120) || (y > 106 && y < 120))
|
||||
color = RGB(27, 25, 16);
|
||||
else if ((x * 7 + y * 11) % 43 == 0)
|
||||
color = RGB(9, 19, 8);
|
||||
/* Pond with a light shore. */
|
||||
if (x >= 22 && x < 76 && y >= 44 && y < 86) {
|
||||
color = RGB(24, 25, 15);
|
||||
if (x >= 25 && x < 73 && y >= 47 && y < 83)
|
||||
color = ((x + y * 2) % 23 < 5) ? RGB(13, 25, 29) : RGB(5, 17, 25);
|
||||
}
|
||||
/* Our little starting house. */
|
||||
if (x >= 147 && x < 206 && y >= 32 && y < 49)
|
||||
color = ((x + y) % 8 == 0) ? RGB(21, 7, 8) : RGB(26, 10, 10);
|
||||
if (x >= 152 && x < 201 && y >= 49 && y < 72) {
|
||||
color = RGB(29, 27, 20);
|
||||
if (x >= 172 && x < 183 && y >= 55) color = RGB(12, 8, 6);
|
||||
if (x >= 157 && x < 166 && y >= 54 && y < 62) color = RGB(8, 20, 28);
|
||||
if (x >= 187 && x < 196 && y >= 54 && y < 62) color = RGB(8, 20, 28);
|
||||
}
|
||||
/* Flower patches. */
|
||||
if (x > 126 && x < 146 && y > 77 && y < 98 && x % 7 < 2 && y % 7 < 2)
|
||||
color = RGB(31, 21, 7);
|
||||
return color;
|
||||
}
|
||||
|
||||
static void restore(int x, int y, int w, int h)
|
||||
{
|
||||
for (int row = y; row < y + h; ++row)
|
||||
for (int col = x; col < x + w; ++col)
|
||||
if (col >= 0 && col < WIDTH && row >= 0 && row < HEIGHT)
|
||||
SCREEN[row * WIDTH + col] = scenery(col, row);
|
||||
}
|
||||
|
||||
static void creature(void)
|
||||
{
|
||||
u16 gold = RGB(30, 23, 7), dark = RGB(7, 9, 10);
|
||||
rectangle(175, 102, 14, 3, RGB(8, 16, 7));
|
||||
rectangle(175, 88, 3, 6, gold);
|
||||
rectangle(185, 88, 3, 6, gold);
|
||||
rectangle(175, 93, 13, 9, gold);
|
||||
rectangle(178, 101, 3, 3, gold);
|
||||
rectangle(184, 101, 3, 3, gold);
|
||||
rectangle(178, 95, 2, 2, dark);
|
||||
rectangle(184, 95, 2, 2, dark);
|
||||
rectangle(181, 99, 2, 1, RGB(25, 10, 8));
|
||||
}
|
||||
|
||||
/* x and y mark the player's feet. */
|
||||
static void person(int x, int y, int step, u16 clothes, u16 hat)
|
||||
{
|
||||
u16 skin = RGB(30, 23, 17), dark = RGB(5, 7, 10);
|
||||
rectangle(x - 7, y - 1, 15, 3, RGB(8, 16, 7));
|
||||
rectangle(x - 5, y - 19, 10, 6, hat);
|
||||
rectangle(x - 6, y - 15, 13, 2, hat);
|
||||
rectangle(x - 4, y - 13, 8, 5, skin);
|
||||
rectangle(x - 2, y - 12, 1, 1, dark);
|
||||
rectangle(x + 2, y - 12, 1, 1, dark);
|
||||
rectangle(x - 5, y - 8, 10, 7, clothes);
|
||||
rectangle(x - 7, y - 7, 2, 5, skin);
|
||||
rectangle(x + 5, y - 7, 2, 5, skin);
|
||||
rectangle(x - 4, y - 1, 3, 3 - step, dark);
|
||||
rectangle(x + 1, y - 1, 3, 2 + step, dark);
|
||||
}
|
||||
|
||||
static void player(int x, int y, int step)
|
||||
{
|
||||
person(x, y, step, shirt, RGB(28, 8, 9));
|
||||
}
|
||||
|
||||
static void actors(int x, int y, int step)
|
||||
{
|
||||
/* Draw people behind others first, using the position of their feet. */
|
||||
if (y < 104) player(x, y, step);
|
||||
creature();
|
||||
if (y >= 104 && y < NPC_Y) player(x, y, step);
|
||||
person(NPC_X, NPC_Y, 0, RGB(4, 21, 13), RGB(12, 7, 4));
|
||||
if (y >= NPC_Y) player(x, y, step);
|
||||
}
|
||||
|
||||
static int can_walk(int x, int y)
|
||||
{
|
||||
if (x < 17 || x > 222 || y < 47 || y > 131) return 0;
|
||||
if (x > 16 && x < 82 && y > 42 && y < 92) return 0;
|
||||
if (x > 141 && x < 212 && y < 78) return 0;
|
||||
if (x > NPC_X - 13 && x < NPC_X + 13 &&
|
||||
y > NPC_Y - 9 && y < NPC_Y + 9) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void status(const char *message)
|
||||
{
|
||||
rectangle(0, 0, WIDTH, 18, RGB(3, 7, 10));
|
||||
text(8, 6, message, RGB(31, 30, 25));
|
||||
}
|
||||
|
||||
static void dialogue_box(int page)
|
||||
{
|
||||
u16 white = RGB(31, 30, 25);
|
||||
rectangle(8, 108, 224, 32, white);
|
||||
rectangle(10, 110, 220, 28, RGB(3, 7, 10));
|
||||
text(16, 113, "MILA", RGB(14, 29, 19));
|
||||
text(192, 113, page == DIALOGUE_PAGES - 1 ? "A CLOSE" : "A NEXT", white);
|
||||
text(16, 122, dialogue[page][0], white);
|
||||
text(16, 130, dialogue[page][1], white);
|
||||
}
|
||||
|
||||
static void wait_for_frame(void)
|
||||
{
|
||||
while (VCOUNT >= 160) { }
|
||||
while (VCOUNT < 160) { }
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* Mode 3 gives us a 240x160 canvas of 15-bit colors. */
|
||||
DISPLAY = 0x0080; /* Hide the screen until the scene is drawn. */
|
||||
restore(0, 0, WIDTH, HEIGHT);
|
||||
status("MY FIRST QUEST");
|
||||
text(8, 148, "ARROWS MOVE A TALK START RESET", RGB(31, 30, 25));
|
||||
int x = 108, y = 103, frame = 0, greeting = 0;
|
||||
int dialogue_page = -1; /* -1 means we are not talking. */
|
||||
unsigned previous = 0;
|
||||
actors(x, y, 0);
|
||||
DISPLAY = 0x0403;
|
||||
|
||||
for (;;) {
|
||||
wait_for_frame();
|
||||
unsigned keys = (~KEYS) & 0x03ff;
|
||||
unsigned pressed = keys & ~previous;
|
||||
previous = keys;
|
||||
/* Talking pauses walking. One fresh button press turns one page. */
|
||||
if (dialogue_page >= 0) {
|
||||
if (pressed & A) {
|
||||
dialogue_page = next_dialogue_page(dialogue_page);
|
||||
if (dialogue_page >= 0) {
|
||||
dialogue_box(dialogue_page);
|
||||
} else {
|
||||
restore(8, 108, 224, 32);
|
||||
actors(x, y, 0);
|
||||
status("MY FIRST QUEST");
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
int old_x = x, old_y = y;
|
||||
int next_x = x + !!(keys & RIGHT) - !!(keys & LEFT);
|
||||
int next_y = y + !!(keys & DOWN) - !!(keys & UP);
|
||||
if (can_walk(next_x, y)) x = next_x;
|
||||
if (can_walk(x, next_y)) y = next_y;
|
||||
if (pressed & START) { x = 108; y = 103; }
|
||||
if (pressed & A) {
|
||||
if (near_npc(x, y)) {
|
||||
dialogue_page = 0;
|
||||
greeting = 0;
|
||||
status("TALKING TO MILA");
|
||||
} else if (x > 154 && x < 209 && y > 79 && y < 125) {
|
||||
status("HELLO NEW FRIEND");
|
||||
greeting = 180;
|
||||
} else {
|
||||
status("WALK CLOSE TO MILA TO TALK");
|
||||
greeting = 180;
|
||||
}
|
||||
}
|
||||
if (greeting && --greeting == 0) status("MY FIRST QUEST");
|
||||
if (x != old_x || y != old_y) {
|
||||
restore(old_x - 8, old_y - 20, 17, 24);
|
||||
actors(x, y, (frame >> 3) & 1);
|
||||
}
|
||||
if (dialogue_page >= 0) dialogue_box(dialogue_page);
|
||||
++frame;
|
||||
if (frame == 1024) frame = 0;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user