-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameObject.cs
More file actions
63 lines (52 loc) · 1.72 KB
/
GameObject.cs
File metadata and controls
63 lines (52 loc) · 1.72 KB
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
namespace Storm;
using Components;
using Logging;
using Utils;
public class GameObject {
public string name = "";
public Transform transform;
private readonly List<Component> components = new();
public string[] tags;
public int updateIndex = 0;
public int renderIndex = 0;
public bool visible = true;
public GameObject(string name, Transform transform, string[] tags = null!, bool visible = true)
{
this.name = name;
AddComponent(transform);
this.transform = transform;
this.tags = tags ?? Array.Empty<string>();
this.visible = visible;
Game.gameObjectsUpdate.Add(this);
Game.gameObjectsDraw.Add(this);
Log.Info($"GameObject \" {name} \" was registered!");
}
public virtual void OnUpdate(double deltaTime) {}
public virtual void OnDraw(Graphics graphics) {}
public void UpdateComponents(double deltaTime)
{
foreach (Component comp in components)
if (comp.isActive) try { comp.OnUpdate(deltaTime); } catch {}
}
public void AddComponent(Component component)
{
component.BindObject(this);
components.Add(component);
}
public void RemoveComponent(Component component)
{
component.BindObject(null!);
components.Remove(component);
}
public override string ToString()
{
return $"GameObject : \" {name} \"";
}
public void LookAt(Vector2 target)
{
float dx = target.x - MathF.Cos(MathHelper.DegToRad(transform.rotation));
float dy = target.y - MathF.Sin(MathHelper.DegToRad(transform.rotation));
float angleRad = MathF.Atan2(dy, dx);
transform.rotation = MathHelper.RadToDeg(angleRad) - 90;
}
}