Skip to content

Commit f0f86ea

Browse files
committed
feat(entity): create children and parent system
1 parent a6ee389 commit f0f86ea

6 files changed

Lines changed: 97 additions & 22 deletions

File tree

SharpEngine.Core/Component/AutoComponent.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ public override void Update(float delta)
3939

4040
if (Direction.Length() != 0)
4141
{
42-
_transform.Position = new Vec2(
43-
_transform.Position.X + Direction.X * delta,
44-
_transform.Position.Y + Direction.Y * delta
42+
_transform.LocalPosition = new Vec2(
43+
_transform.LocalPosition.X + Direction.X * delta,
44+
_transform.LocalPosition.Y + Direction.Y * delta
4545
);
4646
}
4747

4848
if (Rotation != 0)
49-
_transform.Rotation += Rotation;
49+
_transform.LocalRotation += Rotation;
5050
}
5151
}

SharpEngine.Core/Component/ControlComponent.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,27 +127,27 @@ public override void Update(float delta)
127127
IsMoving = true;
128128
Direction = move.Normalized();
129129
var newPos = new Vec2(
130-
_transform.Position.X + Direction.X * Speed * delta,
131-
_transform.Position.Y + Direction.Y * Speed * delta
130+
_transform.LocalPosition.X + Direction.X * Speed * delta,
131+
_transform.LocalPosition.Y + Direction.Y * Speed * delta
132132
);
133133
var newPosX = new Vec2(
134-
_transform.Position.X + Speed * delta * (Direction.X < 0 ? -1 : 1),
135-
_transform.Position.Y
134+
_transform.LocalPosition.X + Speed * delta * (Direction.X < 0 ? -1 : 1),
135+
_transform.LocalPosition.Y
136136
);
137137
var newPosY = new Vec2(
138-
_transform.Position.X,
139-
_transform.Position.Y + Speed * delta * (Direction.Y < 0 ? -1 : 1)
138+
_transform.LocalPosition.X,
139+
_transform.LocalPosition.Y + Speed * delta * (Direction.Y < 0 ? -1 : 1)
140140
);
141-
if (_basicPhysics == null || _basicPhysics.CanGo(newPos))
142-
_transform.Position = newPos;
143-
else if (Direction.X != 0 && _basicPhysics.CanGo(newPosX))
141+
if (_basicPhysics == null || _basicPhysics.CanGo(newPos + Entity?.Parent?.GetComponentAs<TransformComponent>()?.Position ?? Vec2.Zero))
142+
_transform.LocalPosition = newPos;
143+
else if (Direction.X != 0 && _basicPhysics.CanGo(newPosX + Entity?.Parent?.GetComponentAs<TransformComponent>()?.Position ?? Vec2.Zero))
144144
{
145-
_transform.Position = newPosX;
145+
_transform.LocalPosition = newPosX;
146146
Direction = new Vec2(Direction.X < 0 ? -1 : 1, 0);
147147
}
148-
else if (Direction.Y != 0 && _basicPhysics.CanGo(newPosY))
148+
else if (Direction.Y != 0 && _basicPhysics.CanGo(newPosY + Entity?.Parent?.GetComponentAs<TransformComponent>()?.Position ?? Vec2.Zero))
149149
{
150-
_transform.Position = newPosY;
150+
_transform.LocalPosition = newPosY;
151151
Direction = new Vec2(0, Direction.Y < 0 ? -1 : 1);
152152
}
153153
else

SharpEngine.Core/Component/ParticleComponent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override void Update(float delta)
3232
return;
3333

3434
foreach (var emitter in ParticleEmitters)
35-
emitter.Update(delta, _transform.GetTransformedPosition());
35+
emitter.Update(delta, _transform.Position);
3636
}
3737

3838
/// <inheritdoc />

