Skip to content

Commit

Permalink
Use object initializers for PropertiesObjectType
Browse files Browse the repository at this point in the history
  • Loading branch information
vanjac committed Jan 15, 2024
1 parent 04ad9fb commit 15e7c59
Show file tree
Hide file tree
Showing 41 changed files with 324 additions and 168 deletions.
6 changes: 5 additions & 1 deletion Assets/Base/CustomTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
public class CustomTexture : PropertiesObject
{
public static PropertiesObjectType objectType = new PropertiesObjectType(
"Custom Texture", s => s.CustomTextureDesc, "image", typeof(CustomTexture));
"Custom Texture", typeof(CustomTexture))
{
description = s => s.CustomTextureDesc,
iconName = "image",
};
public PropertiesObjectType ObjectType => objectType;

public enum CustomFilter
Expand Down
82 changes: 25 additions & 57 deletions Assets/Base/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,24 @@ public static IEnumerable<Property> JoinProperties(
// with all the missing data (this is done by Filter)
public class PropertiesObjectType
{
public static readonly PropertiesObjectType NONE =
new PropertiesObjectType("None", GUIStringSet.Empty, "cancel", null);
public static readonly PropertiesObjectType NONE = new PropertiesObjectType("None", null)
{ iconName = "cancel" };

public string fullName; // not readonly so it can be serialized
// This name is used for identifying types and for error messages if a type is not recognized.
// It is always in English (not localized) and can never change.
public string fullName;

[XmlIgnore]
public readonly Type type;
[XmlIgnore]
public readonly Localizer description;
public Func<PropertiesObject> constructor;

[XmlIgnore]
public readonly Localizer longDescription;
public Localizer description = GUIStringSet.Empty;
[XmlIgnore]
public readonly string iconName;
public Localizer longDescription = GUIStringSet.Empty;
[XmlIgnore]
public readonly Type type;
private readonly Func<PropertiesObject> constructor;
public string iconName = "";

private Texture _icon;
public Texture icon
Expand All @@ -73,47 +78,25 @@ public Texture icon
}

// empty constructor for deserialization
public PropertiesObjectType() { }

public PropertiesObjectType(string fullName, Type type)
public PropertiesObjectType()
{
this.fullName = fullName;
description = GUIStringSet.Empty;
longDescription = GUIStringSet.Empty;
iconName = "";
this.type = type;
constructor = DefaultConstructor;
}

public PropertiesObjectType(string fullName, Localizer description, string iconName,
Type type, Func<PropertiesObject> constructor = null)
{
this.fullName = fullName;
this.description = description;
longDescription = GUIStringSet.Empty;
this.iconName = iconName;
this.type = type;
this.constructor = constructor ?? DefaultConstructor;
}

public PropertiesObjectType(string fullName, Localizer description, Localizer longDescription,
string iconName, Type type, Func<PropertiesObject> constructor = null)
public PropertiesObjectType(string fullName, Type type)
{
this.fullName = fullName;
this.description = description;
this.longDescription = longDescription;
this.iconName = iconName;
this.type = type;
this.constructor = constructor ?? DefaultConstructor;
constructor = DefaultConstructor;
}

public PropertiesObjectType(PropertiesObjectType baseType, Func<PropertiesObject> newConstructor)
{
this.fullName = baseType.fullName;
this.description = baseType.description;
this.longDescription = baseType.longDescription;
this.iconName = baseType.iconName;
this.type = baseType.type;
fullName = baseType.fullName;
description = baseType.description;
longDescription = baseType.longDescription;
iconName = baseType.iconName;
type = baseType.type;
constructor = newConstructor;
}

Expand Down Expand Up @@ -190,7 +173,8 @@ public interface PropertiesObject
public abstract class Entity : PropertiesObject
{
public static PropertiesObjectType objectType = new PropertiesObjectType(
"Anything", GUIStringSet.Empty, "circle-outline", typeof(Entity));
"Anything", typeof(Entity))
{ iconName = "circle-outline" };

public EntityComponent component;
public Sensor sensor;
Expand Down Expand Up @@ -562,27 +546,11 @@ public virtual void LastBehaviorDisabled() { }

public class BehaviorType : PropertiesObjectType
{
public readonly Predicate<Entity> rule;
public Predicate<Entity> rule = DefaultRule;

public BehaviorType(string fullName, Type type)
: base(fullName, type)
{
this.rule = DefaultRule;
}

public BehaviorType(string fullName, Localizer description, string iconName, Type type,
Predicate<Entity> rule = null)
: base(fullName, description, iconName, type)
{
this.rule = rule ?? DefaultRule;
}

public BehaviorType(string fullName, Localizer description, Localizer longDescription,
string iconName, Type type, Predicate<Entity> rule = null)
: base(fullName, description, longDescription, iconName, type)
{
this.rule = rule ?? DefaultRule;
}
{ }

private static bool DefaultRule(Entity checkEntity) => true;

Expand Down
6 changes: 5 additions & 1 deletion Assets/Base/Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
public abstract class ObjectEntity : DynamicEntity
{
public static new PropertiesObjectType objectType = new PropertiesObjectType(
"Object", s => s.ObjectDesc, "circle", typeof(ObjectEntity));
"Object", typeof(ObjectEntity))
{
description = s => s.ObjectDesc,
iconName = "circle",
};
public override PropertiesObjectType ObjectType => objectType;

public ObjectMarker marker;
Expand Down
8 changes: 6 additions & 2 deletions Assets/Base/Substance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ public enum Pos { Min, Center, Max };
public class Substance : DynamicEntity
{
public static new PropertiesObjectType objectType = new PropertiesObjectType(
"Substance", s => s.SubstanceDesc, s => s.SubstanceLongDesc, "cube-outline",
typeof(Substance));
"Substance", typeof(Substance))
{
description = s => s.SubstanceDesc,
longDescription = s => s.SubstanceLongDesc,
iconName = "cube-outline",
};
public override PropertiesObjectType ObjectType => objectType;

public Pivot pivot = new Pivot { x = Pivot.Pos.Center, y = Pivot.Pos.Center, z = Pivot.Pos.Center };
Expand Down
6 changes: 5 additions & 1 deletion Assets/Base/WorldProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ public class WorldProperties : PropertiesObject
};

public static PropertiesObjectType objectType = new PropertiesObjectType(
"World", s => s.WorldDesc, "earth", typeof(WorldProperties));
"World", typeof(WorldProperties))
{
description = s => s.WorldDesc,
iconName = "earth",
};
public PropertiesObjectType ObjectType => objectType;

