Skip to content

Commit

Permalink
Generic behavior/sensor implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vanjac committed Nov 10, 2023
1 parent 0d988b3 commit c7f1566
Show file tree
Hide file tree
Showing 26 changed files with 50 additions and 197 deletions.
24 changes: 24 additions & 0 deletions Assets/Base/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,18 @@ public virtual ICollection<Property> DeprecatedProperties()
public abstract Behaviour MakeComponent(GameObject gameObject);
}

public abstract class GenericEntityBehavior<SelfType, ComponentType> : EntityBehavior
where SelfType : GenericEntityBehavior<SelfType, ComponentType>
where ComponentType : BehaviorComponent<SelfType>
{
public override Behaviour MakeComponent(GameObject gameObject)
{
var component = gameObject.AddComponent<ComponentType>();
component.Init((SelfType)this);
return component;
}
}


public abstract class BehaviorComponent<T> : MonoBehaviour
{
Expand Down Expand Up @@ -687,6 +699,18 @@ public virtual ICollection<Property> DeprecatedProperties()
public abstract ISensorComponent MakeComponent(GameObject gameObject);
}

public abstract class GenericSensor<SelfType, ComponentType> : Sensor
where SelfType : GenericSensor<SelfType, ComponentType>
where ComponentType : SensorComponent<SelfType>
{
public override ISensorComponent MakeComponent(GameObject gameObject)
{
var component = gameObject.AddComponent<ComponentType>();
component.Init((SelfType)this);
return component;
}
}

public interface ISensorComponent
{
bool IsOn();
Expand Down
11 changes: 2 additions & 9 deletions Assets/Behaviors/Carryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class CarryableBehavior : EntityBehavior
public class CarryableBehavior : GenericEntityBehavior<CarryableBehavior, CarryableComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Carryable", "Allow player to pick up / drop / throw",
Expand All @@ -13,7 +13,7 @@ public class CarryableBehavior : EntityBehavior
BehaviorType.AndRule(
BehaviorType.BaseTypeRule(typeof(DynamicEntity)),
BehaviorType.NotBaseTypeRule(typeof(PlayerObject))));

public float throwSpeed = 0;
public float throwAngle = 25;

Expand All @@ -36,13 +36,6 @@ public override ICollection<Property> Properties()
PropertyGUIs.Float),
});
}

public override Behaviour MakeComponent(GameObject gameObject)
{
var component = gameObject.AddComponent<CarryableComponent>();
component.Init(this);
return component;
}
}


Expand Down
9 changes: 1 addition & 8 deletions Assets/Behaviors/Force.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class ForceBehavior : EntityBehavior
public class ForceBehavior : GenericEntityBehavior<ForceBehavior, ForceComponent>
{
public enum ForceBehaviorMode
{
Expand Down Expand Up @@ -55,13 +55,6 @@ public override ICollection<Property> Properties()
PropertyGUIs.Target)
});
}

public override Behaviour MakeComponent(GameObject gameObject)
{
var force = gameObject.AddComponent<ForceComponent>();
force.Init(this);
return force;
}
}

public class ForceComponent : BehaviorComponent<ForceBehavior>
Expand Down
9 changes: 1 addition & 8 deletions Assets/Behaviors/HaloComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnityEngine;

[EditorPreviewBehavior]
public class HaloBehavior : EntityBehavior
public class HaloBehavior : GenericEntityBehavior<HaloBehavior, HaloComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Halo", "Glowing effect",
Expand Down Expand Up @@ -31,13 +31,6 @@ public override ICollection<Property> Properties()
PropertyGUIs.Color)
});
}

public override Behaviour MakeComponent(GameObject gameObject)
{
var component = gameObject.AddComponent<HaloComponent>();
component.Init(this);
return component;
}
}

