/
GameObject.cs
110 lines (86 loc) · 2.8 KB
/
GameObject.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpaceWars
{
public abstract class GameObject : IRenderable, ICollidable, IObjectProducer
{
public const string CollisionGroupString = "object";
protected MatrixCoords topLeft;
public MatrixCoords TopLeft
{
get
{
return new MatrixCoords(topLeft.Row, topLeft.Col);
}
protected set
{
this.topLeft = new MatrixCoords(value.Row, value.Col);
}
}
protected char[,] body;
public bool IsDestroyed { get; protected set; }
protected GameObject(MatrixCoords topLeft, char[,] body)
{
this.TopLeft = topLeft;
int imageRows = body.GetLength(0);
int imageCols = body.GetLength(1);
this.body = this.CopyBodyMatrix(body);
this.IsDestroyed = false;
}
public abstract void Update();
public virtual List<MatrixCoords> GetCollisionProfile()
{
List<MatrixCoords> profile = new List<MatrixCoords>();
int bodyRows = this.body.GetLength(0);
int bodyCols = this.body.GetLength(1);
for (int row = 0; row < bodyRows; row++)
{
for (int col = 0; col < bodyCols; col++)
{
profile.Add(new MatrixCoords(row + this.topLeft.Row, col + this.topLeft.Col));
}
}
return profile;
}
public virtual void RespondToCollision(CollisionData collisionData)
{
}
public virtual bool CanCollideWith(string otherCollisionGroupString)
{
return GameObject.CollisionGroupString == otherCollisionGroupString;
}
public virtual string GetCollisionGroupString()
{
return GameObject.CollisionGroupString;
}
char[,] CopyBodyMatrix(char[,] matrixToCopy)
{
int rows = matrixToCopy.GetLength(0);
int cols = matrixToCopy.GetLength(1);
char[,] result = new char[rows, cols];
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
result[row, col] = matrixToCopy[row, col];
}
}
return result;
}
public virtual MatrixCoords GetTopLeft()
{
return this.TopLeft;
}
public virtual char[,] GetImage()
{
return this.CopyBodyMatrix(this.body);
}
public virtual IEnumerable<GameObject> ProduceObjects()
{
return new List<GameObject>();
}
}
}