This repository has been archived by the owner on Sep 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More abstraction, implement flags, implement zone types
- Loading branch information
Showing
18 changed files
with
317 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Safezone.Model.Flag | ||
{ | ||
public class EnterVehiclesFlag : Flag | ||
{ | ||
public EnterVehiclesFlag() : base("EnterVehicles", true) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Xml.Serialization; | ||
|
||
namespace Safezone.Model.Flag | ||
{ | ||
[Serializable] | ||
[XmlType(AnonymousType = false)] | ||
[XmlInclude(typeof(PickupAllowedFlag))] | ||
[XmlInclude(typeof(GodmodeFlag))] | ||
[XmlInclude(typeof(NoZombieFlag))] | ||
[XmlInclude(typeof(EnterVehiclesFlag))] | ||
|
||
//for now this does not support 3rd party flags | ||
public abstract class Flag | ||
{ | ||
public String Name; | ||
public Object Value; | ||
public T GetValue<T>() | ||
{ | ||
if (!(Value is T)) | ||
{ | ||
throw new InvalidOperationException("Can't cast " + Value.GetType().Name + " to " + typeof(T).Name); | ||
} | ||
return (T) Value; | ||
} | ||
|
||
public T GetDefaultValue<T>() | ||
{ | ||
if (!(_defaultValue is T)) | ||
{ | ||
throw new InvalidOperationException("Can't cast " + Value.GetType().Name + " to " + typeof(T).Name); | ||
} | ||
return (T)_defaultValue; | ||
} | ||
|
||
private readonly Object _defaultValue; | ||
protected Flag(String name, Object defaultValue) | ||
{ | ||
Name = name; | ||
_defaultValue = defaultValue; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Safezone.Model.Flag | ||
{ | ||
public class GodmodeFlag : Flag | ||
{ | ||
public GodmodeFlag() : base("Godmode", true) | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Safezone.Model.Flag | ||
{ | ||
public class NoZombieFlag : Flag | ||
{ | ||
public NoZombieFlag() : base("NoZombie", true) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Xml.Serialization; | ||
|
||
namespace Safezone.Model.Flag | ||
{ | ||
public class PickupAllowedFlag : Flag | ||
{ | ||
public PickupAllowedFlag() : base("PickupAllowed", true) | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Xml.Serialization; | ||
|
||
namespace Safezone.Model | ||
{ | ||
[Serializable] | ||
[XmlType(AnonymousType = true)] | ||
public class Position | ||
{ | ||
[XmlAttribute("x")] | ||
public float X; | ||
[XmlAttribute("y")] | ||
public float Y; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections; | ||
using Rocket.Unturned; | ||
using Rocket.Unturned.Player; | ||
|
||
namespace Safezone.Model | ||
{ | ||
public class RectangleType : SafeZoneType | ||
{ | ||
public Position Position1; | ||
public Position Position2; | ||
|
||
public override string GetName() | ||
{ | ||
return "Rectangle"; | ||
} | ||
|
||
public override SafeZone Create(RocketPlayer player, String name, ArrayList args) | ||
{ | ||
if (!SafeZonePlugin.Instance.HasPositionSet(player)) | ||
{ | ||
RocketChat.Say(player.CSteamID, "Please set pos1 (/spos1) and pos2 (/spos2) before using this command", UnityEngine.Color.red); | ||
return null; | ||
} | ||
|
||
Position1 = SafeZonePlugin.Instance.GetPosition1(player); | ||
Position2 = SafeZonePlugin.Instance.GetPosition2(player); | ||
SafeZone zone = new SafeZone | ||
{ | ||
Name = name, | ||
Owner = SafeZonePlugin.GetId(player), | ||
Type = this | ||
}; | ||
|
||
return zone; | ||
} | ||
|
||
public override bool IsInSafeZone(Position p) | ||
{ | ||
Position p1 = Position1; | ||
Position p2 = Position2; | ||
|
||
//float x2 = p1.X; | ||
//float y2 = p4.X; | ||
//float x3 = p4.X; | ||
//float y3 = p1.Y; | ||
|
||
//Position p2 = new Position() {X = x2, Y = y2}; | ||
//Position p3 = new Position() {X = x3, Y = y3 }; | ||
|
||
bool b1 = p.X >= Math.Min(p1.X, p2.X); | ||
bool b2 = p.X <= Math.Max(p1.X, p2.X); | ||
bool b3 = p.Y >= Math.Min(p1.Y, p2.Y); | ||
bool b4 = p.Y <= Math.Max(p1.Y, p2.Y); | ||
|
||
return b1 && b2 && b3 && b4; | ||
} | ||
|
||
public override bool Redefine(RocketPlayer player, ArrayList args) | ||
{ | ||
if (!SafeZonePlugin.Instance.HasPositionSet(player)) | ||
{ | ||
RocketChat.Say(player.CSteamID, "Please set pos1 (/spos1) and pos2 (/spos2) before using this command", UnityEngine.Color.red); | ||
return false; | ||
} | ||
|
||
Position1 = SafeZonePlugin.Instance.GetPosition1(player); | ||
Position2 = SafeZonePlugin.Instance.GetPosition2(player); | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Xml.Serialization; | ||
|
||
namespace Safezone.Model | ||
{ | ||
public class SafeZone | ||
{ | ||
[XmlAttribute("Name")] | ||
public string Name; | ||
[XmlAttribute("Owner")] | ||
public uint Owner; | ||
[XmlElement("Type")] | ||
public SafeZoneType Type; | ||
|
||
[XmlArray("Flags")] | ||
public List<Flag.Flag> Flags; | ||
|
||
public Flag.Flag GetFlag(Type t, bool createIfNotFound = true) | ||
{ | ||
if (t != typeof(Flag.Flag)) | ||
{ | ||
throw new ArgumentException("Can't get " + t.Name + " as flag!"); | ||
} | ||
|
||
foreach (Flag.Flag flag in Flags.Where(flag => flag.GetType().FullName == t.FullName)) | ||
{ | ||
return flag; | ||
} | ||
|
||
if (!createIfNotFound) return null; | ||
|
||
return (Flag.Flag)Activator.CreateInstance(t); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Xml.Serialization; | ||
using Rocket.Unturned.Player; | ||
|
||
namespace Safezone.Model | ||
{ | ||
[XmlInclude(typeof(RectangleType))] | ||
public abstract class SafeZoneType | ||
{ | ||
public SafeZoneType() | ||
{ | ||
// dummy | ||
} | ||
|
||
private static readonly Dictionary<String, Type> RegistereTypes = new Dictionary<String, Type>(); | ||
|
||
public static SafeZoneType CreateSafeZoneType(String name) | ||
{ | ||
if (!RegistereTypes.ContainsKey(name)) | ||
{ | ||
return null; | ||
} | ||
Type t = RegistereTypes[name]; | ||
return (SafeZoneType)Activator.CreateInstance(t); | ||
} | ||
|
||
public static void RegisterSafeZoneType(String name, Type t) | ||
{ | ||
if(t != typeof(SafeZoneType)) | ||
{ | ||
throw new ArgumentException(t.Name + " is not a SafeZoneType!"); | ||
} | ||
|
||
if (RegistereTypes.ContainsKey(name)) | ||
{ | ||
throw new ArgumentException(name + " is already registered!"); | ||
} | ||
|
||
RegistereTypes.Add(name, t); | ||
} | ||
|
||
public abstract String GetName(); | ||
public abstract SafeZone Create(RocketPlayer player, String name, ArrayList args); | ||
public abstract bool IsInSafeZone(Position p); | ||
public abstract bool Redefine(RocketPlayer player, ArrayList args); | ||
} | ||
} |
Oops, something went wrong.