Extracted JokerObject and CardObject common functionalities to SpriteObject, removing duplicate code and dangerous casts.
This commit is contained in:
+134
-1
@@ -1,7 +1,9 @@
|
||||
#include "sprite.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <tonc.h>
|
||||
#include <stdlib.h>
|
||||
#include <maxmod.h>
|
||||
|
||||
#define MAX_SPRITES 128
|
||||
#define MAX_AFFINES 32
|
||||
@@ -96,4 +98,135 @@ void sprite_draw()
|
||||
obj_copy(obj_mem, obj_buffer, MAX_SPRITES);
|
||||
obj_aff_copy(obj_aff_mem, obj_aff_buffer, MAX_AFFINES);
|
||||
oam_copy(oam_mem, obj_buffer, MAX_SPRITES);
|
||||
}
|
||||
}
|
||||
|
||||
// SpriteObject methods
|
||||
SpriteObject* sprite_object_new()
|
||||
{
|
||||
SpriteObject* sprite_object = (SpriteObject*)malloc(sizeof(SpriteObject));
|
||||
sprite_object->sprite = NULL;
|
||||
sprite_object_reset_transform(sprite_object);
|
||||
sprite_object->selected = false;
|
||||
|
||||
return sprite_object;
|
||||
}
|
||||
|
||||
void sprite_object_destroy(SpriteObject** sprite_object)
|
||||
{
|
||||
if (*sprite_object == NULL) return;
|
||||
sprite_destroy(&((*sprite_object)->sprite));
|
||||
free(*sprite_object);
|
||||
*sprite_object = NULL;
|
||||
}
|
||||
|
||||
void sprite_object_set_sprite(SpriteObject* sprite_object, Sprite* sprite)
|
||||
{
|
||||
if (sprite_object == NULL)
|
||||
return;
|
||||
sprite_destroy(&sprite_object->sprite); // Destroy the old sprite if it exists
|
||||
sprite_object->sprite = sprite;
|
||||
}
|
||||
|
||||
void sprite_object_reset_transform(SpriteObject* sprite_object)
|
||||
{
|
||||
sprite_object->tx = 0; // Target position
|
||||
sprite_object->ty = 0;
|
||||
sprite_object->x = 0;
|
||||
sprite_object->y = 0;
|
||||
sprite_object->vx = 0;
|
||||
sprite_object->vy = 0;
|
||||
sprite_object->tscale = float2fx(1.0f); // Target scale
|
||||
sprite_object->scale = float2fx(1.0f);
|
||||
sprite_object->vscale = float2fx(0.0f);
|
||||
sprite_object->trotation = 0; // Target rotation
|
||||
sprite_object->rotation = 0;
|
||||
sprite_object->vrotation = 0;
|
||||
}
|
||||
|
||||
void sprite_object_update(SpriteObject* sprite_object)
|
||||
{
|
||||
sprite_object->vx += (sprite_object->tx - sprite_object->x) / 8;
|
||||
sprite_object->vy += (sprite_object->ty - sprite_object->y) / 8;
|
||||
|
||||
sprite_object->vscale += (sprite_object->tscale - sprite_object->scale) / 8; // Scale up the card when it's played
|
||||
|
||||
sprite_object->vrotation += (sprite_object->trotation - sprite_object->rotation) / 8; // Rotate the card when it's played
|
||||
|
||||
// set velocity to 0 if it's close enough to the target
|
||||
const float epsilon = float2fx(0.01f);
|
||||
if (sprite_object->vx < epsilon && sprite_object->vx > -epsilon && sprite_object->vy < epsilon && sprite_object->vy > -epsilon)
|
||||
{
|
||||
sprite_object->vx = 0;
|
||||
sprite_object->vy = 0;
|
||||
|
||||
sprite_object->x = sprite_object->tx;
|
||||
sprite_object->y = sprite_object->ty;
|
||||
}
|
||||
else
|
||||
{
|
||||
sprite_object->vx = (sprite_object->vx * 7) / 10;
|
||||
sprite_object->vy = (sprite_object->vy * 7) / 10;
|
||||
|
||||
sprite_object->x += sprite_object->vx;
|
||||
sprite_object->y += sprite_object->vy;
|
||||
}
|
||||
|
||||
// Set scale to 0 if it's close enough to the target
|
||||
if (sprite_object->vscale < epsilon && sprite_object->vscale > -epsilon)
|
||||
{
|
||||
sprite_object->vscale = 0;
|
||||
sprite_object->scale = sprite_object->tscale; // Set the scale to the target scale
|
||||
}
|
||||
else
|
||||
{
|
||||
sprite_object->vscale = (sprite_object->vscale * 7) / 10;
|
||||
sprite_object->scale += sprite_object->vscale;
|
||||
}
|
||||
|
||||
// Set rotation to 0 if it's close enough to the target
|
||||
if (sprite_object->vrotation < epsilon && sprite_object->vrotation > -epsilon)
|
||||
{
|
||||
sprite_object->vrotation = 0;
|
||||
sprite_object->rotation = sprite_object->trotation; // Set the rotation to the target rotation
|
||||
}
|
||||
else
|
||||
{
|
||||
sprite_object->vrotation = (sprite_object->vrotation * 7) / 10;
|
||||
sprite_object->rotation += sprite_object->vrotation;
|
||||
}
|
||||
|
||||
obj_aff_rotscale(sprite_object->sprite->aff, sprite_object->scale, sprite_object->scale, -sprite_object->vx + sprite_object->rotation); // Apply rotation and scale to the sprite
|
||||
sprite_position(sprite_object->sprite, fx2int(sprite_object->x), fx2int(sprite_object->y));
|
||||
}
|
||||
|
||||
void sprite_object_shake(SpriteObject* sprite_object, mm_word sound_id)
|
||||
{
|
||||
sprite_object->vscale = float2fx(0.3f); // Scale down the card when it's scored
|
||||
sprite_object->vrotation = float2fx(8.0f); // Rotate the card when it's scored
|
||||
|
||||
if (sound_id == UNDEFINED) return; // If no sound ID is provided, do nothing
|
||||
|
||||
mm_sound_effect sfx_select = { {sound_id}, 1024, 0, 255, 128, };
|
||||
mmEffectEx(&sfx_select);
|
||||
}
|
||||
|
||||
void sprite_object_set_selected(SpriteObject* sprite_object, bool selected)
|
||||
{
|
||||
if (sprite_object == NULL)
|
||||
return;
|
||||
sprite_object->selected = selected;
|
||||
}
|
||||
|
||||
bool sprite_object_is_selected(SpriteObject* sprite_object)
|
||||
{
|
||||
if (sprite_object == NULL)
|
||||
return false;
|
||||
return sprite_object->selected;
|
||||
}
|
||||
|
||||
Sprite* sprite_object_get_sprite(SpriteObject* sprite_object)
|
||||
{
|
||||
if (sprite_object == NULL)
|
||||
return NULL;
|
||||
return sprite_object->sprite;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user