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
*/
@@ -19,21 +21,22 @@ public class CharacterSprite extends MapActor {
private Animation<TextureRegion> currentAnimation = null;
private AnimationTypes currentAnimationType = AnimationTypes.Idle;
private AnimationDirections currentAnimationDir = AnimationDirections.None;
private final Array<Sprite> avatar=new Array<>();
private final Array<Sprite> avatar = new Array<>();
public boolean hidden = false;
public boolean inactive = false;
private String atlasPath;
private float wakeTimer = 0.0f;
public DialogData.ConditionData[] spawnConditions = new DialogData.ConditionData[0]; //List of conditions for the sprite to spawn.
public CharacterSprite(int id,String path) {
public CharacterSprite(int id, String path) {
super(id);
collisionHeight=0.4f;
collisionHeight = 0.4f;
atlasPath = path;
load(path);
}
public CharacterSprite(String path) {
this(0,path);
this(0, path);
}
@Override
@@ -42,12 +45,11 @@ public class CharacterSprite extends MapActor {
}
protected void load(String path) {
if(path==null||path.isEmpty())return;
TextureAtlas atlas = Config.instance().getAtlas(path);
if (path == null || path.isEmpty()) return;
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,13 +57,13 @@ 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));
if(getWidth()==0.0)//init size onload
if (getWidth() == 0.0)//init size onload
{
setWidth(anim.first().getWidth());
setHeight(anim.first().getHeight());
@@ -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);
}
@@ -172,13 +172,13 @@ public class CharacterSprite extends MapActor {
}
@Override
public void moveBy(float x, float y){
moveBy(x,y,0.0f);
public void moveBy(float x, float y) {
moveBy(x, y, 0.0f);
}
public void moveBy(float x, float y, float delta) {
if (inactive){
if (inactive) {
return;
}
@@ -188,11 +188,10 @@ public class CharacterSprite extends MapActor {
setAnimation(AnimationTypes.Wake);
wakeTimer = 0.0f;
hidden = false;
}
else return;
} else return;
}
if (currentAnimationType == AnimationTypes.Wake && wakeTimer <= currentAnimation.getAnimationDuration()){
if (currentAnimationType == AnimationTypes.Wake && wakeTimer <= currentAnimation.getAnimationDuration()) {
wakeTimer += delta;
return;
}
@@ -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,34 +239,30 @@ 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);
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);
}
setHeight(currentFrame.getRegionHeight());
setWidth(currentFrame.getRegionWidth());
Color oldColor=batch.getColor().cpy();
Color oldColor = batch.getColor().cpy();
batch.setColor(getColor());
float scale = 1f;
if (this instanceof EnemySprite) {
scale = ((EnemySprite) this).getData().scale;
}
batch.draw(currentFrame, getX(), getY(), getWidth()*scale, getHeight()*scale);
batch.draw(currentFrame, getX(), getY(), getWidth() * scale, getHeight() * scale);
batch.setColor(oldColor);
super.draw(batch,parentAlpha);
super.draw(batch, parentAlpha);
//batch.draw(getDebugTexture(),getX(),getY());
}
@@ -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

@@ -14,8 +14,7 @@ import java.util.HashMap;
* PortalActor
* 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 Animation<TextureRegion> currentAnimation = null;
private PortalAnimationTypes currentAnimationType = PortalAnimationTypes.Closed;
@@ -24,35 +23,29 @@ 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()
{
if(currentAnimationType == PortalAnimationTypes.Inactive) {
public void onPlayerCollide() {
if (currentAnimationType == PortalAnimationTypes.Inactive) {
//Activate portal? Launch Dialog?
}
if(currentAnimationType == PortalAnimationTypes.Active) {
if (currentAnimationType == PortalAnimationTypes.Active) {
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,33 +55,30 @@ 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);
stage.getPlayerSprite().setPosition(x + w / 2 - stage.getPlayerSprite().getWidth() / 2, y + h);
break;
case "down":
stage.getPlayerSprite().setPosition(x+w/2-stage.getPlayerSprite().getWidth()/2,y-stage.getPlayerSprite().getHeight());
stage.getPlayerSprite().setPosition(x + w / 2 - stage.getPlayerSprite().getWidth() / 2, y - stage.getPlayerSprite().getHeight());
break;
case "right":
stage.getPlayerSprite().setPosition(x-stage.getPlayerSprite().getWidth(),y+h/2-stage.getPlayerSprite().getHeight()/2);
stage.getPlayerSprite().setPosition(x - stage.getPlayerSprite().getWidth(), y + h / 2 - stage.getPlayerSprite().getHeight() / 2);
break;
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;
}
}
protected void load(String path) {
if(path==null||path.isEmpty())return;
TextureAtlas atlas = Config.instance().getAtlas(path);
if (path == null || path.isEmpty()) return;
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
if (getWidth() == 0.0)//init size onload
{
setWidth(anim.first().getWidth());
setHeight(anim.first().getHeight());
@@ -108,7 +98,7 @@ public class PortalActor extends EntryActor{
}
public void setAnimation(String typeName) {
switch(typeName.toLowerCase()){
switch (typeName.toLowerCase()) {
case "active":
setAnimation(PortalAnimationTypes.Active);
break;
@@ -156,32 +146,27 @@ 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);
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);
}
setHeight(currentFrame.getRegionHeight());
setWidth(currentFrame.getRegionWidth());
Color oldColor=batch.getColor().cpy();
Color oldColor = batch.getColor().cpy();
batch.setColor(getColor());
batch.draw(currentFrame, getX(), getY(), getWidth(), getHeight());
batch.setColor(oldColor);
super.draw(batch,parentAlpha);
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,8 +38,7 @@ 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);
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);
}
}