This repository was archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCollisionRule.cs
105 lines (93 loc) · 2.52 KB
/
CollisionRule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
namespace HatlessEngine
{
public sealed class CollisionRule
{
public bool Active = true;
internal CollisionRuleType Type;
internal CollisionAction Action;
internal Delegate Method;
internal bool DeactivateAfterCollision = false;
internal object Target;
internal bool FilterEnabled;
internal List<Type> ObjectmapFilter;
#region Constructors
public CollisionRule(Shape shape, CollisionAction action, Action<Shape> method = null)
{
Type = CollisionRuleType.Shape;
Target = shape;
Action = action;
Method = method;
}
public CollisionRule(PhysicalObject obj, CollisionAction action, Action<PhysicalObject> method = null)
{
Type = CollisionRuleType.Object;
Target = obj;
Action = action;
Method = method;
}
public CollisionRule(Type objType, CollisionAction action, Action<PhysicalObject> method = null)
{
//validate objType
if (!objType.IsSubclassOf(typeof(PhysicalObject)))
throw new InvalidObjectTypeException("objType (" + objType + ") is not a subclass of PhysicalObject");
Type = CollisionRuleType.ObjectType;
Target = objType;
Action = action;
Method = method;
}
public CollisionRule(GameObjectCollection objMap, CollisionAction action, Type[] objFilter = null, Action<PhysicalObject> method = null)
{
Type = CollisionRuleType.Objectmap;
Target = objMap;
Action = action;
Method = method;
if (objFilter != null)
{
//validate the filter
foreach (Type objType in objFilter)
{
if (!objType.IsSubclassOf(typeof (PhysicalObject)))
throw new InvalidObjectTypeException("objType in objFilter (" + objType +
") is not a subclass of PhysicalObject");
}
ObjectmapFilter = new List<Type>(objFilter);
FilterEnabled = true;
}
else
FilterEnabled = false;
}
#endregion
internal void CallMethod(object result)
{
if (Method != null)
Method.DynamicInvoke(result);
}
}
public enum CollisionRuleType
{
Shape = 0,
Object = 1,
ObjectType = 2,
Objectmap = 3,
Spritemap = 4,
//Shapemap = 6
}
public enum CollisionAction
{
/// <summary>
/// Do nothing. For triggering a method only.
/// </summary>
None = 0,
/// <summary>
/// Set object's speed to 0 at touching point.
/// </summary>
Block = 1,
/// <summary>
/// Bounce perfectly off the surface changing the SpeedDirection only.
/// </summary>
Bounce = 2,
//Slide = 4, keep distance left or only distance on unlocked axis left?
}
}