Skip to content

Commit

Permalink
Unify gun and missile code a bit more
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Oct 28, 2020
1 parent 71a4498 commit 17674ac
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
20 changes: 10 additions & 10 deletions MCGalaxy/Commands/Fun/CmdGun.cs
Expand Up @@ -32,18 +32,18 @@ public sealed class CmdGun : Command2 {
if (p.weapon != null && message.Length == 0) {
p.weapon.Disable(); return;
}

Gun gun = GetGun(p, message);
if (gun == null) { Help(p); return; }
gun.Enable(p);
WeaponType type = Weapon.ParseType(message);
if (type == WeaponType.Invalid) { Help(p); return; }
GetGun(type).Enable(p);
}

static Gun GetGun(Player p, string mode) {
if (mode.Length == 0) return new Gun();
if (mode.CaselessEq("destroy")) return new PenetrativeGun();
if (mode.CaselessEq("tp") || mode.CaselessEq("teleport")) return new TeleportGun();
if (mode.CaselessEq("explode")) return new ExplosiveGun();
if (mode.CaselessEq("laser")) return new LaserGun();
static Gun GetGun(WeaponType type) {
if (type == WeaponType.Normal) return new Gun();
if (type == WeaponType.Destroy) return new PenetrativeGun();
if (type == WeaponType.Teleport) return new TeleportGun();
if (type == WeaponType.Explode) return new ExplosiveGun();
if (type == WeaponType.Laser) return new LaserGun();
return null;
}

Expand Down
15 changes: 3 additions & 12 deletions MCGalaxy/Commands/Fun/CmdMissile.cs
Expand Up @@ -33,23 +33,14 @@ public sealed class CmdMissile : Command2 {
p.weapon.Disable(); return;
}

WeaponType weaponType = GetWeaponType(p, message);
if (weaponType == WeaponType.Invalid) { Help(p); return; }
WeaponType type = Weapon.ParseType(message);
if (type == WeaponType.Invalid) { Help(p); return; }

Missile missile = new Missile();
missile.type = weaponType;
missile.type = type;
missile.Enable(p);
}

WeaponType GetWeaponType(Player p, string mode) {
if (mode.Length == 0) return WeaponType.Normal;
if (mode.CaselessEq("destroy")) return WeaponType.Destroy;
if (mode.CaselessEq("tp") || mode.CaselessEq("teleport")) return WeaponType.Teleport;
if (mode.CaselessEq("explode")) return WeaponType.Explode;
if (mode.CaselessEq("laser")) return WeaponType.Laser;
return WeaponType.Invalid;
}

public override void Help(Player p) {
p.Message("%T/Missile [at end]");
p.Message("%HAllows you to fire missiles at people. Differs from %T/gun %Hin that the missile is guided.");
Expand Down
6 changes: 3 additions & 3 deletions MCGalaxy/Games/Weapons/Guns.cs
Expand Up @@ -37,12 +37,12 @@ public class Gun : Weapon {
AmmunitionData args = new AmmunitionData();
args.block = block;

args.start = (Vec3U16)p.Pos.BlockCoords;
args.dir = dir;
args.start = (Vec3U16)p.Pos.BlockCoords;
args.dir = dir;
args.iterations = 4;
return args;
}

protected virtual bool OnHitBlock(AmmunitionData args, Vec3U16 pos, BlockID block) {
return true;
}
Expand Down
17 changes: 14 additions & 3 deletions MCGalaxy/Games/Weapons/Weapon.cs
Expand Up @@ -26,7 +26,6 @@
namespace MCGalaxy.Games {

/// <summary> Represents a weapon which can interact with blocks or players until it dies. </summary>
/// <remarks> Activated by clicking through either PlayerClick or on a glass box around the player. </remarks>
public abstract class Weapon {

public abstract string Name { get; }
Expand All @@ -35,6 +34,7 @@ public abstract class Weapon {
protected Player p;
AimBox aimer;

/// <summary> Applies this weapon to the given player, and sets up necessary state. </summary>
public virtual void Enable(Player p) {
if (!hookedEvents) {
OnPlayerClickEvent.Register(PlayerClickCallback, Priority.Low);
Expand Down Expand Up @@ -62,18 +62,20 @@ public abstract class Weapon {
p.weapon = null;
}

/// <summary> Called when the player fires this weapon. </summary>
/// <remarks> Activated by clicking through either PlayerClick or on a glass box around the player. </remarks>
protected abstract void OnActivated(Vec3F32 dir, BlockID block);


static void BlockChangingCallback(Player p, ushort x, ushort y, ushort z, BlockID block, bool placing, ref bool cancel) {
Weapon weapon = p.weapon;
if (weapon == null) return;

// revert block back since client assumes changes always succeeds
// Revert block back since client assumes changes always succeeds
p.RevertBlock(x, y, z);
cancel = true;

// defer to player click handler if used
// Defer to player click handler if PlayerClick supported
if (weapon.aimer == null) return;

if (!p.level.Config.Guns) { weapon.Disable(); return; }
Expand Down Expand Up @@ -114,6 +116,15 @@ public abstract class Weapon {
}
return null;
}

public static WeaponType ParseType(string type) {
if (type.Length == 0) return WeaponType.Normal;
if (type.CaselessEq("destroy")) return WeaponType.Destroy;
if (type.CaselessEq("tp") || type.CaselessEq("teleport")) return WeaponType.Teleport;
if (type.CaselessEq("explode")) return WeaponType.Explode;
if (type.CaselessEq("laser")) return WeaponType.Laser;
return WeaponType.Invalid;
}
}

public enum WeaponType { Invalid, Normal, Destroy, Teleport, Explode, Laser };
Expand Down

0 comments on commit 17674ac

Please sign in to comment.