我有一个攻击类,有基本的伤害和成本,并在游戏中工作。最初,我把它放在人工智能瞄准算法上,这样你就可以选择攻击,你就可以看到敌人受到伤害。
问题是,现在我想动画的攻击,我发现他们不符合一个单一的模式,如其他图形。如果你是一个最后的幻想游戏,有攻击射击火球,这样一个单一的图像将在屏幕上移动。攻击就像激光束,所以图像在屏幕上移动时会增长。攻击时会有东西从屏幕顶部传来,是各种礼仪。
我想让这个类--我现在有了一个带有动画方法的抽象类--并从中派生出几种类型的攻击类,但我担心这会增加额外的复杂性。我可以重写动画方法,但这会使攻击类变得巨大并浪费内存。
有人对我能做什么有什么建议吗?
发布于 2011-08-19 12:48:56
不久前,我问了自己一个类似的问题,下面是我想出的(使用基于组件的系统):
创建一个只有持续时间的AnimationFrame
类。当它更新时,它会更新已过的时间。(如果您有一个baseObject
类,请确保这是从它派生的)
创建一个从ImageAnimation
派生的AnimationFrame
类,它有一个SpriteSheet
变量来保存您的图像,可能还有一个matrix
来调整/裁剪spritesheet的大小。当更新时,它会绘制图像(裁剪/调整大小)。它还应该super.update()
来更新已过的时间。
创建一个可以保存SpriteSheetManager
对象的ImageAnimation
类。当SpriteSheetManager
更新时,它会更新当前的ImageAnimation
对象(即当前帧)。当完成当前的ImageAnimation
(持续时间已过)时,管理器应该转移到下一个ImageAnimation
。您必须手动添加ImageAnimations
并指定图像/持续时间/矩阵(或从数据文件中以某种方式将其提取)。
将SpriteSheetManager
添加到您的Player对象中,以便它们在玩家更新时进行更新。
现在,如果您有一个baseObject
类,并且您的游戏对象存储为一个baseObjects
数组,那么您可以将SpriteSheetManager
添加到游戏对象中,这样它就可以更新了。这对于占据整个屏幕的大型法术是有用的。
还可以增强ImageAnimation
类(或创建派生类)来处理滚动图像。
希望这能给你一些想法。
发布于 2011-08-19 16:34:59
正如上面提到的,它实际上取决于您编写代码的方式。但这似乎是一种不难实现的通用方法:
(伪码)
public class User {
public void ExecuteAttack(Enemy target, string selectedAttack){
//calculates damage
int dmg = (baseDamge * somedamageformula);
Animation animation = AttackDictionary.getAnimation(name)
Animator.animateAttack(animation, damage); //executes the animation, might want to show damage number in animation
}
}
public class battle{
array enemies;
array party;
//etc
public void ExecuteAttack(PartyMember user, Enemy target){
user.attack(target, selectedAttack);
}
private void animationComplete(e Event){
nextTurn();
}
}
public class AttackDictionary{
enum attacks{
//long list of all atacks, could be an array an enem or you could collect it from xml or whatevr
}
public static void getAnimation(String attackname){
//you could prevent a big switch statement by using xml or a different method that
//allows you to couple info in 1 item (associative array for example)
switch attacks.attackname{
case attacks.slash{
return new slashanimation.png; //return the animation (spritesheet, model etc)
}
}
}
}
我并不是说这是最好的方法,这正是我应该做的:)
发布于 2011-08-19 06:46:52
这真的取决于您的代码是如何组织的,我认为没有人能够给出一个非常好的答案,您应该和不应该做什么,而不知道您的代码和样式的深入。无论如何,这听起来不像是一个容易消耗大量问题内存的问题。但是它可能会给程序员带来问题,因为它是大量的代码。所以,不要太担心记忆,担心做一些你可以使用的东西。
https://gamedev.stackexchange.com/questions/16176
复制