public void SetSky(Material sky)
Expand Down
12 changes: 8 additions & 4 deletions Assets/Behaviors/Carryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
public class CarryableBehavior : GenericEntityBehavior<CarryableBehavior, CarryableComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Carryable", s => s.CarryableDesc, s => s.CarryableLongDesc, "coffee",
typeof(CarryableBehavior),
BehaviorType.AndRule(
"Carryable", typeof(CarryableBehavior))
{
description = s => s.CarryableDesc,
longDescription = s => s.CarryableLongDesc,
iconName = "coffee",
rule = BehaviorType.AndRule(
BehaviorType.BaseTypeRule(typeof(DynamicEntity)),
BehaviorType.NotBaseTypeRule(typeof(PlayerObject))));
BehaviorType.NotBaseTypeRule(typeof(PlayerObject))),
};
public override BehaviorType BehaviorObjectType => objectType;

public float throwSpeed = 0;
Expand Down
12 changes: 8 additions & 4 deletions Assets/Behaviors/CharacterComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
public class CharacterBehavior : BasePhysicsBehavior
{
public static new BehaviorType objectType = new BehaviorType(
"Character", s => s.CharacterDesc, s => s.CharacterLongDesc, "human",
typeof(CharacterBehavior),
BehaviorType.AndRule(
"Character", typeof(CharacterBehavior))
{
description = s => s.CharacterDesc,
longDescription = s => s.CharacterLongDesc,
iconName = "human",
rule = BehaviorType.AndRule(
BehaviorType.BaseTypeRule(typeof(DynamicEntity)),
BehaviorType.NotBaseTypeRule(typeof(PlayerObject))));
BehaviorType.NotBaseTypeRule(typeof(PlayerObject))),
};
public override BehaviorType BehaviorObjectType => objectType;