public class HaloComponent : BehaviorComponent<HaloBehavior>
Expand Down
9 changes: 1 addition & 8 deletions Assets/Behaviors/HurtHeal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class HurtHealBehavior : EntityBehavior
public class HurtHealBehavior : GenericEntityBehavior<HurtHealBehavior, HurtHealComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Hurt/Heal", "Lose/gain health; below 0, object dies",
Expand Down Expand Up @@ -53,13 +53,6 @@ public override ICollection<Property> DeprecatedProperties()
PropertyGUIs.Float)
});
}

public override Behaviour MakeComponent(GameObject gameObject)
{
HurtHealComponent component = gameObject.AddComponent<HurtHealComponent>();
component.Init(this);
return component;
}
}

public class HurtHealComponent : BehaviorComponent<HurtHealBehavior>
Expand Down
9 changes: 1 addition & 8 deletions Assets/Behaviors/LightComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnityEngine;

[EditorPreviewBehavior]
public class LightBehavior : EntityBehavior
public class LightBehavior : GenericEntityBehavior<LightBehavior, LightComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Light", "Light source at the center of object",
Expand Down Expand Up @@ -53,13 +53,6 @@ public override ICollection<Property> DeprecatedProperties()
PropertyGUIs.Toggle)
});
}

public override Behaviour MakeComponent(GameObject gameObject)
{
var light = gameObject.AddComponent<LightComponent>();
light.Init(this);
return light;
}
}

public class LightComponent : BehaviorComponent<LightBehavior>
Expand Down
9 changes: 1 addition & 8 deletions Assets/Behaviors/LookAt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class LookAtBehavior : EntityBehavior
public class LookAtBehavior : GenericEntityBehavior<LookAtBehavior, LookAtComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Look At", "Point in a direction or towards object",
Expand Down Expand Up @@ -46,13 +46,6 @@ public override ICollection<Property> Properties()
PropertyGUIs.DoubleToggle)
});
}

public override Behaviour MakeComponent(GameObject gameObject)
{
var component = gameObject.AddComponent<LookAtComponent>();
component.Init(this);
return component;
}
}

public class LookAtComponent : MotionComponent<LookAtBehavior>
Expand Down
9 changes: 1 addition & 8 deletions Assets/Behaviors/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class MoveBehavior : EntityBehavior
public class MoveBehavior : GenericEntityBehavior<MoveBehavior, MoveComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Move", "Move in a direction or toward object",
Expand Down Expand Up @@ -33,13 +33,6 @@ public override ICollection<Property> Properties()
PropertyGUIs.Target)
});
}

public override Behaviour MakeComponent(GameObject gameObject)
{
MoveComponent move = gameObject.AddComponent<MoveComponent>();
move.Init(this);
return move;
}
}

public class MoveComponent : MotionComponent<MoveBehavior>
Expand Down
9 changes: 1 addition & 8 deletions Assets/Behaviors/MoveWith.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class MoveWithBehavior : EntityBehavior
public class MoveWithBehavior : GenericEntityBehavior<MoveWithBehavior, MoveWithComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Move With", "Follow the motion of another object",
Expand Down Expand Up @@ -32,13 +32,6 @@ public override ICollection<Property> Properties()
PropertyGUIs.Toggle)
});
}

public override Behaviour MakeComponent(GameObject gameObject)
{
MoveWithComponent component = gameObject.AddComponent<MoveWithComponent>();
component.Init(this);
return component;
}
}

public class MoveWithComponent : MotionComponent<MoveWithBehavior>
Expand Down
9 changes: 1 addition & 8 deletions Assets/Behaviors/Reflector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using UnityEngine.Rendering;

[EditorPreviewBehavior]
public class ReflectorBehavior : EntityBehavior
public class ReflectorBehavior : GenericEntityBehavior<ReflectorBehavior, ReflectorComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Reflector", "Add more realistic reflections to area",
Expand Down Expand Up @@ -40,13 +40,6 @@ public override ICollection<Property> Properties()
PropertyGUIs.Toggle)
});
}

public override Behaviour MakeComponent(GameObject gameObject)
{
var component = gameObject.AddComponent<ReflectorComponent>();
component.Init(this);
return component;
}
}

