Weapon mount cleanup

This commit is contained in:
Anuken
2020-01-11 17:02:38 -05:00
parent 6f3c771d73
commit f7c4ea3e58
11 changed files with 173 additions and 160 deletions

View File

@@ -10,14 +10,14 @@ import mindustry.gen.*;
import mindustry.type.*;
public class UnitTypes implements ContentList{
public static UnitType
public static UnitDef
draug, spirit, phantom,
wraith, ghoul, revenant, lich, reaper,
dagger, crawler, titan, fortress, eruptor, chaosArray, eradicator;
@Override
public void load(){
draug = new UnitType("draug", MinerDrone::new){{
draug = new UnitDef("draug", MinerDrone::new){{
flying = true;
drag = 0.01f;
speed = 0.3f;
@@ -27,12 +27,9 @@ public class UnitTypes implements ContentList{
minePower = 0.9f;
engineSize = 1.8f;
engineOffset = 5.7f;
weapon = new Weapon("you have incurred my wrath. prepare to die."){{
bullet = Bullets.lancerLaser;
}};
}};
spirit = new UnitType("spirit", RepairDrone::new){{
spirit = new UnitDef("spirit", RepairDrone::new){{
flying = true;
drag = 0.01f;
speed = 0.42f;
@@ -53,7 +50,7 @@ public class UnitTypes implements ContentList{
}};
}};
phantom = new UnitType("phantom", BuilderDrone::new){{
phantom = new UnitDef("phantom", BuilderDrone::new){{
flying = true;
drag = 0.01f;
mass = 2f;
@@ -76,7 +73,7 @@ public class UnitTypes implements ContentList{
}};
}};
dagger = new UnitType("dagger", GroundUnit::new){{
dagger = new UnitDef("dagger", GroundUnit::new){{
maxVelocity = 1.1f;
speed = 0.2f;
drag = 0.4f;
@@ -92,7 +89,7 @@ public class UnitTypes implements ContentList{
}};
}};
crawler = new UnitType("crawler", GroundUnit::new){{
crawler = new UnitDef("crawler", GroundUnit::new){{
maxVelocity = 1.27f;
speed = 0.285f;
drag = 0.4f;
@@ -123,7 +120,7 @@ public class UnitTypes implements ContentList{
}};
}};
titan = new UnitType("titan", GroundUnit::new){{
titan = new UnitDef("titan", GroundUnit::new){{
maxVelocity = 0.8f;
speed = 0.22f;
drag = 0.4f;
@@ -145,7 +142,7 @@ public class UnitTypes implements ContentList{
}};
}};
fortress = new UnitType("fortress", GroundUnit::new){{
fortress = new UnitDef("fortress", GroundUnit::new){{
maxVelocity = 0.78f;
speed = 0.15f;
drag = 0.4f;
@@ -167,7 +164,7 @@ public class UnitTypes implements ContentList{
}};
}};
eruptor = new UnitType("eruptor", GroundUnit::new){{
eruptor = new UnitDef("eruptor", GroundUnit::new){{
maxVelocity = 0.81f;
speed = 0.16f;
drag = 0.4f;
@@ -189,7 +186,7 @@ public class UnitTypes implements ContentList{
}};
}};
chaosArray = new UnitType("chaos-array", GroundUnit::new){{
chaosArray = new UnitDef("chaos-array", GroundUnit::new){{
maxVelocity = 0.68f;
speed = 0.12f;
drag = 0.4f;
@@ -213,7 +210,7 @@ public class UnitTypes implements ContentList{
}};
}};
eradicator = new UnitType("eradicator", GroundUnit::new){{
eradicator = new UnitDef("eradicator", GroundUnit::new){{
maxVelocity = 0.68f;
speed = 0.12f;
drag = 0.4f;
@@ -238,7 +235,7 @@ public class UnitTypes implements ContentList{
}};
}};
wraith = new UnitType("wraith", FlyingUnit::new){{
wraith = new UnitDef("wraith", FlyingUnit::new){{
speed = 0.3f;
maxVelocity = 1.9f;
drag = 0.01f;
@@ -257,7 +254,7 @@ public class UnitTypes implements ContentList{
}};
}};
ghoul = new UnitType("ghoul", FlyingUnit::new){{
ghoul = new UnitDef("ghoul", FlyingUnit::new){{
health = 220;
speed = 0.2f;
maxVelocity = 1.4f;
@@ -281,7 +278,7 @@ public class UnitTypes implements ContentList{
}};
}};
revenant = new UnitType("revenant", HoverUnit::new){{
revenant = new UnitDef("revenant", HoverUnit::new){{
health = 1000;
mass = 5f;
hitsize = 20f;
@@ -312,7 +309,7 @@ public class UnitTypes implements ContentList{
}};
}};
lich = new UnitType("lich", HoverUnit::new){{
lich = new UnitDef("lich", HoverUnit::new){{
health = 6000;
mass = 20f;
hitsize = 40f;
@@ -345,7 +342,7 @@ public class UnitTypes implements ContentList{
}};
}};
reaper = new UnitType("reaper", HoverUnit::new){{
reaper = new UnitDef("reaper", HoverUnit::new){{
health = 11000;
mass = 30f;
hitsize = 56f;

View File

@@ -1,40 +1,87 @@
package mindustry.entities;
import arc.*;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.util.*;
import mindustry.*;
import mindustry.annotations.Annotations.*;
import mindustry.entities.bullet.*;
import mindustry.entities.traits.*;
import mindustry.entities.type.*;
import mindustry.gen.*;
import mindustry.type.*;
import static mindustry.Vars.net;
public class Weapons{
/** 1 */
private static final int[] one = {1};
/** minimum cursor distance from player, fixes 'cross-eyed' shooting */
private static final float minAimDst = 20f;
/** temporary weapon sequence number */
private static int sequenceNum = 0;
private WeaponMount[] mounts;
private UnitDef lastDef;
/** weapon mount array, never null */
private WeaponMount[] mounts = {};
public void update(Unit unit){
check(unit);
public void init(Unit unit){
mounts = new WeaponMount[unit.type().weapons.size];
for(int i = 0; i < mounts.length; i++){
mounts[i] = new WeaponMount(unit.type().weapons.get(i));
}
}
/** Aim at something. This will make all mounts point at it. */
public void aim(Unit unit, float x, float y){
Tmp.v1.set(x, y).sub(unit.x, unit.y);
if(Tmp.v1.len() < minAimDst) Tmp.v1.setLength(minAimDst);
x = Tmp.v1.x + unit.x;
y = Tmp.v1.y + unit.y;
for(WeaponMount mount : mounts){
Weapon weapon = mount.weapon;
mount.aimX = x;
mount.aimY = y;
}
}
for(int i : (weapon.mirror ? Mathf.signs : one)){
i *= Mathf.sign(weapon.flipped);
/** Update shooting and rotation for this unit. */
public void update(Unit unit){
for(WeaponMount mount : mounts){
Weapon weapon = mount.weapon;
mount.reload -= Time.delta();
float rotation = unit.rotation - 90;
//rotate if applicable
if(weapon.rotate){
float axisXOffset = weapon.mirror ? 0f : weapon.x;
float axisX = unit.x + Angles.trnsx(rotation, axisXOffset, weapon.y),
axisY = unit.y + Angles.trnsy(rotation, axisXOffset, weapon.y);
mount.rotation = Angles.moveToward(mount.rotation, Angles.angle(axisX, axisY, mount.aimX, mount.aimY), weapon.rotateSpeed);
}
//shoot if applicable
//TODO only shoot if angle is reached, don't shoot inaccurately
if(mount.reload <= 0){
for(int i : (weapon.mirror && !weapon.alternate ? Mathf.signs : one)){
i *= Mathf.sign(weapon.flipped) * Mathf.sign(mount.side);
//m a t h
float weaponRotation = rotation + (weapon.rotate ? mount.rotation : 0);
float mountX = unit.x + Angles.trnsx(rotation, weapon.x * i, weapon.y),
mountY = unit.y + Angles.trnsy(rotation, weapon.x * i, weapon.y);
float shootX = mountX + Angles.trnsx(weaponRotation, weapon.shootX * i, weapon.shootY),
shootY = mountY + Angles.trnsy(weaponRotation, weapon.shootX * i, weapon.shootY);
float shootAngle = weapon.rotate ? weaponRotation : Angles.angle(shootX, shootY, mount.aimX, mount.aimY);
shoot(unit, weapon, shootX, shootY, shootAngle);
}
mount.side = !mount.side;
mount.reload = weapon.reload;
}
}
}
/** Draw weapon mounts. */
public void draw(Unit unit){
check(unit);
for(WeaponMount mount : mounts){
Weapon weapon = mount.weapon;
@@ -42,12 +89,12 @@ public class Weapons{
i *= Mathf.sign(weapon.flipped);
float rotation = unit.rotation - 90 + (weapon.rotate ? mount.rotation : 0);
float trY = weapon.length - mount.reload / weapon.reload * weapon.recoil;
float trY = weapon.y - (mount.reload / weapon.reload * weapon.recoil) * (weapon.alternate ? Mathf.num(i == Mathf.sign(mount.side)) : 1);
float width = i > 0 ? -weapon.region.getWidth() : weapon.region.getWidth();
Draw.rect(weapon.region,
unit.x + Angles.trnsx(rotation, weapon.width * i, trY),
unit.y + Angles.trnsy(rotation, weapon.width * i, trY),
unit.x + Angles.trnsx(rotation, weapon.x * i, trY),
unit.y + Angles.trnsy(rotation, weapon.x * i, trY),
width * Draw.scl,
weapon.region.getHeight() * Draw.scl,
rotation - 90);
@@ -55,54 +102,19 @@ public class Weapons{
}
}
//check mount validity
private void check(Unit unit){
if(mounts == null || mounts.length != unit.type().weapons.size || lastDef != unit.type()){
mounts = new WeaponMount[unit.type().weapons.size];
for(int i = 0; i < mounts.length; i++){
mounts[i] = new WeaponMount(unit.type().weapons.get(i));
}
lastDef = unit.type();
}
}
//region weapon code
@Remote(targets = Loc.server, called = Loc.both, unreliable = true)
public static void onPlayerShootWeapon(Player player, float x, float y, float rotation, boolean left){
if(player == null) return;
//clients do not see their own shoot events: they are simulated completely clientside to prevent laggy visuals
//messing with the firerate or any other stats does not affect the server (take that, script kiddies!)
if(net.client() && player == Vars.player){
return;
}
shootDirect(player, x, y, rotation, left);
}
@Remote(targets = Loc.server, called = Loc.both, unreliable = true)
public static void onGenericShootWeapon(ShooterTrait shooter, float x, float y, float rotation, boolean left){
if(shooter == null) return;
shootDirect(shooter, x, y, rotation, left);
}
public static void shootDirect(ShooterTrait shooter, float offsetX, float offsetY, float rotation, boolean left){
float x = shooter.getX() + offsetX;
float y = shooter.getY() + offsetY;
private void shoot(ShooterTrait shooter, Weapon weapon, float x, float y, float rotation){
float baseX = shooter.getX(), baseY = shooter.getY();
Weapon weapon = shooter.getWeapon();
weapon.shootSound.at(x, y, Mathf.random(0.8f, 1.0f));
sequenceNum = 0;
if(weapon.shotDelay > 0.01f){
Angles.shotgun(weapon.shots, weapon.spacing, rotation, f -> {
Time.run(sequenceNum * weapon.shotDelay, () -> weapon.bullet(shooter, x + shooter.getX() - baseX, y + shooter.getY() - baseY, f + Mathf.range(weapon.inaccuracy)));
Time.run(sequenceNum * weapon.shotDelay, () -> bullet(shooter, weapon, x + shooter.getX() - baseX, y + shooter.getY() - baseY, f + Mathf.range(weapon.inaccuracy)));
sequenceNum++;
});
}else{
Angles.shotgun(weapon.shots, weapon.spacing, rotation, f -> weapon.bullet(shooter, x, y, f + Mathf.range(weapon.inaccuracy)));
Angles.shotgun(weapon.shots, weapon.spacing, rotation, f -> bullet(shooter, weapon, x, y, f + Mathf.range(weapon.inaccuracy)));
}
BulletType ammo = weapon.bullet;
@@ -115,64 +127,16 @@ public class Weapons{
boolean parentize = ammo.keepVelocity;
Effects.shake(weapon.shake, weapon.shake, x, y);
Effects.effect(weapon.ejectEffect, x, y, rotation * -Mathf.sign(left));
Effects.effect(weapon.ejectEffect, x, y, rotation);
Effects.effect(ammo.shootEffect, x + Tmp.v1.x, y + Tmp.v1.y, rotation, parentize ? shooter : null);
Effects.effect(ammo.smokeEffect, x + Tmp.v1.x, y + Tmp.v1.y, rotation, parentize ? shooter : null);
//reset timer for remote players
shooter.getTimer().get(shooter.getShootTimer(left), weapon.reload);
}
public void load(){
region = Core.atlas.find(name + "-equip", Core.atlas.find(name, Core.atlas.find("clear")));
}
public void update(ShooterTrait shooter, float pointerX, float pointerY){
for(boolean left : Mathf.booleans){
Tmp.v1.set(pointerX, pointerY).sub(shooter.getX(), shooter.getY());
if(Tmp.v1.len() < minPlayerDist) Tmp.v1.setLength(minPlayerDist);
float cx = Tmp.v1.x + shooter.getX(), cy = Tmp.v1.y + shooter.getY();
float ang = Tmp.v1.angle();
Tmp.v1.trns(ang - 90, width * Mathf.sign(left), length + Mathf.range(lengthRand));
update(shooter, shooter.getX() + Tmp.v1.x, shooter.getY() + Tmp.v1.y, Angles.angle(shooter.getX() + Tmp.v1.x, shooter.getY() + Tmp.v1.y, cx, cy), left);
}
}
public void update(ShooterTrait shooter, float mountX, float mountY, float angle, boolean left){
if(shooter.getTimer().get(shooter.getShootTimer(left), reload)){
if(alternate){
shooter.getTimer().reset(shooter.getShootTimer(!left), reload / 2f);
}
shoot(shooter, mountX - shooter.getX(), mountY - shooter.getY(), angle, left);
}
}
public void shoot(ShooterTrait p, float x, float y, float angle, boolean left){
if(net.client()){
//call it directly, don't invoke on server
shootDirect(p, x, y, angle, left);
}else{
if(p instanceof Player){ //players need special weapon handling logic
Call.onPlayerShootWeapon((Player)p, x, y, angle, left);
}else{
Call.onGenericShootWeapon(p, x, y, angle, left);
}
}
}
void bullet(ShooterTrait owner, float x, float y, float angle){
if(owner == null) return;
private void bullet(ShooterTrait owner, Weapon weapon, float x, float y, float angle){
Tmp.v1.trns(angle, 3f);
Bullet.create(bullet, owner, owner.getTeam(), x + Tmp.v1.x, y + Tmp.v1.y, angle, (1f - velocityRnd) + Mathf.random(velocityRnd));
Bullet.create(weapon.bullet, owner, owner.getTeam(), x + Tmp.v1.x, y + Tmp.v1.y, angle, (1f - weapon.velocityRnd) + Mathf.random(weapon.velocityRnd));
}
//endregion
private static class WeaponMount{
/** reload in frames; 0 means ready to fire */
float reload;
@@ -180,6 +144,10 @@ public class Weapons{
float rotation;
/** weapon associated with this mount */
Weapon weapon;
/** aiming position in world coordinates */
float aimX, aimY;
/** side that's being shot - only valid for mirrors */
boolean side;
public WeaponMount(Weapon weapon){
this.weapon = weapon;

View File

@@ -28,7 +28,7 @@ import java.io.*;
import static mindustry.Vars.*;
public abstract class Unit extends DestructibleEntity implements SaveTrait, TargetTrait, SyncTrait, DrawTrait, TeamTrait{
public abstract class Unit extends DestructibleEntity implements SaveTrait, TargetTrait, SyncTrait, DrawTrait, TeamTrait, ShooterTrait{
/** Total duration of hit flash effect */
public static final float hitDuration = 9f;
/** Percision divisor of velocity, used when writing. For example a value of '2' would mean the percision is 1/2 = 0.5-size chunks. */
@@ -42,12 +42,29 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
public float rotation;
protected final Interpolator interpolator = new Interpolator();
/** status effects */
protected final Statuses status = new Statuses();
/** current item held */
protected final ItemStack item = new ItemStack(content.item(0), 0);
/** holds weapon aiming positions and angles */
protected final Weapons weapons = new Weapons();
/** team; can be changed at any time */
protected Team team = Team.sharded;
/** timers for drowning and getting hit */
protected float drownTime, hitTime;
/** this unit's type; do not change internally without calling setType(...) */
protected UnitDef type;
public void setType(UnitDef type){
this.type = type;
clampHealth();
weapons.init(this);
}
public UnitDef type(){
return type;
}
@Override
public boolean collidesGrid(int x, int y){
@@ -59,6 +76,11 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
return team;
}
@Override
public Weapons getWeapons(){
return weapons;
}
@Override
public void interpolate(){
interpolator.update();
@@ -137,18 +159,18 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
@Override
public void hitbox(Rect rect){
rect.setSize(type().hitsize).setCenter(x, y);
rect.setSize(type.hitsize).setCenter(x, y);
}
@Override
public void hitboxTile(Rect rect){
rect.setSize(type().hitsizeTile).setCenter(x, y);
rect.setSize(type.hitsizeTile).setCenter(x, y);
}
@Override
public float drag(){
return type().drag;
return type.drag;
}
@Override
@@ -181,8 +203,6 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
this.rotation = rotation;
}
public abstract UnitDef type();
public void writeSave(DataOutput stream, boolean net) throws IOException{
if(item.item == null) item.item = Items.copper;
@@ -210,7 +230,7 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
}
public boolean isImmune(StatusEffect effect){
return type().immunities.contains(effect);
return type.immunities.contains(effect);
}
public boolean isOutOfBounds(){
@@ -411,8 +431,8 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
}
public void drawLight(){
if(type().lightRadius > 0){
renderer.lights.add(x, y, type().lightRadius, type().lightColor, 0.6f);
if(type.lightRadius > 0){
renderer.lights.add(x, y, type.lightRadius, type.lightColor, 0.6f);
}
}
@@ -471,15 +491,15 @@ public abstract class Unit extends DestructibleEntity implements SaveTrait, Targ
public abstract TextureRegion getIconRegion();
public final int getItemCapacity(){
return type().itemCapacity;
return type.itemCapacity;
}
@Override
public float mass(){
return type().mass;
return type.mass;
}
public boolean isFlying(){
return type().flying;
return type.flying;
}
}

View File

@@ -321,11 +321,12 @@ public class EventType{
}
//TODO rename
public static class MechChangeEvent{
public final Player player;
public final Mech mech;
public final UnitDef mech;
public MechChangeEvent(Player player, Mech mech){
public MechChangeEvent(Player player, UnitDef mech){
this.player = player;
this.mech = mech;
}

View File

@@ -233,12 +233,12 @@ public class TypeIO{
}
@WriteClass(UnitDef.class)
public static void writeUnitType(ByteBuffer buffer, UnitDef effect){
public static void writeUnitDef(ByteBuffer buffer, UnitDef effect){
buffer.putShort(effect.id);
}
@ReadClass(UnitDef.class)
public static UnitDef readUnitType(ByteBuffer buffer){
public static UnitDef readUnitDef(ByteBuffer buffer){
return content.getByID(ContentType.unit, buffer.getShort());
}

View File

@@ -281,7 +281,7 @@ public class ContentParser{
UnitType unit;
if(locate(ContentType.unit, name) == null){
Class<BaseUnit> type = resolve(legacyUnitMap.get(Strings.capitalize(getType(value)), getType(value)), "mindustry.entities.type.base");
unit = new UnitType(mod + "-" + name, supply(type));
unit = new UnitDef(mod + "-" + name, supply(type));
}else{
unit = locate(ContentType.unit, name);
}

View File

@@ -20,13 +20,8 @@ import mindustry.gen.*;
import static mindustry.Vars.net;
public class Weapon{
/** displayed weapon region */
public String name;
/** minimum cursor distance from player, fixes 'cross-eyed' shooting. */
protected static float minPlayerDist = 20f;
//temporary only
protected static int sequenceNum = 0;
/** bullet shot */
public @NonNull BulletType bullet;
/** shell ejection effect */
@@ -39,6 +34,8 @@ public class Weapon{
public boolean alternate = false;
/** whether to rotate toward the target independently of unit */
public boolean rotate = false;
/** rotation speed of weapon when rotation is enabled, in degrees/t*/
public float rotateSpeed = 2f;
/** weapon reload in frames */
public float reload;
/** amount of shots per fire */
@@ -51,10 +48,10 @@ public class Weapon{
public float shake = 0f;
/** visual weapon knockback. */
public float recoil = 1.5f;
/** shoot barrel y offset */
public float length = 3f;
/** shoot barrel x offset. */
public float width = 4f;
/** projectile/effect offsets from center of weapon */
public float shootX = 0f, shootY = 3f;
/** offsets of weapon position on unit */
public float x = 5f, y = 0f;
/** fraction of velocity that is random */
public float velocityRnd = 0f;
/** randomization of shot length */
@@ -70,13 +67,16 @@ public class Weapon{
/** displayed region (autoloaded) */
public TextureRegion region;
protected Weapon(String name){
public Weapon(String name){
this.name = name;
}
public Weapon(){
//no region
this.name = "";
this("");
}
public void load(){
region = Core.atlas.find(name + "-equip", Core.atlas.find(name, Core.atlas.find("clear")));
}
}

View File

@@ -352,7 +352,7 @@ public class HudFragment extends Fragment{
}
@Remote(targets = Loc.both, called = Loc.server)
public static void spawnUnitEditor(Player player, UnitType type){
public static void spawnUnitEditor(Player player, UnitDef type){
if(state.isEditor()){
BaseUnit unit = type.create(player.getTeam());
unit.set(player.x, player.y);

View File

@@ -25,6 +25,8 @@ import mindustry.world.Block;
import mindustry.world.Tile;
import mindustry.world.meta.*;
import java.io.*;
import static mindustry.Vars.tilesize;
public abstract class Turret extends Block{
@@ -320,5 +322,26 @@ public abstract class Turret extends Block{
public float heat;
public int shots;
public TargetTrait target;
@Override
public void write(DataOutput stream) throws IOException{
super.write(stream);
stream.writeFloat(reload);
stream.writeFloat(rotation);
}
@Override
public void read(DataInput stream, byte revision) throws IOException{
super.read(stream, revision);
if(revision == 1){
reload = stream.readFloat();
rotation = stream.readFloat();
}
}
@Override
public byte version(){
return 1;
}
}
}