public override IEnumerable<Property> Properties() =>
Expand Down
12 changes: 8 additions & 4 deletions Assets/Behaviors/Clone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

public class CloneBehavior : TeleportBehavior
{
public static new BehaviorType objectType = new BehaviorType(
"Clone", s => s.CloneDesc, s => s.CloneLongDesc, "content-copy", typeof(CloneBehavior),
BehaviorType.AndRule(
public static new BehaviorType objectType = new BehaviorType("Clone", typeof(CloneBehavior))
{
description = s => s.CloneDesc,
longDescription = s => s.CloneLongDesc,
iconName = "content-copy",
rule = BehaviorType.AndRule(
BehaviorType.BaseTypeRule(typeof(DynamicEntity)),
BehaviorType.NotBaseTypeRule(typeof(PlayerObject))));
BehaviorType.NotBaseTypeRule(typeof(PlayerObject))),
};
public override BehaviorType BehaviorObjectType => objectType;

public override Behaviour MakeComponent(GameObject gameObject)
Expand Down
10 changes: 7 additions & 3 deletions Assets/Behaviors/Force.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ public enum ForceBehaviorMode
IMPULSE, CONTINUOUS
}

public static new BehaviorType objectType = new BehaviorType(
"Force", s => s.ForceDesc, s => s.ForceLongDesc, "rocket", typeof(ForceBehavior),
BehaviorType.BaseTypeRule(typeof(DynamicEntity)));
public static new BehaviorType objectType = new BehaviorType("Force", typeof(ForceBehavior))
{
description = s => s.ForceDesc,
longDescription = s => s.ForceLongDesc,
iconName = "rocket",
rule = BehaviorType.BaseTypeRule(typeof(DynamicEntity)),
};
public override BehaviorType BehaviorObjectType => objectType;

public ForceBehaviorMode mode = ForceBehaviorMode.CONTINUOUS;
Expand Down
8 changes: 6 additions & 2 deletions Assets/Behaviors/HaloComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
[EditorPreviewBehavior]
public class HaloBehavior : GenericEntityBehavior<HaloBehavior, HaloComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Halo", s => s.HaloDesc, s => s.HaloLongDesc, "blur", typeof(HaloBehavior));
public static new BehaviorType objectType = new BehaviorType("Halo", typeof(HaloBehavior))
{
description = s => s.HaloDesc,
longDescription = s => s.HaloLongDesc,
iconName = "blur",
};
public override BehaviorType BehaviorObjectType => objectType;

public float size = 3;
Expand Down
9 changes: 7 additions & 2 deletions Assets/Behaviors/HurtHeal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
public class HurtHealBehavior : GenericEntityBehavior<HurtHealBehavior, HurtHealComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Hurt/Heal", s => s.HurtHealDesc, s => s.HurtHealLongDesc, "heart",
typeof(HurtHealBehavior), BehaviorType.BaseTypeRule(typeof(DynamicEntity)));
"Hurt/Heal", typeof(HurtHealBehavior))
{
description = s => s.HurtHealDesc,
longDescription = s => s.HurtHealLongDesc,
iconName = "heart",
rule = BehaviorType.BaseTypeRule(typeof(DynamicEntity)),
};
public override BehaviorType BehaviorObjectType => objectType;

public float amount = -30;
Expand Down
10 changes: 7 additions & 3 deletions Assets/Behaviors/Joystick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ public class JoystickBehavior
public enum JoystickAlignment { HORIZONTAL, VERTICAL }

public static new BehaviorType objectType = new BehaviorType(
"Joystick", s => s.JoystickDesc, "joystick", typeof(JoystickBehavior),
BehaviorType.AndRule(
"Joystick", typeof(JoystickBehavior))
{
description = s => s.JoystickDesc,
iconName = "joystick",
rule = BehaviorType.AndRule(
BehaviorType.BaseTypeRule(typeof(DynamicEntity)),
BehaviorType.NotBaseTypeRule(typeof(PlayerObject))));
BehaviorType.NotBaseTypeRule(typeof(PlayerObject))),
};
public override BehaviorType BehaviorObjectType => objectType;