public class ReflectorComponent : BehaviorComponent<ReflectorBehavior>
Expand Down
9 changes: 1 addition & 8 deletions Assets/Behaviors/Scale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class ScaleBehavior : EntityBehavior
public class ScaleBehavior : GenericEntityBehavior<ScaleBehavior, ScaleComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Scale", "Change size along each axis",
Expand Down Expand Up @@ -30,13 +30,6 @@ public override ICollection<Property> Properties()
PropertyGUIs.Vector3),
});
}

public override Behaviour MakeComponent(GameObject gameObject)
{
var component = gameObject.AddComponent<ScaleComponent>();
component.Init(this);
return component;
}
}

public class ScaleComponent : BehaviorComponent<ScaleBehavior>
Expand Down
9 changes: 1 addition & 8 deletions Assets/Behaviors/Score.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class ScoreBehavior : EntityBehavior
public class ScoreBehavior : GenericEntityBehavior<ScoreBehavior, ScoreComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Score", "Add or subtract from player's score",
Expand All @@ -25,13 +25,6 @@ public override ICollection<Property> Properties()
PropertyGUIs.Int)
});
}

public override Behaviour MakeComponent(GameObject gameObject)
{
var component = gameObject.AddComponent<ScoreComponent>();
component.Init(this);
return component;
}
}

public class ScoreComponent : BehaviorComponent<ScoreBehavior>
Expand Down
7 changes: 1 addition & 6 deletions Assets/Behaviors/Solid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class SolidBehavior : EntityBehavior
public class SolidBehavior : GenericEntityBehavior<SolidBehavior, SolidComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Solid", "Block and collide with other objects",
Expand All @@ -15,11 +15,6 @@ public override BehaviorType BehaviorObjectType()
{
return objectType;
}

public override Behaviour MakeComponent(GameObject gameObject)
{
return gameObject.AddComponent<SolidComponent>();
}
}

public class SolidComponent : BehaviorComponent<SolidBehavior>
Expand Down
9 changes: 1 addition & 8 deletions Assets/Behaviors/Spin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class SpinBehavior : EntityBehavior
public class SpinBehavior : GenericEntityBehavior<SpinBehavior, SpinComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Spin", "Rotate continuously",
Expand Down Expand Up @@ -33,13 +33,6 @@ public override ICollection<Property> Properties()
PropertyGUIs.TargetStatic)
});
}

public override Behaviour MakeComponent(GameObject gameObject)
{
SpinComponent spin = gameObject.AddComponent<SpinComponent>();
spin.Init(this);
return spin;
}
}

public class SpinComponent : MotionComponent<SpinBehavior>
Expand Down
9 changes: 1 addition & 8 deletions Assets/Behaviors/Teleport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class TeleportBehavior : EntityBehavior
public class TeleportBehavior : GenericEntityBehavior<TeleportBehavior, TeleportComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Teleport", "Instantly teleport to another location",
Expand Down Expand Up @@ -46,13 +46,6 @@ public override ICollection<Property> Properties()
})
});
}

public override Behaviour MakeComponent(GameObject gameObject)
{
TeleportComponent component = gameObject.AddComponent<TeleportComponent>();
component.Init(this);
return component;
}
}

public class TeleportComponent : BehaviorComponent<TeleportBehavior>
Expand Down
7 changes: 1 addition & 6 deletions Assets/Behaviors/Visible.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class VisibleBehavior : EntityBehavior
public class VisibleBehavior : GenericEntityBehavior<VisibleBehavior, VisibleComponent>
{
public static new BehaviorType objectType = new BehaviorType(
"Visible", "Object is visible in the game",
Expand All @@ -15,11 +15,6 @@ public override BehaviorType BehaviorObjectType()
{
return objectType;
}

public override Behaviour MakeComponent(GameObject gameObject)
{
return gameObject.AddComponent<VisibleComponent>();
}
}

public class VisibleComponent : BehaviorComponent<VisibleBehavior>
Expand Down
Loading

0 comments on commit c7f1566

Please sign in to comment.