-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.cs
64 lines (58 loc) · 2.22 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
// Entity.cs
// Created on: 2020-10-14
// Author: Leo Treloar
using System;
using System.Collections.Generic;
using SFML.Audio;
using SFML.Graphics;
using SFML.System;
using SFML.Window;
namespace UniverseIntruders
{
class Entity : Sprite
{
public View TargetView { get; protected set; }
public IntRect CollisionRect { get; set; }
public CollisionTag CollisionTag { get; set; }
public bool EntityDestroyed { get; set; }
public int Depth { get; set; }
public Entity(Texture texture, View targetView) : base(texture)
{
this.TargetView = targetView;
EntityDestroyed = false;
Depth = 0;
}
// The entity won't be rendered or run Update() until this is called
public virtual void Initialize()
{
Game.Entities.Add(this);
// Sort the list so that entities with the highest depth values come first
Game.Entities.Sort((e1, e2) => { return e2.Depth - e1.Depth; });
}
public virtual void Update() { }
// Calculate the bounding box of the collider in global coordinates
public FloatRect GetCollisionBounds()
{
FloatRect globalBounds = GetGlobalBounds();
FloatRect collisionBounds = new FloatRect();
collisionBounds.Top = globalBounds.Top + CollisionRect.Top;
collisionBounds.Left = globalBounds.Left + CollisionRect.Left;
collisionBounds.Width = CollisionRect.Width;
collisionBounds.Height = CollisionRect.Height;
return collisionBounds;
}
// Use the entire bounding rectangle of the sprite for collisions
protected void SetDefaultCollider() { CollisionRect = (IntRect)GetLocalBounds(); }
protected Entity CollisionWithTag(CollisionTag tag)
{
List<Entity> entities = Game.Entities.FindAll(e => e.CollisionTag == tag);
Entity collidingEntity = null;
foreach (Entity entity in entities)
{
if (Collision.IsRectInRect(this.GetCollisionBounds(), entity.GetCollisionBounds()))
collidingEntity = entity;
}
return collidingEntity;
}
}
}