Skip to content

Commit ac03cb9

Browse files
committed
sprite scale animation support
1 parent 8e2727b commit ac03cb9

File tree

5 files changed

+220
-10
lines changed

5 files changed

+220
-10
lines changed

blade-engine/src/com/bladecoder/engine/actions/ActionFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public class ActionFactory {
5050
actions.put("Animation",
5151
"com.bladecoder.engine.actions.AnimationAction");
5252
actions.put("Position",
53-
"com.bladecoder.engine.actions.PosAnimationAction");
53+
"com.bladecoder.engine.actions.PositionAction");
54+
actions.put("Scale",
55+
"com.bladecoder.engine.actions.ScaleAction");
5456
actions.put("RemoveInventoryItem",
5557
"com.bladecoder.engine.actions.RemoveInventoryItemAction");
5658
actions.put("Say",

blade-engine/src/com/bladecoder/engine/actions/PosAnimationAction.java renamed to blade-engine/src/com/bladecoder/engine/actions/PositionAction.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
import java.util.HashMap;
1919

20-
import com.bladecoder.engine.actions.BaseCallbackAction;
21-
import com.bladecoder.engine.actions.Param;
2220
import com.badlogic.gdx.math.Vector2;
2321
import com.badlogic.gdx.utils.Json;
2422
import com.badlogic.gdx.utils.JsonValue;
@@ -27,10 +25,9 @@
2725
import com.bladecoder.engine.assets.EngineAssetManager;
2826
import com.bladecoder.engine.model.SpriteActor;
2927
import com.bladecoder.engine.model.World;
30-
import com.bladecoder.engine.util.EngineLogger;
3128

32-
public class PosAnimationAction extends BaseCallbackAction {
33-
public static final String INFO = "Throws a position type animation";
29+
public class PositionAction extends BaseCallbackAction {
30+
public static final String INFO = "Sets an actor Position animation";
3431
public static final Param[] PARAMS = {
3532
new Param("pos", "The target position", Type.VECTOR2, true),
3633
new Param("speed", "Duration of the animation in seconds", Type.FLOAT, true, "1.0"),
@@ -79,7 +76,6 @@ public void setParams(HashMap<String, String> params) {
7976
@Override
8077
public boolean run(ActionCallback cb) {
8178
setVerbCb(cb);
82-
EngineLogger.debug("SET_POSANIMATION_ACTION");
8379

8480
float scale = EngineAssetManager.getInstance().getScale();
8581

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*******************************************************************************
2+
* Copyright 2014 Rafael Garcia Moreno.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
******************************************************************************/
16+
package com.bladecoder.engine.actions;
17+
18+
import java.util.HashMap;
19+
20+
import com.badlogic.gdx.utils.Json;
21+
import com.badlogic.gdx.utils.JsonValue;
22+
import com.bladecoder.engine.actions.Param.Type;
23+
import com.bladecoder.engine.anim.Tween;
24+
import com.bladecoder.engine.model.SpriteActor;
25+
import com.bladecoder.engine.model.World;
26+
27+
public class ScaleAction extends BaseCallbackAction {
28+
public static final String INFO = "Sets an actor Scale animation";
29+
public static final Param[] PARAMS = {
30+
new Param("scale", "The target scale", Type.FLOAT, true),
31+
new Param("speed", "Duration of the animation in seconds", Type.FLOAT, true, "1.0"),
32+
new Param("count", "The times to repeat", Type.INTEGER),
33+
new Param("wait", "If this param is 'false' the text is showed and the action continues inmediatly", Type.BOOLEAN, true),
34+
new Param("repeat", "The repeat mode", Type.STRING, true, "repeat", new String[]{"repeat", "yoyo", "no_repeat"}),
35+
};
36+
37+
private String actorId;
38+
private float speed;
39+
private float scale;
40+
private int repeat = Tween.NO_REPEAT;
41+
private int count = 1;
42+
43+
@Override
44+
public void setParams(HashMap<String, String> params) {
45+
actorId = params.get("actor");
46+
47+
// get final position. We need to scale the coordinates to the current resolution
48+
scale = Float.parseFloat(params.get("scale"));
49+
50+
speed = Float.parseFloat(params.get("speed"));
51+
52+
if(params.get("count") != null) {
53+
count = Integer.parseInt(params.get("count"));
54+
}
55+
56+
if(params.get("wait") != null) {
57+
setWait(Boolean.parseBoolean(params.get("wait")));
58+
}
59+
60+
if(params.get("repeat") != null) {
61+
String repeatStr = params.get("repeat");
62+
if (repeatStr.equalsIgnoreCase("repeat")) {
63+
repeat = Tween.REPEAT;
64+
} else if (repeatStr.equalsIgnoreCase("yoyo")) {
65+
repeat = Tween.PINGPONG;
66+
} else if (repeatStr.equalsIgnoreCase("no_repeat")) {
67+
repeat = Tween.NO_REPEAT;
68+
} else {
69+
repeat = Tween.FROM_FA;
70+
}
71+
}
72+
}
73+
74+
@Override
75+
public boolean run(ActionCallback cb) {
76+
setVerbCb(cb);
77+
78+
SpriteActor actor = (SpriteActor) World.getInstance().getCurrentScene().getActor(actorId, false);
79+
80+
actor.startScaleAnimation(repeat, count, speed, scale, getWait()?this:null);
81+
82+
return getWait();
83+
}
84+
85+
@Override
86+
public void write(Json json) {
87+
json.writeValue("actorId", actorId);
88+
json.writeValue("scale", scale);
89+
json.writeValue("speed", speed);
90+
json.writeValue("repeat", repeat);
91+
json.writeValue("count", count);
92+
super.write(json);
93+
}
94+
95+
@Override
96+
public void read (Json json, JsonValue jsonData) {
97+
actorId = json.readValue("actorId", String.class, jsonData);
98+
scale = json.readValue("scale", Float.class, jsonData);
99+
speed = json.readValue("speed", Float.class, jsonData);
100+
repeat = json.readValue("repeat", Integer.class, jsonData);
101+
count = json.readValue("count", Integer.class, jsonData);
102+
super.read(json, jsonData);
103+
}
104+
105+
106+
@Override
107+
public String getInfo() {
108+
return INFO;
109+
}
110+
111+
@Override
112+
public Param[] getParams() {
113+
return PARAMS;
114+
}
115+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*******************************************************************************
2+
* Copyright 2014 Rafael Garcia Moreno.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
******************************************************************************/
16+
package com.bladecoder.engine.anim;
17+
18+
import com.bladecoder.engine.anim.Tween;
19+
20+
import com.badlogic.gdx.utils.Json;
21+
import com.badlogic.gdx.utils.JsonValue;
22+
import com.bladecoder.engine.actions.ActionCallback;
23+
import com.bladecoder.engine.model.SpriteActor;
24+
25+
/**
26+
* Tween for spriteactor scale animation
27+
*/
28+
public class SpriteScaleTween extends Tween {
29+
30+
private float startScl;
31+
private float targetScl;
32+
33+
public SpriteScaleTween() {
34+
}
35+
36+
public void start(SpriteActor target, int repeatType, int count, float tScl, float duration, ActionCallback cb) {
37+
38+
startScl = target.getScale();
39+
targetScl = tScl;
40+
41+
setDuration(duration);
42+
setType(repeatType);
43+
setCount(count);
44+
45+
if (cb != null) {
46+
setCb(cb);
47+
}
48+
49+
restart();
50+
}
51+
52+
public void update(SpriteActor a, float delta) {
53+
update(delta);
54+
55+
a.setScale(startScl + getPercent() * (targetScl - startScl));
56+
}
57+
58+
@Override
59+
public void write(Json json) {
60+
super.write(json);
61+
62+
json.writeValue("startScl", startScl);
63+
json.writeValue("targetScl", targetScl);
64+
}
65+
66+
@Override
67+
public void read(Json json, JsonValue jsonData) {
68+
super.read(json, jsonData);
69+
70+
startScl = json.readValue("startScl", Float.class, jsonData);
71+
targetScl = json.readValue("targetScl", Float.class, jsonData);
72+
}
73+
}

blade-engine/src/com/bladecoder/engine/model/SpriteActor.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import com.bladecoder.engine.model.BaseActor;
2222
import com.bladecoder.engine.model.ActorRenderer;
23-
2423
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
2524
import com.badlogic.gdx.math.Vector2;
2625
import com.badlogic.gdx.utils.Json;
@@ -29,6 +28,7 @@
2928
import com.bladecoder.engine.actions.ActionCallbackQueue;
3029
import com.bladecoder.engine.anim.AnimationDesc;
3130
import com.bladecoder.engine.anim.SpritePosTween;
31+
import com.bladecoder.engine.anim.SpriteScaleTween;
3232
import com.bladecoder.engine.anim.Tween;
3333
import com.bladecoder.engine.anim.WalkTween;
3434
import com.bladecoder.engine.assets.EngineAssetManager;
@@ -44,6 +44,7 @@ public static enum DepthType {
4444

4545
private ActorRenderer renderer;
4646
private SpritePosTween posTween;
47+
private SpriteScaleTween scaleTween;
4748
private float scale = 1.0f;
4849

4950
/** Scale sprite acording to the scene depth map */
@@ -138,6 +139,13 @@ public void update(float delta) {
138139
posTween = null;
139140
}
140141
}
142+
143+
if(scaleTween != null) {
144+
scaleTween.update(this, delta);
145+
if(scaleTween.isComplete()) {
146+
scaleTween = null;
147+
}
148+
}
141149
}
142150

143151
public void draw(SpriteBatch batch) {
@@ -212,6 +220,18 @@ public void startPosAnimation(int repeatType, int count, float duration,
212220
posTween.start(this, repeatType, count, destX, destY, duration,
213221
cb);
214222
}
223+
224+
/**
225+
* Create scale animation.
226+
*/
227+
public void startScaleAnimation(int repeatType, int count, float duration,
228+
float scale, ActionCallback cb) {
229+
230+
scaleTween = new SpriteScaleTween();
231+
232+
scaleTween.start(this, repeatType, count, scale, duration,
233+
cb);
234+
}
215235

216236
public void lookat(Vector2 p) {
217237
renderer.lookat(bbox.getX(), bbox.getY(), p);
@@ -334,6 +354,7 @@ public void write(Json json) {
334354
json.writeValue("depthType", depthType);
335355
json.writeValue("renderer", renderer, null);
336356
json.writeValue("bboxFromRenderer", bboxFromRenderer);
357+
json.writeValue("scaleTween", scaleTween, null);
337358
}
338359

339360
@Override
@@ -349,8 +370,11 @@ public void read(Json json, JsonValue jsonData) {
349370

350371
bboxFromRenderer = json.readValue("bboxFromRenderer", Boolean.class, jsonData);
351372

352-
if(bboxFromRenderer)
353-
bbox.setScale(1, 1);
373+
// if(bboxFromRenderer)
374+
// bbox.setScale(1, 1);
375+
376+
scaleTween = json.readValue("scaleTween", SpriteScaleTween.class, jsonData);
377+
setScale(scale);
354378
}
355379

356380
}

0 commit comments

Comments
 (0)