cache animated sprites

This commit is contained in:
Anthony Calosa
2023-08-01 12:42:02 +08:00
parent f0261ac507
commit cfd9dfa8db
5 changed files with 82 additions and 81 deletions

View File

@@ -8,7 +8,9 @@ import com.badlogic.gdx.utils.Array;
import forge.adventure.data.DialogData; import forge.adventure.data.DialogData;
import forge.adventure.stage.SpriteGroup; import forge.adventure.stage.SpriteGroup;
import forge.adventure.util.Config; import forge.adventure.util.Config;
import java.util.HashMap; import java.util.HashMap;
/** /**
* CharacterSprite base class for animated sprites on the map * CharacterSprite base class for animated sprites on the map
*/ */
@@ -32,6 +34,7 @@ public class CharacterSprite extends MapActor {
atlasPath = path; atlasPath = path;
load(path); load(path);
} }
public CharacterSprite(String path) { public CharacterSprite(String path) {
this(0, path); this(0, path);
} }
@@ -43,11 +46,10 @@ public class CharacterSprite extends MapActor {
protected void load(String path) { protected void load(String path) {
if (path == null || path.isEmpty()) return; if (path == null || path.isEmpty()) return;
TextureAtlas atlas = Config.instance().getAtlas(path);
animations.clear(); animations.clear();
for (AnimationTypes stand : AnimationTypes.values()) { for (AnimationTypes stand : AnimationTypes.values()) {
if (stand == AnimationTypes.Avatar) { if (stand == AnimationTypes.Avatar) {
avatar.addAll(atlas.createSprites(stand.toString())); avatar.addAll(Config.instance().getAnimatedSprites(path, stand.toString()));
continue; continue;
} }
HashMap<AnimationDirections, Animation<TextureRegion>> dirs = new HashMap<>(); HashMap<AnimationDirections, Animation<TextureRegion>> dirs = new HashMap<>();
@@ -55,9 +57,9 @@ public class CharacterSprite extends MapActor {
Array<Sprite> anim; Array<Sprite> anim;
if (dir == AnimationDirections.None) if (dir == AnimationDirections.None)
anim = atlas.createSprites(stand.toString()); anim = Config.instance().getAnimatedSprites(path, stand.toString());
else else
anim = atlas.createSprites(stand.toString() + dir.toString()); anim = Config.instance().getAnimatedSprites(path, stand.toString() + dir.toString());
if (anim.size != 0) { if (anim.size != 0) {
dirs.put(dir, new Animation<>(0.2f, anim)); dirs.put(dir, new Animation<>(0.2f, anim));
@@ -69,7 +71,6 @@ public class CharacterSprite extends MapActor {
} }
} }
animations.put(stand, dirs); animations.put(stand, dirs);
} }
@@ -111,7 +112,6 @@ public class CharacterSprite extends MapActor {
} }
} }
setAnimation(AnimationTypes.Idle); setAnimation(AnimationTypes.Idle);
setDirection(AnimationDirections.Right); setDirection(AnimationDirections.Right);
} }
@@ -188,8 +188,7 @@ public class CharacterSprite extends MapActor {
setAnimation(AnimationTypes.Wake); setAnimation(AnimationTypes.Wake);
wakeTimer = 0.0f; wakeTimer = 0.0f;
hidden = false; hidden = false;
} } else return;
else return;
} }
if (currentAnimationType == AnimationTypes.Wake && wakeTimer <= currentAnimation.getAnimationDuration()) { if (currentAnimationType == AnimationTypes.Wake && wakeTimer <= currentAnimation.getAnimationDuration()) {
@@ -203,7 +202,8 @@ public class CharacterSprite extends MapActor {
Vector2 vec = new Vector2(x, y); Vector2 vec = new Vector2(x, y);
float degree = vec.angleDeg(); float degree = vec.angleDeg();
if (!hidden) setAnimation(AnimationTypes.Walk); if (!hidden)
setAnimation(AnimationTypes.Walk);
if (degree < 22.5) if (degree < 22.5)
setDirection(AnimationDirections.Right); setDirection(AnimationDirections.Right);
else if (degree < 22.5 + 45) else if (degree < 22.5 + 45)
@@ -230,7 +230,6 @@ public class CharacterSprite extends MapActor {
} }
@Override @Override
public void act(float delta) { public void act(float delta) {
timer += delta; timer += delta;
@@ -240,20 +239,16 @@ public class CharacterSprite extends MapActor {
@Override @Override
public void draw(Batch batch, float parentAlpha) { public void draw(Batch batch, float parentAlpha) {
if (currentAnimation == null || hidden || inactive) if (currentAnimation == null || hidden || inactive) {
{
return; return;
} }
super.draw(batch, parentAlpha); super.draw(batch, parentAlpha);
beforeDraw(batch, parentAlpha); beforeDraw(batch, parentAlpha);
TextureRegion currentFrame; TextureRegion currentFrame;
if (currentAnimationType.equals(AnimationTypes.Wake)) if (currentAnimationType.equals(AnimationTypes.Wake)) {
{
currentFrame = currentAnimation.getKeyFrame(wakeTimer, false); currentFrame = currentAnimation.getKeyFrame(wakeTimer, false);
} } else {
else
{
currentFrame = currentAnimation.getKeyFrame(timer, true); currentFrame = currentAnimation.getKeyFrame(timer, true);
} }
@@ -278,9 +273,11 @@ public class CharacterSprite extends MapActor {
return null; return null;
return avatar.first(); return avatar.first();
} }
public String getAtlasPath() { public String getAtlasPath() {
return atlasPath; return atlasPath;
} }
public Sprite getAvatar(int index) { public Sprite getAvatar(int index) {
return avatar.get(index); return avatar.get(index);
} }
@@ -297,7 +294,6 @@ public class CharacterSprite extends MapActor {
} }
public enum AnimationDirections { public enum AnimationDirections {
None, None,
Right, Right,
RightDown, RightDown,

View File

@@ -15,7 +15,6 @@ import java.util.HashMap;
* Extension of EntryActor, visible on map, multiple states that change behavior * Extension of EntryActor, visible on map, multiple states that change behavior
*/ */
public class PortalActor extends EntryActor { public class PortalActor extends EntryActor {
private final HashMap<PortalAnimationTypes, Animation<TextureRegion>> animations = new HashMap<>(); private final HashMap<PortalAnimationTypes, Animation<TextureRegion>> animations = new HashMap<>();
private Animation<TextureRegion> currentAnimation = null; private Animation<TextureRegion> currentAnimation = null;
private PortalAnimationTypes currentAnimationType = PortalAnimationTypes.Closed; private PortalAnimationTypes currentAnimationType = PortalAnimationTypes.Closed;
@@ -24,20 +23,17 @@ public class PortalActor extends EntryActor{
float transitionTimer; float transitionTimer;
public PortalActor(MapStage stage, int id, String targetMap, float x, float y, float w, float h, String direction, String currentMap, int portalTargetObject, String path) public PortalActor(MapStage stage, int id, String targetMap, float x, float y, float w, float h, String direction, String currentMap, int portalTargetObject, String path) {
{
super(stage, id, targetMap, x, y, w, h, direction, currentMap, portalTargetObject); super(stage, id, targetMap, x, y, w, h, direction, currentMap, portalTargetObject);
load(path); load(path);
} }
public MapStage getMapStage() public MapStage getMapStage() {
{
return stage; return stage;
} }
@Override @Override
public void onPlayerCollide() public void onPlayerCollide() {
{
if (currentAnimationType == PortalAnimationTypes.Inactive) { if (currentAnimationType == PortalAnimationTypes.Inactive) {
//Activate portal? Launch Dialog? //Activate portal? Launch Dialog?
} }
@@ -45,14 +41,11 @@ public class PortalActor extends EntryActor{
if (targetMap == null || targetMap.isEmpty()) { if (targetMap == null || targetMap.isEmpty()) {
stage.exitDungeon(); stage.exitDungeon();
} else { } else {
if (targetMap.equals(currentMap)) if (targetMap.equals(currentMap)) {
{
stage.spawn(entryTargetObject); stage.spawn(entryTargetObject);
stage.getPlayerSprite().playEffect(Paths.EFFECT_TELEPORT, 0.5f); stage.getPlayerSprite().playEffect(Paths.EFFECT_TELEPORT, 0.5f);
stage.startPause(1.5f); stage.startPause(1.5f);
} } else {
else
{
currentMap = targetMap; currentMap = targetMap;
TileMapScene.instance().loadNext(targetMap, entryTargetObject); TileMapScene.instance().loadNext(targetMap, entryTargetObject);
stage.getPlayerSprite().playEffect(Paths.EFFECT_TELEPORT, 0.5f); stage.getPlayerSprite().playEffect(Paths.EFFECT_TELEPORT, 0.5f);
@@ -62,8 +55,7 @@ public class PortalActor extends EntryActor{
} }
public void spawn() { public void spawn() {
switch(direction) switch (direction) {
{
case "up": case "up":
stage.getPlayerSprite().setPosition(x + w / 2 - stage.getPlayerSprite().getWidth() / 2, y + h); stage.getPlayerSprite().setPosition(x + w / 2 - stage.getPlayerSprite().getWidth() / 2, y + h);
break; break;
@@ -76,16 +68,14 @@ public class PortalActor extends EntryActor{
case "left": case "left":
stage.getPlayerSprite().setPosition(x + w, y + h / 2 - stage.getPlayerSprite().getHeight() / 2); stage.getPlayerSprite().setPosition(x + w, y + h / 2 - stage.getPlayerSprite().getHeight() / 2);
break; break;
} }
} }
protected void load(String path) { protected void load(String path) {
if (path == null || path.isEmpty()) return; if (path == null || path.isEmpty()) return;
TextureAtlas atlas = Config.instance().getAtlas(path);
animations.clear(); animations.clear();
for (PortalAnimationTypes stand : PortalAnimationTypes.values()) { for (PortalAnimationTypes stand : PortalAnimationTypes.values()) {
Array<Sprite> anim = atlas.createSprites(stand.toString()); Array<Sprite> anim = Config.instance().getAnimatedSprites(path, stand.toString());
if (anim.size != 0) { if (anim.size != 0) {
animations.put(stand, new Animation<>(0.2f, anim)); animations.put(stand, new Animation<>(0.2f, anim));
if (getWidth() == 0.0)//init size onload if (getWidth() == 0.0)//init size onload
@@ -156,20 +146,16 @@ public class PortalActor extends EntryActor{
} }
public void draw(Batch batch, float parentAlpha) { public void draw(Batch batch, float parentAlpha) {
if (currentAnimation == null) if (currentAnimation == null) {
{
return; return;
} }
super.draw(batch, parentAlpha); super.draw(batch, parentAlpha);
beforeDraw(batch, parentAlpha); beforeDraw(batch, parentAlpha);
TextureRegion currentFrame; TextureRegion currentFrame;
if (currentAnimationType.equals(PortalAnimationTypes.Opening) || currentAnimationType.equals(PortalAnimationTypes.Closing)) if (currentAnimationType.equals(PortalAnimationTypes.Opening) || currentAnimationType.equals(PortalAnimationTypes.Closing)) {
{
currentFrame = currentAnimation.getKeyFrame(transitionTimer, false); currentFrame = currentAnimation.getKeyFrame(transitionTimer, false);
} } else {
else
{
currentFrame = currentAnimation.getKeyFrame(timer, true); currentFrame = currentAnimation.getKeyFrame(timer, true);
} }
@@ -181,7 +167,6 @@ public class PortalActor extends EntryActor{
batch.setColor(oldColor); batch.setColor(oldColor);
super.draw(batch, parentAlpha); super.draw(batch, parentAlpha);
//batch.draw(getDebugTexture(),getX(),getY()); //batch.draw(getDebugTexture(),getX(),getY());
} }
} }

View File

@@ -121,7 +121,7 @@ public class MapViewScene extends UIScene {
if (poi != null) { if (poi != null) {
if (positions.contains(poi.getPosition())) if (positions.contains(poi.getPosition()))
continue; //don't map duplicate position to prevent stacking continue; //don't map duplicate position to prevent stacking
TypingLabel label = Controls.newTypingLabel("[%?BLACKEN][+GPS]{GRADIENT=RED;WHITE;1;1}>" + adq.name + "{ENDGRADIENT}"); TypingLabel label = Controls.newTypingLabel("[+GPS][%?BLACKEN] " + adq.name);
labels.add(label); labels.add(label);
table.addActor(label); table.addActor(label);
label.setPosition(getMapX(poi.getPosition().x) - label.getWidth() / 2, getMapY(poi.getPosition().y) - label.getHeight() / 2); label.setPosition(getMapX(poi.getPosition().x) - label.getWidth() / 2, getMapY(poi.getPosition().y) - label.getHeight() / 2);
@@ -130,7 +130,7 @@ public class MapViewScene extends UIScene {
} }
} }
for (PointOfInterest poi : bookmark) { for (PointOfInterest poi : bookmark) {
TypingLabel label = Controls.newTypingLabel("[%70][+Star]"); TypingLabel label = Controls.newTypingLabel("[%75][+Star] ");
table.addActor(label); table.addActor(label);
label.setPosition(getMapX(poi.getPosition().x) - label.getWidth() / 2, getMapY(poi.getPosition().y) - label.getHeight() / 2); label.setPosition(getMapX(poi.getPosition().x) - label.getWidth() / 2, getMapY(poi.getPosition().y) - label.getHeight() / 2);
label.skipToTheEnd(); label.skipToTheEnd();

View File

@@ -50,6 +50,7 @@ public class Config {
private final String plane; private final String plane;
private ObjectMap<String, ObjectMap<String, Sprite>> atlasSprites = new ObjectMap<>(); private ObjectMap<String, ObjectMap<String, Sprite>> atlasSprites = new ObjectMap<>();
private ObjectMap<PointOfInterestData, Array<Sprite>> poiSprites = new ObjectMap<>(); private ObjectMap<PointOfInterestData, Array<Sprite>> poiSprites = new ObjectMap<>();
private ObjectMap<String, ObjectMap<String, Array<Sprite>>> animatedSprites = new ObjectMap<>();
static public Config instance() { static public Config instance() {
if (currentConfig == null) if (currentConfig == null)
@@ -274,6 +275,24 @@ public class Config {
} }
return sprites; return sprites;
} }
public Array<Sprite> getAnimatedSprites(String path, String animationName) {
Array<Sprite> sprites;
ObjectMap<String, Array<Sprite>> mapSprites = animatedSprites.get(path);
if (mapSprites == null) {
mapSprites = new ObjectMap<>();
}
sprites = mapSprites.get(animationName);
if (sprites == null) {
sprites = getAtlas(path).createSprites(animationName);
if (sprites != null) {
mapSprites.put(animationName, sprites);
animatedSprites.put(path, mapSprites);
}
}
return sprites;
}
public SettingData getSettingData() { public SettingData getSettingData() {
return settingsData; return settingsData;
} }

View File

@@ -11,15 +11,17 @@ public class NavArrowActor extends Actor {
public float navTargetAngle = 0.0f; public float navTargetAngle = 0.0f;
private Animation<TextureRegion> currentAnimation; private Animation<TextureRegion> currentAnimation;
private Array<Sprite> sprites;
float timer; float timer;
public NavArrowActor() { public NavArrowActor() {
if (sprites == null) {
//TODO: Expand compass sprite to have color coded arrows, swap sprites based on distance to target //TODO: Expand compass sprite to have color coded arrows, swap sprites based on distance to target
Array<Sprite> textureAtlas = Config.instance().getAtlas("maps/tileset/compass.atlas").createSprites(); sprites = Config.instance().getAtlas("maps/tileset/compass.atlas").createSprites();
if (textureAtlas.isEmpty()) { if (sprites.isEmpty())
System.out.print("NavArrow sprite not found"); System.out.print("NavArrow sprite not found");
} }
currentAnimation = new Animation<>(0.4f, textureAtlas); currentAnimation = new Animation<>(0.4f, sprites);
} }
@Override @Override
@@ -36,7 +38,6 @@ public class NavArrowActor extends Actor {
setHeight(currentFrame.getRegionHeight()); setHeight(currentFrame.getRegionHeight());
setWidth(currentFrame.getRegionWidth()); setWidth(currentFrame.getRegionWidth());
//TODO: Simplify params somehow for readability? All this does is spin the image around the player. //TODO: Simplify params somehow for readability? All this does is spin the image around the player.
batch.draw(currentFrame, getX() - currentFrame.getRegionWidth() / 2, getY() - currentFrame.getRegionHeight() / 2, (currentFrame.getRegionWidth() * 0.5f), (currentFrame.getRegionHeight() * 0.5f), currentFrame.getRegionWidth(), currentFrame.getRegionHeight(), 1, 1, navTargetAngle); batch.draw(currentFrame, getX() - currentFrame.getRegionWidth() / 2, getY() - currentFrame.getRegionHeight() / 2, (currentFrame.getRegionWidth() * 0.5f), (currentFrame.getRegionHeight() * 0.5f), currentFrame.getRegionWidth(), currentFrame.getRegionHeight(), 1, 1, navTargetAngle);
} }