simpleAttackMeleeByDirection method
void
simpleAttackMeleeByDirection({
- Future<
SpriteAnimation> ? animationRight, - Future<
SpriteAnimation> ? animationDown, - Future<
SpriteAnimation> ? animationLeft, - Future<
SpriteAnimation> ? animationUp, - dynamic id,
- required double damage,
- required Direction direction,
- required double height,
- required double width,
- bool withPush = true,
- double? sizePush,
Execute simple attack melee using animation
Implementation
void simpleAttackMeleeByDirection({
Future<SpriteAnimation>? animationRight,
Future<SpriteAnimation>? animationDown,
Future<SpriteAnimation>? animationLeft,
Future<SpriteAnimation>? animationUp,
dynamic id,
required double damage,
required Direction direction,
required double height,
required double width,
bool withPush = true,
double? sizePush,
}) {
Rect positionAttack;
Future<SpriteAnimation>? anim;
double pushLeft = 0;
double pushTop = 0;
Direction attackDirection = direction;
Vector2Rect rectBase = (this.isObjectCollision())
? (this as ObjectCollision).rectCollision
: position;
switch (attackDirection) {
case Direction.up:
positionAttack = Rect.fromLTWH(
rectBase.rect.center.dx - width / 2,
rectBase.rect.top - height,
width,
height,
);
if (animationUp != null) anim = animationUp;
pushTop = (sizePush ?? height) * -1;
break;
case Direction.right:
positionAttack = Rect.fromLTWH(
rectBase.rect.right,
rectBase.rect.center.dy - height / 2,
width,
height,
);
if (animationRight != null) anim = animationRight;
pushLeft = (sizePush ?? width);
break;
case Direction.down:
positionAttack = Rect.fromLTWH(
rectBase.rect.center.dx - width / 2,
rectBase.rect.bottom,
width,
height,
);
if (animationDown != null) anim = animationDown;
pushTop = (sizePush ?? height);
break;
case Direction.left:
positionAttack = Rect.fromLTWH(
rectBase.rect.left - width,
rectBase.rect.center.dy - height / 2,
width,
height,
);
if (animationLeft != null) anim = animationLeft;
pushLeft = (sizePush ?? width) * -1;
break;
case Direction.upLeft:
positionAttack = Rect.fromLTWH(
rectBase.rect.left - width,
rectBase.rect.center.dy - height / 2,
width,
height,
);
if (animationLeft != null) anim = animationLeft;
pushLeft = (sizePush ?? width) * -1;
break;
case Direction.upRight:
positionAttack = Rect.fromLTWH(
rectBase.rect.right,
rectBase.rect.center.dy - height / 2,
width,
height,
);
if (animationRight != null) anim = animationRight;
pushLeft = (sizePush ?? width);
break;
case Direction.downLeft:
positionAttack = Rect.fromLTWH(
rectBase.rect.left - width,
rectBase.rect.center.dy - height / 2,
width,
height,
);
if (animationLeft != null) anim = animationLeft;
pushLeft = (sizePush ?? width) * -1;
break;
case Direction.downRight:
positionAttack = Rect.fromLTWH(
rectBase.rect.right,
rectBase.rect.center.dy - height / 2,
width,
height,
);
if (animationRight != null) anim = animationRight;
pushLeft = (sizePush ?? width);
break;
}
if (anim != null) {
gameRef.add(AnimatedObjectOnce(
animation: anim,
position: positionAttack.toVector2Rect(),
));
}
gameRef.visibleAttackables().where((a) {
return (this is Player
? a.receivesAttackFromPlayer()
: a.receivesAttackFromEnemy()) &&
a.rectAttackable().rect.overlaps(positionAttack);
}).forEach(
(enemy) {
enemy.receiveDamage(damage, id);
final rectAfterPush =
enemy.position.position.translate(pushLeft, pushTop);
if (withPush &&
(enemy is ObjectCollision &&
!(enemy as ObjectCollision)
.isCollision(displacement: rectAfterPush)
.isNotEmpty)) {
enemy.translate(pushLeft, pushTop);
}
},
);
}