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

View File

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

View File

@@ -121,7 +121,7 @@ public class MapViewScene extends UIScene {
if (poi != null) {
if (positions.contains(poi.getPosition()))
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);
table.addActor(label);
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) {
TypingLabel label = Controls.newTypingLabel("[%70][+Star]");
TypingLabel label = Controls.newTypingLabel("[%75][+Star] ");
table.addActor(label);
label.setPosition(getMapX(poi.getPosition().x) - label.getWidth() / 2, getMapY(poi.getPosition().y) - label.getHeight() / 2);
label.skipToTheEnd();

View File

@@ -50,6 +50,7 @@ public class Config {
private final String plane;
private ObjectMap<String, ObjectMap<String, Sprite>> atlasSprites = new ObjectMap<>();
private ObjectMap<PointOfInterestData, Array<Sprite>> poiSprites = new ObjectMap<>();
private ObjectMap<String, ObjectMap<String, Array<Sprite>>> animatedSprites = new ObjectMap<>();
static public Config instance() {
if (currentConfig == null)
@@ -274,6 +275,24 @@ public class Config {
}
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() {
return settingsData;
}

View File

@@ -11,15 +11,17 @@ public class NavArrowActor extends Actor {
public float navTargetAngle = 0.0f;
private Animation<TextureRegion> currentAnimation;
private Array<Sprite> sprites;
float timer;
public NavArrowActor() {
if (sprites == null) {
//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();
if (textureAtlas.isEmpty()) {
sprites = Config.instance().getAtlas("maps/tileset/compass.atlas").createSprites();
if (sprites.isEmpty())
System.out.print("NavArrow sprite not found");
}
currentAnimation = new Animation<>(0.4f, textureAtlas);
currentAnimation = new Animation<>(0.4f, sprites);
}
@Override
@@ -36,7 +38,6 @@ public class NavArrowActor extends Actor {
setHeight(currentFrame.getRegionHeight());
setWidth(currentFrame.getRegionWidth());
//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);
}