Browse Source

Integrate actor system

master
Forest Belton 3 years ago
parent
commit
3b32124a99
8 changed files with 53 additions and 41 deletions
  1. +0
    -0
      assets/player.png
  2. +24
    -28
      src/actor.c
  3. +3
    -9
      src/actor.h
  4. +8
    -1
      src/level.c
  5. +1
    -1
      src/main.c
  6. +2
    -1
      src/map.c
  7. +12
    -1
      src/player.c
  8. +3
    -0
      src/player.h

assets/player-stand.png → assets/player.png View File


+ 24
- 28
src/actor.c View File

@ -2,6 +2,7 @@
#include <stddef.h> #include <stddef.h>
#include "game.h"
#include "player.h" #include "player.h"
#include "sdk/oam.h" #include "sdk/oam.h"
#include "util.h" #include "util.h"
@ -16,8 +17,7 @@
static actor_anim_state_t ANIM_TOTAL_FRAMES[] = {2}; static actor_anim_state_t ANIM_TOTAL_FRAMES[] = {2};
static uint8_t mob_ids[MAX_UNIQUE_MOBS]; static uint8_t mob_ids[MAX_UNIQUE_MOBS];
static sprite16_mob mob_anim_data[MAX_UNIQUE_MOBS];
static sprite16_player player_anim_data;
static uint8_t *mob_anim_data[MAX_UNIQUE_MOBS];
static actor_t all_actors[MAX_ACTORS]; static actor_t all_actors[MAX_ACTORS];
static uint8_t actor_load_mob_anim_data(uint8_t mob_id); static uint8_t actor_load_mob_anim_data(uint8_t mob_id);
@ -28,6 +28,9 @@ void actor_init(void) {
actor_reset(); actor_reset();
PLAYER_ACTOR->active = 1; PLAYER_ACTOR->active = 1;
PLAYER_ACTOR->anim = ANIM_STAND;
PLAYER_ACTOR->frame_idx = 0;
PLAYER_ACTOR->frame_counter = 0;
PLAYER_ACTOR->x = PLAYER.x; PLAYER_ACTOR->x = PLAYER.x;
PLAYER_ACTOR->y = PLAYER.y; PLAYER_ACTOR->y = PLAYER.y;
} }
@ -70,10 +73,12 @@ actor_t *actor_create_mob(uint8_t id) {
void actor_remove(actor_t *a) { a->active = 0; } void actor_remove(actor_t *a) { a->active = 0; }
void actor_update(void) { void actor_update(void) {
struct oam_entry *oam_ptr = &shadow_oam[0];
actor_t *a = &all_actors[0]; actor_t *a = &all_actors[0];
for (uint8_t i = 0; i < ARRSIZE(all_actors); ++i) { for (uint8_t i = 0; i < ARRSIZE(all_actors); ++i) {
if (!a->active) { if (!a->active) {
a++;
continue; continue;
} }
@ -83,44 +88,35 @@ void actor_update(void) {
a->frame_idx = (a->frame_idx + 1) % ANIM_TOTAL_FRAMES[a->anim]; a->frame_idx = (a->frame_idx + 1) % ANIM_TOTAL_FRAMES[a->anim];
} }
a++;
}
}
void actor_flush_oam(void) {
struct oam_entry *oam_ptr = &shadow_oam[0];
actor_t *a = &all_actors[0];
for (uint8_t i = 0; i < ARRSIZE(all_actors); ++i) {
if (!a->active) {
a++;
continue;
const uint8_t *anim_ptr;
if (i == 0) {
anim_ptr = &player_map[0];
} else {
anim_ptr = mob_anim_data[a->mob_anim_idx];
} }
uint8_t *anim_ptr = mob_anim_data[a->mob_anim_idx].anims[a->anim];
anim_ptr += a->frame_idx * 4; anim_ptr += a->frame_idx * 4;
oam_ptr->y = a->y;
oam_ptr->x = a->x;
oam_ptr->tile = *anim_ptr++;
oam_ptr->y = a->y + 16;
oam_ptr->x = a->x + 8;
oam_ptr->tile = *anim_ptr++ + TILE_INDEX_PLAYER;
oam_ptr->attr = 0; oam_ptr->attr = 0;
oam_ptr++; oam_ptr++;
oam_ptr->y = a->y;
oam_ptr->x = a->x + TILE_WIDTH;
oam_ptr->tile = *anim_ptr++;
oam_ptr->y = a->y + 16;
oam_ptr->x = a->x + TILE_WIDTH + 8;
oam_ptr->tile = *anim_ptr++ + TILE_INDEX_PLAYER;
oam_ptr->attr = 0; oam_ptr->attr = 0;
oam_ptr++; oam_ptr++;
oam_ptr->y = a->y + TILE_WIDTH;
oam_ptr->x = a->x;
oam_ptr->tile = *anim_ptr++;
oam_ptr->y = a->y + TILE_WIDTH + 16;
oam_ptr->x = a->x + 8;
oam_ptr->tile = *anim_ptr++ + TILE_INDEX_PLAYER;
oam_ptr->attr = 0; oam_ptr->attr = 0;
oam_ptr++; oam_ptr++;
oam_ptr->y = a->y + TILE_WIDTH;
oam_ptr->x = a->x + TILE_WIDTH;
oam_ptr->tile = *anim_ptr++;
oam_ptr->y = a->y + TILE_WIDTH + 16;
oam_ptr->x = a->x + TILE_WIDTH + 8;
oam_ptr->tile = *anim_ptr++ + TILE_INDEX_PLAYER;
oam_ptr->attr = 0; oam_ptr->attr = 0;
oam_ptr++; oam_ptr++;

+ 3
- 9
src/actor.h View File

@ -30,17 +30,15 @@ typedef struct {
} actor_t; } actor_t;
typedef struct { typedef struct {
uint8_t *tiles;
uint16_t num_tiles;
uint8_t *anims[ANIM_HURT + 1]; uint8_t *anims[ANIM_HURT + 1];
} sprite16_mob; } sprite16_mob;
typedef struct { typedef struct {
uint8_t *tiles;
uint16_t num_tiles;
uint8_t *anims[ANIM_CLIMB + 1]; uint8_t *anims[ANIM_CLIMB + 1];
} sprite16_player; } sprite16_player;
void actor_init(void);
/** /**
* @brief Clear all actor data. * @brief Clear all actor data.
*/ */
@ -64,11 +62,7 @@ void actor_remove(actor_t *a);
*/ */
void actor_update(void); void actor_update(void);
/**
* @brief Flush actor sprite data to OAM.
*/
void actor_flush_oam(void);
extern actor_t *PLAYER_ACTOR; extern actor_t *PLAYER_ACTOR;
extern sprite16_player player_anim_data;
#endif #endif

+ 8
- 1
src/level.c View File

@ -1,25 +1,32 @@
#include "actor.h"
#include "game.h" #include "game.h"
#include "intro.h" #include "intro.h"
#include "map.h" #include "map.h"
#include "player.h" #include "player.h"
#include "sdk/hardware.h" #include "sdk/hardware.h"
#include "sdk/joypad.h" #include "sdk/joypad.h"
#include "sdk/oam.h"
#include "sdk/video.h" #include "sdk/video.h"
#include "vram.h" #include "vram.h"
void level(void) { void level(void) {
lcd_disable(); lcd_disable();
map_load(&map_intro); map_load(&map_intro);
player_init();
actor_init();
lcd_enable(); lcd_enable();
while (1) { while (1) {
joypad_update(); joypad_update();
player_update(); player_update();
actor_flush_oam();
actor_update();
HALT(); HALT();
rIF = 0; rIF = 0;
vram_update(); vram_update();
oam_dma_copy();
} }
} }

+ 1
- 1
src/main.c View File

@ -2,6 +2,7 @@
#include "actor.h" #include "actor.h"
#include "game.h" #include "game.h"
#include "player.h"
#include "sdk/hardware.h" #include "sdk/hardware.h"
#include "sdk/oam.h" #include "sdk/oam.h"
#include "vram.h" #include "vram.h"
@ -11,7 +12,6 @@ game_state_t game_state;
void main() { void main() {
game_state = GAME_STATE_TITLE; game_state = GAME_STATE_TITLE;
// Setup the OAM for sprite drawing
oam_init(); oam_init();
vram_init(); vram_init();

+ 2
- 1
src/map.c View File

@ -32,7 +32,8 @@ void map_load(map_t *map) {
rSCY = INIT_SCY; rSCY = INIT_SCY;
PLAYER.x = (MAP.spawn_x - MAP.camera_x) * 8; PLAYER.x = (MAP.spawn_x - MAP.camera_x) * 8;
PLAYER.y = (MAP.spawn_y - MAP.camera_y) * 8;
uint8_t y = (MAP.spawn_y - MAP.camera_y) * 8;
PLAYER.y = y << 8;
memcpy(VRAM_TILE_PTR(TILE_INDEX_BACKGROUND), (uint8_t *)MAP.tile_ptr, memcpy(VRAM_TILE_PTR(TILE_INDEX_BACKGROUND), (uint8_t *)MAP.tile_ptr,
MAP.tile_size); MAP.tile_size);

+ 12
- 1
src/player.c View File

@ -1,7 +1,10 @@
#include "player.h" #include "player.h"
#include <string.h>
#include "actor.h" #include "actor.h"
#include "game.h" #include "game.h"
#include "sdk/assets.h"
#include "sdk/joypad.h" #include "sdk/joypad.h"
#define PLAYER_SPEED 2 #define PLAYER_SPEED 2
@ -12,6 +15,14 @@
player_t PLAYER; player_t PLAYER;
ASSET(player_tiles, "player.2bpp");
ASSET(player_map, "player.map");
void player_init(void) {
memcpy(VRAM_TILE_PTR(TILE_INDEX_PLAYER), &player_tiles[0],
player_tiles_end - player_tiles);
}
void player_update(void) { void player_update(void) {
if ((joypad_pressed & PAD_UP) && PLAYER.state != PLAYER_STATE_JUMP) { if ((joypad_pressed & PAD_UP) && PLAYER.state != PLAYER_STATE_JUMP) {
PLAYER.state = PLAYER_STATE_JUMP; PLAYER.state = PLAYER_STATE_JUMP;
@ -51,5 +62,5 @@ void player_update(void) {
} }
PLAYER_ACTOR->x = PLAYER.x; PLAYER_ACTOR->x = PLAYER.x;
PLAYER_ACTOR->y = PLAYER.y;
PLAYER_ACTOR->y = PLAYER.y >> 8;
} }

+ 3
- 0
src/player.h View File

@ -21,6 +21,9 @@ typedef struct {
} player_t; } player_t;
extern player_t PLAYER; extern player_t PLAYER;
extern const uint8_t player_map[];
void player_init(void);
void player_update(void); void player_update(void);

Loading…
Cancel
Save