-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.cs
116 lines (103 loc) · 3.42 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace seainvaders
{
abstract class Entity
{
public int x;
public int y;
public Bitmap sprite;
public Bitmap DeathSprite;
public event EventHandler Deleted;
public float scale = 3;
public int height
{
get
{
return sprite.Height;
}
}
public int width
{
get
{
return sprite.Width;
}
}
public Entity()
{
Game.EntityList.Add(this);
}
public Bitmap CharArrayToBitmap(char[,] graphic)
{
Bitmap outputsprite = new Bitmap(graphic.GetLength(1), graphic.GetLength(0));
for (int i = 0; i < graphic.GetLength(0); i++)
{
for (int j = 0; j < graphic.GetLength(1); j++)
{
if (graphic[i, j] == '.')
{
outputsprite.SetPixel(j, i, Color.Transparent);
}
else
{
outputsprite.SetPixel(j, i, Color.DarkSeaGreen);
}
}
}
return outputsprite;
}
public void SetGraphic(char[,] graphic)
{
sprite = CharArrayToBitmap(graphic);
}
public void SetDeathGraphic(char[,] graphic)
{
DeathSprite = CharArrayToBitmap(graphic);
}
public int[,] GetBoundingPoints()
{
int[,] points = {
{x, y }, // 0 top left
{(x + width), y }, // 1 top right
{x, (y + height) }, // 2 bottom left
{(x + width), (y + height)} // 3 bottom right
};
return points;
}
public bool IsColliding(Entity entity)
{
int[,] points1 = GetBoundingPoints();
int[,] points2 = entity.GetBoundingPoints();
int XOverlap = Math.Max(0, Math.Min(points1[3, 0], points2[3, 0]) - Math.Max(points1[0, 0], points2[0, 0])); // this tells you how much x overlap there is
int YOverlap = Math.Max(0, Math.Min(points1[3, 1], points2[3, 1]) - Math.Max(points1[0, 1], points2[0, 1]));
if (XOverlap > 0 && YOverlap > 0)
{
return true;
}
else
{
return false;
}
}
public virtual void Delete() // virtual is like a placeholder - this is the function that's going to call the event handler
{
Game.EntityList.Remove(this);
EventHandler handler = Deleted;
handler?.Invoke(this, new EventArgs()); // ? means if there is a handler, do this. If there is no handler, then don't do anything.
if (DeathSprite != null)
{
new Explosion(x, y, DeathSprite);
}
}
public abstract void Update(); // this tells classes inheriting "entity" that there needs to be an Update() function at the bottom of their script
public void Render(Graphics graphics)
{
graphics.DrawImage(sprite, scale*x, scale*y, scale*width, scale*height);
}
}
}