From e04ada04bc14865bf048674da7edfc341d4f5ed1 Mon Sep 17 00:00:00 2001 From: Nico Nico Nii Date: Fri, 17 Jun 2022 06:23:02 -0500 Subject: [PATCH] Implement knife animation and attaching Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com> Co-authored-by: sigonasr2 --- sprites/knife-swing.gif | Bin 0 -> 434 bytes src/sig/engine/Action.java | 1 + src/sig/engine/Sprite.java | 1 + src/sig/objects/ConfigureControls.java | 3 +- src/sig/objects/EditorRenderer.java | 3 +- src/sig/objects/Player.java | 12 +++++ src/sig/objects/actor/Attachable.java | 8 ++++ src/sig/objects/actor/AttachableObject.java | 38 ++++++++++++++++ src/sig/objects/weapons/KnifeSwing.java | 46 ++++++++++++++++++++ 9 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 sprites/knife-swing.gif create mode 100644 src/sig/objects/actor/Attachable.java create mode 100644 src/sig/objects/actor/AttachableObject.java create mode 100644 src/sig/objects/weapons/KnifeSwing.java diff --git a/sprites/knife-swing.gif b/sprites/knife-swing.gif new file mode 100644 index 0000000000000000000000000000000000000000..cc25404bba9efd58104bd89c21e5c0a55b1372ec GIT binary patch literal 434 zcmZ?wbh9u|T)?2fuvCH}dRb}q$(+@9Q~tc~{lBB~RZ`L!r?|x$5vAf`mYnX15`pce zq02(!PZk${S~v6m;ZN`SPhZK~+-}z*FQDaR5L6w~u&j0d&Ali7|Np=4(f{7<|8pCD zwWPjj_B~RoyTDJP=05{ahK4|g0SG{TVPL!NFrmOhN2>qClA@D2R`V~cDDhewwf@GM zveR#?_CMHA;iEVE{EIDBXXn<~zOkrudSCVK)1K+)%G2LB6jU{b*VfcGRPRXsy>FW=jILWCmYC+_p%vrO8=T2HSBV={!vg}zMoeHaKgSRdB-#IN~O?1=z zJ$ro*AMrW1zh~{q;~rwEgo#!Zf&%{AnV^I3$oVoq#Wc(`3a*=vr+#zjZFCAPCLGFSru Dq=6D+ literal 0 HcmV?d00001 diff --git a/src/sig/engine/Action.java b/src/sig/engine/Action.java index 0fbd989..757e084 100644 --- a/src/sig/engine/Action.java +++ b/src/sig/engine/Action.java @@ -11,6 +11,7 @@ public enum Action { MOVE_LEFT(new KeyBind(KeyEvent.VK_LEFT),new KeyBind(KeyEvent.VK_A)), JUMP(new KeyBind(KeyEvent.VK_SPACE),new KeyBind(KeyEvent.VK_W)), FALL(new KeyBind(KeyEvent.VK_DOWN),new KeyBind(KeyEvent.VK_S)), + ATTACK(new KeyBind(KeyEvent.VK_Z)), SLIDE(new KeyBind(KeyEvent.VK_CONTROL)), LEVEL_EDITOR(new KeyBind(KeyEvent.VK_F2)), PLAY_GAME(new KeyBind(KeyEvent.VK_F1)), diff --git a/src/sig/engine/Sprite.java b/src/sig/engine/Sprite.java index b1741e5..c827593 100644 --- a/src/sig/engine/Sprite.java +++ b/src/sig/engine/Sprite.java @@ -26,6 +26,7 @@ public class Sprite{ public static AnimatedSprite ERINA_JUMP_FALL = new AnimatedSprite(new File(new File("..","sprites"),"erina_jump_fall.gif"),32,32); public static AnimatedSprite ERINA_SLIDE1 = new AnimatedSprite(new File(new File("..","sprites"),"erina_slide1.gif"),32,32); public static AnimatedSprite ERINA_SLIDE = new AnimatedSprite(new File(new File("..","sprites"),"erina_slide.gif"),32,32); + public static AnimatedSprite KNIFE_SWING = new AnimatedSprite(new File(new File("..","sprites"),"knife-swing.gif"),32,32); diff --git a/src/sig/objects/ConfigureControls.java b/src/sig/objects/ConfigureControls.java index 71bac77..43b2a2b 100644 --- a/src/sig/objects/ConfigureControls.java +++ b/src/sig/objects/ConfigureControls.java @@ -152,7 +152,8 @@ public class ConfigureControls extends Object{ public void KeyPressed(Action a) { switch(a) { case PLAY_GAME:{ - RabiClone.OBJ.remove(RabiClone.level_renderer); + RabiClone.OBJ.clear(); + RabiClone.ResetGame(); RabiClone.OBJ.add(RabiClone.level_renderer = new LevelRenderer(RabiClone.p)); RabiClone.StartGame(); }break; diff --git a/src/sig/objects/EditorRenderer.java b/src/sig/objects/EditorRenderer.java index 153e267..21ae7da 100644 --- a/src/sig/objects/EditorRenderer.java +++ b/src/sig/objects/EditorRenderer.java @@ -219,7 +219,8 @@ public class EditorRenderer extends LevelRenderer{ int tileY = (int)(RabiClone.MOUSE_POS.getY()+getY())/Tile.TILE_HEIGHT; switch (a) { case PLAY_GAME:{ - RabiClone.OBJ.remove(RabiClone.level_renderer); + RabiClone.OBJ.clear(); + RabiClone.ResetGame(); RabiClone.OBJ.add(RabiClone.level_renderer = new LevelRenderer(RabiClone.p)); RabiClone.StartGame(); }break; diff --git a/src/sig/objects/Player.java b/src/sig/objects/Player.java index 463bf7b..9b930b1 100644 --- a/src/sig/objects/Player.java +++ b/src/sig/objects/Player.java @@ -12,6 +12,7 @@ import sig.map.View; import sig.objects.actor.PhysicsObject; import sig.objects.actor.RenderedObject; import sig.objects.actor.State; +import sig.objects.weapons.KnifeSwing; import sig.utils.TimeUtils; public class Player extends PhysicsObject implements RenderedObject{ @@ -20,6 +21,9 @@ public class Player extends PhysicsObject implements RenderedObject{ final static long jump_fall_AnimationWaitTime = TimeUtils.millisToNanos(200); final static long slide_AnimationWaitTime = TimeUtils.millisToNanos(100); final static long slide_duration = TimeUtils.millisToNanos(700); + final static long weaponSwingAnimationTime = TimeUtils.millisToNanos(333); + + long weaponSwingTime = 0; State prvState = state; @@ -67,6 +71,9 @@ public class Player extends PhysicsObject implements RenderedObject{ switch (state) { case ATTACK: + if (RabiClone.TIME - weaponSwingTime > weaponSwingAnimationTime) { + state=State.IDLE; + } break; case FALLING: if (prvState != State.FALLING) { @@ -204,6 +211,11 @@ public class Player extends PhysicsObject implements RenderedObject{ default: break; } + if (a == Action.ATTACK&&(state==State.IDLE||state==State.FALLING||state==State.JUMP)&&(RabiClone.TIME-weaponSwingTime>=weaponSwingAnimationTime)) { + RabiClone.OBJ.add(new KnifeSwing(Sprite.KNIFE_SWING,15,RabiClone.p,this)); + state=State.ATTACK; + weaponSwingTime=RabiClone.TIME; + } if (groundCollision) { if (spacebarReleased && (a == Action.JUMP) && jumpCount > 0) { state = State.JUMP; diff --git a/src/sig/objects/actor/Attachable.java b/src/sig/objects/actor/Attachable.java new file mode 100644 index 0000000..ba73c7f --- /dev/null +++ b/src/sig/objects/actor/Attachable.java @@ -0,0 +1,8 @@ +package sig.objects.actor; + +import sig.engine.objects.Object; + +public interface Attachable { + Object getAttachedObject(); + void setAttachedObject(Object o); +} diff --git a/src/sig/objects/actor/AttachableObject.java b/src/sig/objects/actor/AttachableObject.java new file mode 100644 index 0000000..cb8a6a8 --- /dev/null +++ b/src/sig/objects/actor/AttachableObject.java @@ -0,0 +1,38 @@ +package sig.objects.actor; + +import sig.engine.AnimatedSprite; +import sig.engine.Panel; +import sig.engine.objects.AnimatedObject; +import sig.engine.objects.Object; + +public abstract class AttachableObject extends AnimatedObject implements Attachable{ + Object attached; + + protected AttachableObject(AnimatedSprite spr, double animationSpd, Panel panel, Object attachedObj) { + super(spr, animationSpd, panel); + setAttachedObject(attachedObj); + } + + + + protected Object getAttached() { + return attached; + } + + + + protected void setAttached(Object attached) { + this.attached = attached; + } + + + + public void setAttachedObject(Object o) { + this.attached=o; + } + + public Object getAttachedObject() { + return this.attached; + } + +} diff --git a/src/sig/objects/weapons/KnifeSwing.java b/src/sig/objects/weapons/KnifeSwing.java new file mode 100644 index 0000000..223ab49 --- /dev/null +++ b/src/sig/objects/weapons/KnifeSwing.java @@ -0,0 +1,46 @@ +package sig.objects.weapons; + +import sig.engine.AnimatedSprite; +import sig.engine.Panel; +import sig.engine.Transform; +import sig.objects.actor.AttachableObject; +import sig.objects.actor.RenderedObject; +import sig.engine.objects.Object; + +public class KnifeSwing extends AttachableObject implements RenderedObject{ + + final byte frameCount = 5; //Number of frames before animation ends. + + public KnifeSwing(AnimatedSprite spr, double animationSpd, Panel panel, Object attachedObj) { + super(spr, animationSpd, panel, attachedObj); + } + + @Override + public void update(double updateMult) { + super.update(updateMult); + if (getCurrentFrame()>frameCount) { + setMarkedForDeletion(true); + return; + } + if (getSpriteTransform()==Transform.HORIZONTAL) { + setX(getAttachedObject().getX()-getAnimatedSpr().getWidth()/2); + } else { + setX(getAttachedObject().getX()+getAnimatedSpr().getWidth()/2); + } + setY(getAttachedObject().getY()); + } + + @Override + public void draw(byte[] p) { + } + + @Override + public Transform getSpriteTransform() { + return getAttached().getSpriteTransform()==Transform.HORIZONTAL||getAttached().getSpriteTransform()==Transform.HORIZ_VERTIC?Transform.NONE:Transform.HORIZONTAL; + } + + @Override + public boolean isFriendlyObject() { + return true; + } +}