From 67f1442d7d1ed9a87356c8d06a2614ace09b912f Mon Sep 17 00:00:00 2001 From: "oskar.wiksten@gmail.com" Date: Tue, 18 Oct 2011 20:56:08 +0000 Subject: [PATCH] Even smoother combat animations. Allow multiple animations to run concurrently. git-svn-id: https://andors-trail.googlecode.com/svn/trunk@187 08aca716-68be-ccc6-4d58-36f5abd142ac --- .../res/values/loadresources_debug.xml | 2 + .../AndorsTrail/activity/DebugInterface.java | 8 -- .../controller/CombatController.java | 2 +- .../controller/VisualEffectController.java | 104 +++++------------- 4 files changed, 29 insertions(+), 87 deletions(-) diff --git a/AndorsTrail/res/values/loadresources_debug.xml b/AndorsTrail/res/values/loadresources_debug.xml index bd8484b..5eaba9b 100644 --- a/AndorsTrail/res/values/loadresources_debug.xml +++ b/AndorsTrail/res/values/loadresources_debug.xml @@ -11,6 +11,7 @@ [id|iconID|name|category|displaytype|hasManualPrice|baseMarketCost|hasEquipEffect|equip_boostMaxHP|equip_boostMaxAP|equip_moveCostPenalty|equip_attackCost|equip_attackChance|equip_criticalChance|equip_criticalMultiplier|equip_attackDamage_Min|equip_attackDamage_Max|equip_blockChance|equip_damageResistance|equip_conditions[condition|magnitude|]|hasUseEffect|use_boostHP_Min|use_boostHP_Max|use_boostAP_Min|use_boostAP_Max|use_conditionsSource[condition|magnitude|duration|chance|]|hasHitEffect|hit_boostHP_Min|hit_boostHP_Max|hit_boostAP_Min|hit_boostAP_Max|hit_conditionsSource[condition|magnitude|duration|chance|]|hit_conditionsTarget[condition|magnitude|duration|chance|]|hasKillEffect|kill_boostHP_Min|kill_boostHP_Max|kill_boostAP_Min|kill_boostAP_Max|kill_conditionsSource[condition|magnitude|duration|chance|]|]; {debug_dagger1|items_weapons:20|Black heart dagger|0|3|1|6|1||||2|100|30|3|5|10|||||||||||||||||||||||}; {debug_ring1|items_jewelry:4|Black heart ring|7|1|1|3|1|||||50|||10|10|||||||||||||||||||||||}; +{shadow_slayer|items_weapons:60|Shadow of the slayer|0|3|1|0|1||2||7|25|10|2|5|9|||||||||||||||||1|1|1||||}; @@ -31,6 +32,7 @@ {dagger0|1|1|100|} {debug_dagger1|1|1|100|} {debug_ring1|1|1|100|} + {shadow_slayer|1|1|100|} }|}; diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/DebugInterface.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/DebugInterface.java index a249786..86f8f39 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/DebugInterface.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/activity/DebugInterface.java @@ -124,14 +124,6 @@ public final class DebugInterface { mainActivity.showToast("DEBUG: hp set to max", Toast.LENGTH_SHORT); } }) - ,new DebugButton("exp", new OnClickListener() { - @Override - public void onClick(View arg0) { - world.model.player.addExperience(10000); - mainActivity.updateStatus(); - mainActivity.showToast("DEBUG: added 10k exp", Toast.LENGTH_SHORT); - } - }) /* ,new DebugButton("cg", new OnClickListener() { @Override diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/CombatController.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/CombatController.java index 6a76c8b..bb79f1e 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/CombatController.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/CombatController.java @@ -195,10 +195,10 @@ public final class CombatController implements VisualEffectCompletedCallback { } message(msg); + context.mainActivity.updateStatus(); if (lastAttackResult.targetDied) { playerKilledMonster(currentlyAttackedMonster); } - context.mainActivity.updateStatus(); startAttackEffect(attack, model.uiSelections.selectedPosition, this, CALLBACK_PLAYERATTACK); } else { diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/VisualEffectController.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/VisualEffectController.java index 2b0a473..506d6a1 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/VisualEffectController.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/VisualEffectController.java @@ -3,7 +3,7 @@ package com.gpl.rpg.AndorsTrail.controller; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Align; -import android.os.AsyncTask; +import android.os.Handler; import com.gpl.rpg.AndorsTrail.VisualEffectCollection; import com.gpl.rpg.AndorsTrail.VisualEffectCollection.VisualEffect; @@ -14,7 +14,7 @@ import com.gpl.rpg.AndorsTrail.util.Size; import com.gpl.rpg.AndorsTrail.view.MainView; public final class VisualEffectController { - private VisualEffectAnimation currentEffect; + private int effectCount = 0; private final VisualEffectCollection effectTypes; public VisualEffectController(WorldContext world) { @@ -22,64 +22,28 @@ public final class VisualEffectController { } public void startEffect(MainView mainview, Coord position, int effectID, int displayValue, VisualEffectCompletedCallback callback, int callbackValue) { - VisualEffectAnimation e = currentEffect; - if (e != null) { - e.killjoin(); - } - currentEffect = new VisualEffectAnimation(effectTypes.effects[effectID], position, mainview, displayValue, callback, callbackValue); - currentEffect.execute(); + ++effectCount; + (new VisualEffectAnimation(effectTypes.effects[effectID], position, mainview, displayValue, callback, callbackValue)) + .start(); } - public final class VisualEffectAnimation extends AsyncTask { - - @Override - protected Void doInBackground(Void... arg0) { - final int sleepInterval = effect.millisecondPerFrame / 2; - try { - while (isAlive) { - update(); - Thread.sleep(sleepInterval); - if (isCancelled()) return null; - } - Thread.sleep(effect.millisecondPerFrame); - } catch (InterruptedException e) { } - - return null; - } - - @Override - protected void onCancelled() { - isAlive = false; - } - - public void killjoin() { this.cancel(true); } + public final class VisualEffectAnimation extends Handler implements Runnable { - private void update() { - int elapsed = (int)(System.currentTimeMillis() - startTime); - if (elapsed > effect.duration) { - isAlive = false; - return; - } - - int currentFrame = (int) Math.floor(elapsed / effect.millisecondPerFrame); - - if (currentFrame > effect.lastFrame) currentFrame = effect.lastFrame; - if (currentFrame < 0) currentFrame = 0; - final boolean changed = currentFrame != this.lastFrame; - if (!changed) return; - - this.lastFrame = currentFrame; - this.publishProgress(currentFrame); - } - - @Override - protected void onProgressUpdate(Integer... progress) { - super.onProgressUpdate(progress); - redrawFrame(progress[0]); + public void run() { + update(); + if (currentFrame == effect.lastFrame) { + onCompleted(); + } else { + postDelayed(this, effect.millisecondPerFrame); + } } - private void redrawFrame(int frame) { - int tileID = effect.frameIconIDs[frame]; + + private void update() { + ++currentFrame; + int frame = currentFrame; + + int tileID = effect.frameIconIDs[frame]; int textYOffset = -2 * (frame); if (frame >= beginFadeAtFrame && displayText != null) { this.textPaint.setAlpha(255 * (effect.lastFrame - frame) / (effect.lastFrame - beginFadeAtFrame)); @@ -87,26 +51,19 @@ public final class VisualEffectController { view.redrawAreaWithEffect(area, this, tileID, textYOffset, this.textPaint); } - @Override - protected void onPostExecute(Void result) { - super.onPostExecute(result); - VisualEffectController.this.currentEffect = null; + protected void onCompleted() { + --effectCount; view.redrawArea(area, MainView.REDRAW_AREA_EFFECT_COMPLETED); if (callback != null) callback.onVisualEffectCompleted(callbackValue); } - - @Override - protected void onPreExecute() { - super.onPreExecute(); - this.isAlive = true; - redrawFrame(0); + + public void start() { + postDelayed(this, 0); } - private boolean isAlive = false; - private int lastFrame = 0; + private int currentFrame = 0; private final VisualEffect effect; - private final long startTime; private final MainView view; public final Coord position; @@ -129,25 +86,16 @@ public final class VisualEffectController { this.textPaint.setTextSize(view.scaledTileSize * 0.5f); // 32dp. this.textPaint.setAlpha(255); this.textPaint.setTextAlign(Align.CENTER); - this.startTime = System.currentTimeMillis(); this.view = view; this.beginFadeAtFrame = effect.lastFrame / 2; } } - public static interface VisualEffectCompletedCallback { public void onVisualEffectCompleted(int callbackValue); } - public void killCurrentEffect() { - VisualEffectAnimation e = currentEffect; - if (e != null) { - e.killjoin(); - } - } - public boolean isRunningVisualEffect() { - return currentEffect != null; + return effectCount > 0; } } -- 2.49.0