forked from ToadsworthLP/desktoptale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhysics.cs
81 lines (67 loc) · 2.54 KB
/
Physics.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
using System.Collections.Generic;
using Desktoptale.Messages;
using Desktoptale.Messaging;
using Microsoft.Xna.Framework;
namespace Desktoptale
{
public class Physics
{
public IPhysicsObject PhysicsObjectUnderCursor { get; private set; }
public bool HasColliderUnderCursorChanged { get; private set; }
private ISet<IPhysicsObject> colliders;
private InputManager inputManager;
private bool clickThrough;
public Physics(InputManager inputManager)
{
this.inputManager = inputManager;
colliders = new HashSet<IPhysicsObject>();
MessageBus.Subscribe<ClickThroughChangedMessage>(OnClickThroughChangedMessage);
}
public void Update()
{
IPhysicsObject physicsObject;
if (clickThrough && !inputManager.CtrlPressed)
{
physicsObject = null;
}
else
{
physicsObject = GetColliderAtPosition(inputManager.PointerPosition.ToVector2());
}
if (PhysicsObjectUnderCursor != physicsObject) HasColliderUnderCursorChanged = true;
PhysicsObjectUnderCursor = physicsObject;
if(inputManager.LeftClickJustPressed) PhysicsObjectUnderCursor?.OnLeftClicked();
if(inputManager.RightClickJustPressed) PhysicsObjectUnderCursor?.OnRightClicked();
if (inputManager.LeftClickJustPressed && PhysicsObjectUnderCursor == null)
{
MessageBus.Send(new UnfocusAllCharactersMessage());
}
}
public void AddCollider(IPhysicsObject physicsObject)
{
colliders.Add(physicsObject);
}
public bool RemoveCollider(IPhysicsObject physicsObject)
{
return colliders.Remove(physicsObject);
}
public IPhysicsObject GetColliderAtPosition(Vector2 position)
{
IPhysicsObject result = null;
float lowestZ = float.MaxValue;
foreach (IPhysicsObject collider in colliders)
{
if (collider.HitBox.Contains(position) && collider.Depth < lowestZ)
{
result = collider;
lowestZ = collider.Depth;
}
}
return result;
}
private void OnClickThroughChangedMessage(ClickThroughChangedMessage message)
{
clickThrough = message.Enabled;
}
}
}