SharpEngine.Core/Component/TransformComponent.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,41 @@ public class TransformComponent(
1919
/// <summary>
2020
/// Position of Component
2121
/// </summary>
22-
public Vec2 Position { get; set; } = position ?? Vec2.Zero;
22+
public Vec2 Position => Entity?.Parent?.GetComponentAs<TransformComponent>()?.Position + LocalPosition ?? LocalPosition;
2323

2424
/// <summary>
2525
/// Scale of Component
2626
/// </summary>
27-
public Vec2 Scale { get; set; } = scale ?? Vec2.One;
27+
public Vec2 Scale => Entity?.Parent?.GetComponentAs<TransformComponent>()?.Scale * LocalScale ?? LocalScale;
2828

2929
/// <summary>
3030
/// Rotation of Component
3131
/// </summary>
32-
public float Rotation { get; set; } = rotation;
32+
public float Rotation => Entity?.Parent?.GetComponentAs<TransformComponent>()?.Rotation + LocalRotation ?? LocalRotation;
3333

3434
/// <summary>
3535
/// ZLayer of Component
3636
/// </summary>
37-
public int ZLayer { get; set; } = zLayer;
37+
public int ZLayer => Entity?.Parent?.GetComponentAs<TransformComponent>()?.ZLayer + LocalZLayer ?? LocalZLayer;
38+
/// <summary>
39+
/// Position of Component
40+
/// </summary>
41+
public Vec2 LocalPosition { get; set; } = position ?? Vec2.Zero;
42+
43+
/// <summary>
44+
/// Scale of Component
45+
/// </summary>
46+
public Vec2 LocalScale { get; set; } = scale ?? Vec2.One;
47+
48+
/// <summary>
49+
/// Rotation of Component
50+
/// </summary>
51+
public float LocalRotation { get; set; } = rotation;
52+
53+
/// <summary>
54+
/// ZLayer of Component
55+
/// </summary>
56+
public int LocalZLayer { get; set; } = zLayer;
3857

3958
/// <summary>
4059
/// Get transformed Position

SharpEngine.Core/Entity/Entity.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ public class Entity
3434
/// </summary>
3535
public List<Component.Component> Components { get; } = [];
3636

37+
/// <summary>
38+
/// Get All Children of Entity
39+
/// </summary>
40+
public List<Entity> Children { get; } = [];
41+
42+
/// <summary>
43+
/// Parent of Entity
44+
/// </summary>
45+
public Entity? Parent { get; set; }
46+
3747
/// <summary>
3848
/// Get All Components of one Type
3949
/// </summary>
@@ -58,6 +68,21 @@ public List<T> GetComponentsAs<T>()
5868
public T? GetSceneAs<T>()
5969
where T : Scene => (T?)Scene;
6070

71+
/// <summary>
72+
/// Add Child Entity
73+
/// </summary>
74+
/// <typeparam name="T"></typeparam>
75+
/// <param name="entity"></param>
76+
/// <returns></returns>
77+
public T AddChild<T>(T entity)
78+
where T : Entity
79+
{
80+
Children.Add(entity);
81+
entity.Parent = this;
82+
entity.Scene = Scene;
83+
return entity;
84+
}
85+
6186
/// <summary>
6287
/// Add Component and return it
6388
/// </summary>
@@ -72,6 +97,17 @@ public T AddComponent<T>(T component)
7297
return component;
7398
}
7499

100+
/// <summary>
101+
/// Remove Child
102+
/// </summary>
103+
/// <param name="entity">Child</param>
104+
public void RemoveChild(Entity entity)
105+
{
106+
entity.Parent = null;
107+
entity.Scene = null;
108+
Children.Remove(entity);
109+
}
110+
75111
/// <summary>
76112
/// Remove Component
77113
/// </summary>
@@ -89,6 +125,9 @@ public virtual void Load()
89125
{
90126
foreach (var component in Components)
91127
component.Load();
128+
129+
foreach (var child in Children)
130+
child.Load();
92131
}
93132

94133
/// <summary>
@@ -98,6 +137,9 @@ public virtual void Unload()
98137
{
99138
foreach (var component in Components)
100139
component.Unload();
140+
141+
foreach (var child in Children)
142+
child.Unload();
101143
}
102144

103145
/// <summary>
@@ -108,6 +150,9 @@ public virtual void Update(float delta)
108150
{
109151
foreach (var component in Components)
110152
component.Update(delta);
153+
154+
foreach (var child in Children)
155+
child.Update(delta);
111156
}
112157

113158
/// <summary>
@@ -117,5 +162,8 @@ public virtual void Draw()
117162
{
118163
foreach (var component in Components)
119164
component.Draw();
165+
166+
foreach (var child in Children)
167+
child.Draw();
120168
}
121169
}

Testing/MyScene.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,22 @@ public class MyScene : Scene
1616

1717
public MyScene()
1818
{
19+
var movable = new Entity();
20+
movable.AddComponent(new TransformComponent(new Vec2(100)));
21+
movable.AddComponent(new SpriteSheetComponent("portal", new Vec2(100), [
22+
new("animation", Enumerable.Range(0, 10).Select(x => Convert.ToUInt32(x)).ToList(), 0.2f, false)
23+
], "animation"));
24+
movable.AddComponent(new ControlComponent());
25+
AddEntity(movable);
26+
1927
var entity = new Entity();
2028
entity.AddComponent(new TransformComponent(new Vec2(300)));
2129
_sprite = entity.AddComponent(new SpriteSheetComponent("portal", new Vec2(100), [
2230
new("animation", Enumerable.Range(0, 10).Select(x => Convert.ToUInt32(x)).ToList(), 0.2f, false)
2331
], "animation"));
2432
_sprite.AnimationEnded += (_, _) => Console.WriteLine($"Animation Ended : {_sprite.Anim}");
2533
_text = entity.AddComponent(new TextComponent("0", offset: new Vec2(0, 50), fontSize: 25));
26-
AddEntity(entity);
34+
movable.AddChild(entity);
2735

2836
var button = new Button(
2937
new Vec2(100, 100),

0 commit comments

Comments
 (0)