From 730691c589cf4fe6677f73525dbb1d7bc47657ad Mon Sep 17 00:00:00 2001 From: Anuke Date: Sat, 5 Mar 2022 12:13:51 -0500 Subject: [PATCH] System for controlling bullet movement --- core/src/mindustry/entities/Mover.java | 9 +++++++++ core/src/mindustry/entities/bullet/BulletType.java | 10 ++++++++++ core/src/mindustry/entities/comp/BulletComp.java | 9 +++++++++ 3 files changed, 28 insertions(+) create mode 100644 core/src/mindustry/entities/Mover.java diff --git a/core/src/mindustry/entities/Mover.java b/core/src/mindustry/entities/Mover.java new file mode 100644 index 0000000000..93e127999f --- /dev/null +++ b/core/src/mindustry/entities/Mover.java @@ -0,0 +1,9 @@ +package mindustry.entities; + +import arc.math.geom.*; +import mindustry.gen.*; + +/** Applies custom movement to a bullet. */ +public interface Mover{ + float move(Bullet bullet); +} diff --git a/core/src/mindustry/entities/bullet/BulletType.java b/core/src/mindustry/entities/bullet/BulletType.java index efce92afb4..db147ccaff 100644 --- a/core/src/mindustry/entities/bullet/BulletType.java +++ b/core/src/mindustry/entities/bullet/BulletType.java @@ -501,6 +501,11 @@ public class BulletType extends Content implements Cloneable{ return create(owner, team, x, y, angle, -1, velocityScl, lifetimeScl, null); } + + public @Nullable Bullet create(Entityc owner, Team team, float x, float y, float angle, float velocityScl, float lifetimeScl, Mover mover){ + return create(owner, team, x, y, angle, -1, velocityScl, lifetimeScl, null, mover); + } + public @Nullable Bullet create(Bullet parent, float x, float y, float angle){ return create(parent.owner, parent.team, x, y, angle); } @@ -514,6 +519,10 @@ public class BulletType extends Content implements Cloneable{ } public @Nullable Bullet create(@Nullable Entityc owner, Team team, float x, float y, float angle, float damage, float velocityScl, float lifetimeScl, Object data){ + return create(owner, team, x, y, angle, damage, velocityScl, lifetimeScl, data, null); + } + + public @Nullable Bullet create(@Nullable Entityc owner, Team team, float x, float y, float angle, float damage, float velocityScl, float lifetimeScl, Object data, @Nullable Mover mover){ if(spawnUnit != null){ //don't spawn units clientside! if(!net.client()){ @@ -548,6 +557,7 @@ public class BulletType extends Content implements Cloneable{ bullet.data = data; bullet.drag = drag; bullet.hitSize = hitSize; + bullet.mover = mover; bullet.damage = (damage < 0 ? this.damage : damage) * bullet.damageMultiplier(); //reset trail if(bullet.trail != null){ diff --git a/core/src/mindustry/entities/comp/BulletComp.java b/core/src/mindustry/entities/comp/BulletComp.java index 7c860da285..ac3418fe76 100644 --- a/core/src/mindustry/entities/comp/BulletComp.java +++ b/core/src/mindustry/entities/comp/BulletComp.java @@ -2,12 +2,14 @@ package mindustry.entities.comp; import arc.func.*; import arc.graphics.g2d.*; +import arc.math.*; import arc.math.geom.*; import arc.struct.*; import arc.util.*; import mindustry.annotations.Annotations.*; import mindustry.content.*; import mindustry.core.*; +import mindustry.entities.*; import mindustry.entities.bullet.*; import mindustry.game.*; import mindustry.game.Teams.*; @@ -35,6 +37,7 @@ abstract class BulletComp implements Timedc, Damagec, Hitboxc, Teamc, Posc, Draw @ReadOnly private float rotation; + transient @Nullable Mover mover; transient boolean absorbed, hit; transient @Nullable Trail trail; @@ -119,6 +122,12 @@ abstract class BulletComp implements Timedc, Damagec, Hitboxc, Teamc, Posc, Draw @Override public void update(){ + if(mover != null){ + float v = mover.move(self()) * Time.delta; + float ang = vel.angle(); + vel.add(Angles.trnsx(ang, 0f, v), Angles.trnsy(ang, 0f, v)).limit(type.speed); + } + type.update(self()); if(type.collidesTiles && type.collides && type.collidesGround){