-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEntity.cs
86 lines (69 loc) · 2 KB
/
Entity.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
using System;
using System.Collections.Generic;
namespace Nitemare3D
{
public class Entity
{
public Vec2 position = new Vec2();
public int id;
static int entCount;
public virtual void Update()
{
}
public virtual void Start()
{
}
public bool hasCollision = true;
public static List<Entity> entities = new List<Entity>();
static List<Entity> entityQueue = new List<Entity>();
static List<Entity> removeQueue = new List<Entity>();
public static T Create<T>() where T : Entity
{
return Create<T>(0, 0);
}
public void SendMessage(string message)
{
GetType().GetMethod(message)?.Invoke(this, null);
}
public static void Add(Entity entity, Vec2 position)
{
entityQueue.Add(entity);
entity.position = position;
}
public static void Remove(Entity entity)
{
removeQueue.Add(entity);
}
public static T Create<T>(Vec2 position) where T : Entity
{
return Create<T>(position.X, position.Y);
}
public static T Create<T>(float x, float y) where T : Entity
{
var entity = Activator.CreateInstance<T>();
entity.position = new Vec2(x, y) - .5f;
entityQueue.Add(entity);
return entity;
}
public static void UpdateEntites()
{
foreach (var entity in entities)
{
entity.Update();
}
foreach (var entity in entityQueue)
{
entity.id = entCount;
entCount++;
entity.Start();
entities.Add(entity);
}
foreach (var entity in removeQueue)
{
entities.Remove(entity);
}
entityQueue.Clear();
removeQueue.Clear();
}
}
}