mirror of
https://github.com/MistWeatherMedia/WeatherSTAR-3000-Java.git
synced 2026-11-28 14:38:08 -08:00
47 lines
1.3 KiB
Java
47 lines
1.3 KiB
Java
package components;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import org.joml.Vector2f;
|
|
|
|
import renderer.Texture;
|
|
|
|
public class Spritesheet {
|
|
private Texture texture;
|
|
private List<Sprite> sprites;
|
|
|
|
public Spritesheet(Texture texture, int spriteWidth, int spriteHeight, int numSprites, int spacing) {
|
|
this.sprites = new ArrayList<>();
|
|
|
|
this.texture = texture;
|
|
int currentX = 0;
|
|
int currentY = texture.getHeight() - spriteHeight;
|
|
for (int i = 0; i < numSprites; i++) {
|
|
float topY = (currentY + spriteHeight) / (float)texture.getHeight();;
|
|
float rightX = (currentX + spriteWidth) / (float)texture.getWidth();
|
|
float leftX = currentX / (float)texture.getWidth();
|
|
float bottomY = currentY / (float)texture.getHeight();;
|
|
|
|
Vector2f[] texCoords = {
|
|
new Vector2f(rightX, bottomY),
|
|
new Vector2f(rightX, topY),
|
|
new Vector2f(leftX, topY),
|
|
new Vector2f(leftX, bottomY)
|
|
};
|
|
Sprite sprite = new Sprite(this.texture, texCoords);
|
|
this.sprites.add(sprite);
|
|
|
|
currentX += spriteWidth + spacing;
|
|
if (currentX >= texture.getWidth()) {
|
|
currentX = 0;
|
|
currentY -= spriteHeight + spacing;
|
|
}
|
|
}
|
|
}
|
|
|
|
public Sprite getSprite(int index) {
|
|
return this.sprites.get(index);
|
|
}
|
|
}
|