add actor array and draw all actors to screen

This commit is contained in:
StrangeD0s
2024-04-14 15:44:42 +02:00
parent 1754ce9954
commit 6cd4b87b05
6 changed files with 274 additions and 149 deletions
+19 -45
View File
@@ -59,12 +59,12 @@ void move_actor(Actor *actor, uint8_t x, uint8_t y) // Evtl. sollte is_moving hi
// Move the metasprite
if (actor->is_facing_right)
{
move_metasprite_ex(actor->metasprite, 0, 0, 0, x + 8, y + 16);
move_metasprite_ex(actor->metasprite, 0, 0, actor->sprite_id, x + 8, y + 16);
}
if (!actor->is_facing_right)
{
// flip the metasprite horizontally when is_facing_right is TRUE
move_metasprite_flipx(actor->metasprite, 0, 0, 0, x + 8, y + 16);
move_metasprite_flipx(actor->metasprite, 0, 0, actor->sprite_id, x + 8, y + 16);
}
}
@@ -205,50 +205,24 @@ void move_actor_with_joypad(Actor *actor, uint8_t framecount)
scroll_actor(actor);
}
void setup_actor(Actor *actor, uint8_t sprite_id, uint8_t tile_width, uint8_t tile_height, uint8_t tilemap_start, uint8_t total_frames, bool is_facing_right, const unsigned char *tilemap, const metasprite_t *metasprite)
void setup_actor(Actor *actor, uint8_t sprite_id, uint8_t tile_width, uint8_t tile_height, uint8_t tilemap_start, uint8_t total_frames, bool is_facing_right, uint8_t x, uint8_t y, const unsigned char *tilemap, const metasprite_t *metasprite)
{
actor->sprite_id = sprite_id; // Set player sprite ID
actor->x = x; // Set initial X
actor->y = y; // Set initial Y
actor->next_x = x; // Set initial next X
actor->next_y = y; // Set initial next Y
actor->is_facing_right = is_facing_right; // Set horizontal direction of
actor->is_moving = FALSE; // Set if the actor is moving
actor->tilemap = tilemap; // Store pointer to tilemap
actor->tilemap_start = tilemap_start; // Set tile start position (in tileset)
actor->sprite_tile_width = tile_width; // Set width
actor->sprite_tile_height = tile_height; // Set height
actor->sprite_frame = total_frames; // Set number of frames
actor->sprite_current_frame = 0; // Make sure to start at frame 1
actor->metasprite = metasprite; // Store pointer to metasprite
// Set initial X
actor->x = 80;
// move_metasprite_ex(actor->metasprite, 0, 0, actor->sprite_id, x + 8, y + 16);
// Set initial Y
actor->y = 90;
// Set initial next X
actor->next_x = 80;
// Set initial next Y
actor->next_y = 90;
// Set horizontal direction of
actor->is_facing_right = is_facing_right;
// Set if the actor is moving
actor->is_moving = FALSE;
// Store pointer to tilemap
actor->tilemap = tilemap;
// Set tile start position (in tileset)
actor->tilemap_start = tilemap_start;
// Set player sprite ID
actor->sprite_id = sprite_id;
// Set size
actor->sprite_tile_width = tile_width;
actor->sprite_tile_height = tile_height;
// Set number of frames
actor->sprite_frame = total_frames;
// Make sure to start at frame 1
actor->sprite_current_frame = 0;
// Move to built in metasprite methods
// load_sprite_frame(actor, 0);
// Store pointer to metasprite
actor->metasprite = metasprite;
move_actor(actor, actor->x, actor->y);
move_actor(actor, x, y);
}
+1 -1
View File
@@ -38,4 +38,4 @@ void scroll_actor(Actor *actor);
void move_actor_with_joypad(Actor *actor, uint8_t framecount);
void setup_actor(Actor *actor, uint8_t sprite_id, uint8_t tile_width, uint8_t tile_height, uint8_t tilemap_start, uint8_t total_frames, bool is_facing_right, const unsigned char *tilemap, const metasprite_t *metasprite);
void setup_actor(Actor *actor, uint8_t sprite_id, uint8_t tile_width, uint8_t tile_height, uint8_t tilemap_start, uint8_t total_frames, bool is_facing_right, uint8_t x, uint8_t y, const unsigned char *tilemap, const metasprite_t *metasprite);
+99 -22
View File
@@ -6,15 +6,19 @@
#include "../res/hud.h"
#include "../res/underwater_tiles.h"
#include "../res/underwater_map.h"
#include "../res/submarine_sprites.h"
// #include "../res/submarine_sprites.h"
#include "../res/test-tileset.h"
#include "../res/test-tilemap.h"
#include "../res/Sprites.h"
#include "actor.h"
// #include "input.h"
#include "collision.h"
Actor submarine;
Actor squid1;
Actor squid2;
Actor fish1;
uint8_t framecount;
uint8_t two_frame_counter = 0;
@@ -23,6 +27,8 @@ uint8_t two_frame_real_value = 0;
#define TILEMAP_WIDTH_IN_TILES (tilemap_WIDTH >> 3)
#define TILEMAP_HEIGHT_IN_TILES (tilemap_HEIGHT >> 3)
#define _countof(x) (sizeof(x) / sizeof((x)[0]))
// animation counter and the directional controls update the player's location on every 2nd frame
// from https://gbdev.gg8.se/forums/viewtopic.php?id=850
void animcounter(uint8_t numframes)
@@ -100,6 +106,91 @@ void shiftBGTiles(void)
}
}
void setup(void)
{
struct Entity
{
Actor *actor;
uint8_t id; // Max 40 because of sprite limit on screen. Must be 1 larger than the previous metasprite
uint8_t width;
uint8_t height;
uint8_t startframe;
uint8_t total_frame;
bool is_facing_right;
uint8_t initialX;
uint8_t initialY;
const unsigned char *tilemap;
const metasprite_t *metasprite;
};
// make array of actor entities
struct Entity entities[4]; //! Ich könnte stattdessen wohl auch "struct Actor entities[2]" machen und direkt den Actor struct benutzen
entities[0].actor = &submarine;
entities[0].id = 0;
entities[0].width = 2;
entities[0].height = 2;
entities[0].startframe = 2;
entities[0].total_frame = 2;
entities[0].is_facing_right = TRUE;
entities[0].initialX = 80;
entities[0].initialY = 90;
entities[0].tilemap = submarine_tilemap;
entities[0].metasprite = submarine_metasprite;
entities[1].actor = &squid1;
entities[1].id = 4;
entities[1].width = 2;
entities[1].height = 2;
entities[1].startframe = 2;
entities[1].total_frame = 2;
entities[1].is_facing_right = TRUE;
entities[1].initialX = 20;
entities[1].initialY = 80;
entities[1].tilemap = squid_tilemap;
entities[1].metasprite = squid_metasprite;
entities[2].actor = &squid2;
entities[2].id = 8;
entities[2].width = 2;
entities[2].height = 2;
entities[2].startframe = 2;
entities[2].total_frame = 2;
entities[2].is_facing_right = TRUE;
entities[2].initialX = 120;
entities[2].initialY = 120;
entities[2].tilemap = squid_tilemap;
entities[2].metasprite = squid_metasprite;
entities[3].actor = &fish1;
entities[3].id = 12;
entities[3].width = 2;
entities[3].height = 2;
entities[3].startframe = 2;
entities[3].total_frame = 2;
entities[3].is_facing_right = TRUE;
entities[3].initialX = 80;
entities[3].initialY = 30;
entities[3].tilemap = fish_tilemap;
entities[3].metasprite = fish_metasprite;
for (int i = 0; i < _countof(entities); ++i)
{
// Setup actors
setup_actor(
entities[i].actor, // player
entities[i].id, // Sprite ID: 0
entities[i].width, entities[i].height, // 2x2 meta-sprite
entities[i].startframe, // Tiles start at 0
entities[i].total_frame, // Total animated frames
entities[i].is_facing_right, // Set is_facing_right bool
entities[i].initialX, // Set initial x
entities[i].initialY, // Set initial x
entities[i].tilemap, // pointer to the tilemap
entities[i].metasprite); // pointer to the metasprite
}
}
void init(void)
{
@@ -121,29 +212,15 @@ void init(void)
set_bkg_tiles(0, 0, TILEMAP_WIDTH_IN_TILES, TILEMAP_HEIGHT_IN_TILES, tilemap_map);
// Fill tileset bank 0 with 4 actor tiles
set_sprite_data(0, 8, submarine_tileset);
set_sprite_data(0, 20, sprites);
//! Prüfen, welche variables für das metasprite überhaupt noch benötigt werden
// Setup player actor
setup_actor(
// player
&submarine,
// Sprite ID: 0
0,
// 2x2 meta-sprite
2, 2,
// Tiles start at 0
0,
// Total animated frames
4,
// Set is_facing_right bool
TRUE,
// pointer to the tilemap
submarine_tilemap,
// pointer to the metasprite
submarine_metasprite);
setup();
// uint8_t startingSprite = 0;
// MoveCharacter(&submarine, 80, 90);
// startingSprite += move_metasprite_ex(squid_metasprite, 0, 0, startingSprite, 20 + 8, 80 + 16);
// startingSprite += move_metasprite_ex(submarine_metasprite, 0, 0, startingSprite, 80 + 8, 90 + 16);
// MoveCharacter(&submarine, 80, 90);
// Show the HUD
// show_hud();
@@ -167,9 +244,9 @@ void main(void)
update_two_frame_counter();
animate_actor(&submarine, two_frame_counter);
// animate_actor(&squid, two_frame_counter);
move_actor_with_joypad(&submarine, framecount);
// joypad_input(&submarine);
animcounter(96); // I picked 96 because of how many factors it has. Easy to have different spaced timings that don't stutter when the counter loops.
}