public float speed = 1;
Expand Down
8 changes: 6 additions & 2 deletions Assets/Behaviors/LightComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
[EditorPreviewBehavior]
public class LightBehavior : GenericEntityBehavior<LightBehavior, LightComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Light", s => s.LightDesc, s => s.LightLongDesc, "lightbulb-on", typeof(LightBehavior));
public static new BehaviorType objectType = new BehaviorType("Light", typeof(LightBehavior))
{
description = s => s.LightDesc,
longDescription = s => s.LightLongDesc,
iconName = "lightbulb-on",
};
public override BehaviorType BehaviorObjectType => objectType;

public float size = 10, intensity = 1;
Expand Down
10 changes: 7 additions & 3 deletions Assets/Behaviors/LookAt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

public class LookAtBehavior : GenericEntityBehavior<LookAtBehavior, LookAtComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Look At", s => s.LookAtDesc, s => s.LookAtLongDesc, "compass", typeof(LookAtBehavior),
BehaviorType.BaseTypeRule(typeof(DynamicEntity)));
public static new BehaviorType objectType = new BehaviorType("Look At", typeof(LookAtBehavior))
{
description = s => s.LookAtDesc,
longDescription = s => s.LookAtLongDesc,
iconName = "compass",
rule = BehaviorType.BaseTypeRule(typeof(DynamicEntity)),
};
public override BehaviorType BehaviorObjectType => objectType;

public Target target = new Target(Target.EAST);
Expand Down
9 changes: 7 additions & 2 deletions Assets/Behaviors/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
public class MoveBehavior : GenericEntityBehavior<MoveBehavior, MoveComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Move", s => s.MoveDesc, s => s.MoveLongDesc, "arrow-right-bold-box-outline",
typeof(MoveBehavior), BehaviorType.BaseTypeRule(typeof(DynamicEntity)));
"Move", typeof(MoveBehavior))
{
description = s => s.MoveDesc,
longDescription = s => s.MoveLongDesc,
iconName = "arrow-right-bold-box-outline",
rule = BehaviorType.BaseTypeRule(typeof(DynamicEntity)),
};
public override BehaviorType BehaviorObjectType => objectType;

public Target target = new Target(Target.NORTH);
Expand Down
10 changes: 7 additions & 3 deletions Assets/Behaviors/MoveWith.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
public class MoveWithBehavior : GenericEntityBehavior<MoveWithBehavior, MoveWithComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Move With", s => s.MoveWithDesc, s => s.MoveWithLongDesc, "move-resize-variant",
typeof(MoveWithBehavior),
BehaviorType.BaseTypeRule(typeof(DynamicEntity)));
"Move With", typeof(MoveWithBehavior))
{
description = s => s.MoveWithDesc,
longDescription = s => s.MoveWithLongDesc,
iconName = "move-resize-variant",
rule = BehaviorType.BaseTypeRule(typeof(DynamicEntity)),
};
public override BehaviorType BehaviorObjectType => objectType;

public EntityReference target = new EntityReference(null);
Expand Down
11 changes: 8 additions & 3 deletions Assets/Behaviors/PhysicsComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ public abstract class BasePhysicsBehavior : EntityBehavior
public class PhysicsBehavior : BasePhysicsBehavior
{
public static new BehaviorType objectType = new BehaviorType(
"Physics", s => s.PhysicsDesc, s => s.PhysicsLongDesc, "soccer", typeof(PhysicsBehavior),
BehaviorType.AndRule(
"Physics", typeof(PhysicsBehavior))
{
description = s => s.PhysicsDesc,
longDescription = s => s.PhysicsLongDesc,
iconName = "soccer",
rule = BehaviorType.AndRule(
BehaviorType.BaseTypeRule(typeof(DynamicEntity)),
BehaviorType.NotBaseTypeRule(typeof(PlayerObject))));
BehaviorType.NotBaseTypeRule(typeof(PlayerObject))),
};
public override BehaviorType BehaviorObjectType => objectType;

public override IEnumerable<Property> Properties() =>
Expand Down
Loading

0 comments on commit 15e7c59

Please sign in to comment.