right now I use an animation class to handle my sprite sheets, I want to add a destorying ore animation with 3 frames per ore. the animation in total is only 1 row per ore, and theres 4 rows in total, each row index corresponds to the ore type, i.e row 1 (index 0) is for ore0's 3 states. I have this full ore sprite sheet and I want you to implement it into my game. Each new state should be triggered by the HEALTH of the ore/tile, as in since theres 3 frames of sprites per ore, make 2 thresholds of tile health that make the next sprite be displayed. So for example at 100% health; display frame 1, at 66% health, frame 2 etc,
heres my animation class:
class Animation {
int row; // The row in the sprite sheet
int frameCount; // Total frames in this animation
int frameDelay; // Controls animation speed (frames per sprite frame)
boolean loop; // Should the animation loop?
code
private int frameCounter = 0; // Counts frames to control animation speed
private int currentFrame = 0; // Current frame index
Animation(int row, int frameCount, int frameDelay, boolean loop) {
this.row = row;
this.frameCount = frameCount;
this.frameDelay = frameDelay;
this.loop = loop;
}
// Updates the animation frame based on frameDelay
void update() {
frameCounter++;
if (frameCounter >= frameDelay) {
frameCounter = 0;
currentFrame++;
if (currentFrame >= frameCount) {
if (loop) {
currentFrame = 0;
} else {
currentFrame = frameCount - 1; // Hold on the last frame if not looping
}
}
}
}
// Resets the animation to the first frame
void reset() {
frameCounter = 0;
currentFrame = 0;
}
// Retrieves the current frame index
int getCurrentFrame() {
return currentFrame;
}
}
heres my tile class (where ores are considered tile objects)
class Tile {
TileType type;
int health;
code
Assistant
Tile(TileType type) {
this.type = type;
this.health = getMaxHealth();
}
void display(float x, float y) {
switch (type){
case EMPTY:
//image(oreSprites[4], x, y, TILE_SIZE, TILE_SIZE);
fill(48,48,48);
rect(x, y, TILE_SIZE, TILE_SIZE);
break;
case ROCK:
image(oreSprites[3], x, y, TILE_SIZE, TILE_SIZE);
break;
case ORE0 :
image(oreSprites[0], x, y, TILE_SIZE, TILE_SIZE);
break;
case ORE1:
image(oreSprites[1], x, y, TILE_SIZE, TILE_SIZE);
break;
case ORE2:
image(oreSprites[2], x, y, TILE_SIZE, TILE_SIZE);
break;
case GOAL:
fill(0, 255, 0);
rect(x, y, TILE_SIZE, TILE_SIZE);
break;
}
}
TileType mine() {
if (type != TileType.EMPTY && type != TileType.GOAL) {
health--;
if (health <= 0) {
TileType minedType = type;
type = TileType.EMPTY;
soundManager.stopMiningSound();
return minedType;
}
}
return null;
}
code
void takeDamage(int damage) {
if (type != TileType.EMPTY) {
health -= damage;
if (health <= 0) {
type = TileType.EMPTY;
}
}
}
int getMaxHealth() {
switch(type) {
case ROCK : return 35;
case ORE0 : return 200;
case ORE1 : return 120;
case ORE2 : return 75;
default : return 0;
}
}
}
please adjust the code to the needs of my requirements
To adjust your code to match your requirements for displaying different animation frames based on the health of the ore tiles, you'll need to make several modifications:
Extract Frames from the Sprite Sheet: Since your sprite sheet contains multiple frames per ore type, you'll need to extract those frames into a usable format.
Modify the Tile Class: Add fields to store the maximum health and the ore type index. Update the constructor and methods to handle these new fields.
Calculate the Correct Frame Based on Health: In the display method, calculate which frame to display based on the current health of the tile.
Update Health Management Methods: Ensure that when a tile's health reaches zero, its type and ore type index are updated accordingly.
Here's the adjusted code:
java
// Assume this code is part of your main class or setup function where you load images
PImage oreSpriteSheet = loadImage("ore_spritesheet.png"); // Load your sprite sheet image
final int NUM_ORES = 4; // Number of ore types including ROCK
final int FRAMES_PER_ORE = 3; // Number of frames per ore type
PImage[][] oreSprites = new PImage[NUM_ORES][FRAMES_PER_ORE]; // 2D array for ore sprites
int frameWidth = oreSpriteSheet.width / FRAMES_PER_ORE;
int frameHeight = oreSpriteSheet.height / NUM_ORES;
for (int oreType = 0; oreType < NUM_ORES; oreType++) {
for (int frameIndex = 0; frameIndex < FRAMES_PER_ORE; frameIndex++) {
oreSprites[oreType][frameIndex] = oreSpriteSheet.get(
frameIndex * frameWidth,
oreType * frameHeight,
frameWidth,
frameHeight
);
}
}
// Adjust your Tile class:
class Tile {
TileType type;
int health;
int maxHealth;
int oreTypeIndex;
Tile(TileType type) {
this.type = type;
this.health = getMaxHealth();
this.maxHealth = this.health;
this.oreTypeIndex = getOreTypeIndex(type);
}
int getOreTypeIndex(TileType type) {
switch(type) {
case ORE0: return 0;
case ORE1: return 1;
case ORE2: return 2;
case ROCK: return 3;
default: return -1;
}
}
void display(float x, float y) {
if (type == TileType.EMPTY) {
fill(48,48,48);
rect(x, y, TILE_SIZE, TILE_SIZE);
} else if (type == TileType.GOAL) {
fill(0, 255, 0);
rect(x, y, TILE_SIZE, TILE_SIZE);
} else if (oreTypeIndex >= 0) {
int frameIndex;
if (oreTypeIndex < 3) { // For ORE tiles
float healthPercentage = (float)health / maxHealth;
frameIndex = (int)((1 - healthPercentage) * FRAMES_PER_ORE);
frameIndex = constrain(frameIndex, 0, FRAMES_PER_ORE - 1);
} else {
frameIndex = 0; // For ROCK tiles
}
image(oreSprites[oreTypeIndex][frameIndex], x, y, TILE_SIZE, TILE_SIZE);
}
}
TileType mine() {
if (type != TileType.EMPTY && type != TileType.GOAL) {
health--;
if (health <= 0) {
TileType minedType = type;
type = TileType.EMPTY;
oreTypeIndex = -1; // Update oreTypeIndex when tile becomes empty
soundManager.stopMiningSound();
return minedType;
}
}
return null;
}
void takeDamage(int damage) {
if (type != TileType.EMPTY) {
health -= damage;
if (health <= 0) {
type = TileType.EMPTY;
oreTypeIndex = -1; // Update oreTypeIndex when tile becomes empty
}
}
}
int getMaxHealth() {
switch(type) {
case ROCK : return 35;
case ORE0 : return 200;
case ORE1 : return 120;
case ORE2 : return 75;
default : return 0;
}
}
}
Explanation:
Loading and Extracting Sprites: The code loads the sprite sheet and extracts each frame into a 2D array oreSprites[oreType][frameIndex].
Tile Class Adjustments:
Added maxHealth and oreTypeIndex fields.
The getOreTypeIndex method maps the TileType to an index used for accessing oreSprites.
Display Method:
Calculates the frame index based on the health percentage.
Uses image() to draw the correct frame from oreSprites.
Health Management:
Updated mine() and takeDamage() methods to set oreTypeIndex to -1 when the tile becomes EMPTY, ensuring the correct display logic.
With these changes, your game will now display different animation frames for ore tiles based on their current health, transitioning through the frames as the player mines the ore.