-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShield.cs
169 lines (152 loc) · 9.25 KB
/
Shield.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace seainvaders
{
class Shield : Entity
{
public char[,] Graphic
{
get
{
char[,] val =
{
{'.','.','.','.','0','0','0','0','0','0','0','0','0','0','0','0','0','0','.','.','.','.'},
{'.','.','.','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','.','.','.'},
{'.','.','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','.','.'},
{'.','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','.'},
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'},
{'0','0','0','0','0','0','0','.','.','.','.','.','.','.','0','0','0','0','0','0','0','0'},
{'0','0','0','0','0','0','.','.','.','.','.','.','.','.','.','0','0','0','0','0','0','0'},
{'0','0','0','0','0','.','.','.','.','.','.','.','.','.','.','.','0','0','0','0','0','0'},
{'0','0','0','0','0','.','.','.','.','.','.','.','.','.','.','.','0','0','0','0','0','0'},
};
return val;
}
}
public Shield(int x, int y) : base()
{
this.x = x; // this.x is from the Entity, and the = x is from our arguments from public Shield( int x, int y) : base()
this.y = y;
SetGraphic(Graphic);
}
public int[] FindIntersect(Entity entity) // read through the sprite and find the intersecting pixel between the two sprites
{
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])); // XOverlap is how many pixels overlap in the x direction
int YOverlap = Math.Max(0, Math.Min(points1[3, 1], points2[3, 1]) - Math.Max(points1[0, 1], points2[0, 1])); // YOverlap is how many pixels overlap in the y direction
int X1Start;
int Y1Start;
int X2Start;
int Y2Start;
// i believe the following if statements are to determine which pixels in our sprites need to be changed from '0' in their bitmaps, to '.' (to make them transparent)
if(points1[0, 0] < points2[0, 0]) // if the first sprite(top left, x value) is further left than the 2nd sprite(top left, x value)
{
X2Start = 0; // the top left, x value of the 2nd sprite already is the beginning of the intersection - no adjustment needed, aka '0'
X1Start = points2[0, 0] - points1[0, 0]; // example: if points1[0,0] = 30 and points2[0,0] = 38, then 38-30 = 8 pixels, so you'd want to add 8 to the x value of points1[0,0]
}
else // if the second sprite(top left, x value) is further left than the first sprite(top left, x value)
{
X1Start = 0; // basically the opposite of above is true, so the first sprite's top left x value is the beginning of the overlap
X2Start = points1[0, 0] - points2[0, 0]; // and the 2nd sprite's top left x value needs to add the difference between the two sprites' top left
// x values to itself to get the 'beginning of the intersection for the 2nd sprite's top left x value'
// i believe we will be using the sprite array and changing the values at the points of the array = points[0,0] + X1Start
}
if (points1[0, 1] < points2[0, 1])
{
Y2Start = 0;
Y1Start = points2[0, 1] - points1[0, 1];
}
else
{
Y1Start = 0;
Y2Start = points1[0, 1] - points2[0, 1];
}
int relY = 0; //relative Y
int j = Y1Start;
int j2 = Y2Start;
for (; relY < YOverlap; relY++, j++, j2++) // this is the key for this function
{
int relX = 0; // relative x
int i = X1Start;
int i2 = X2Start;
for (; relX < XOverlap; relX++, i++, i2++)
{
/*this.sprite.SetPixel(i, j, Color.Red);
entity.sprite.SetPixel(i2, j2, Color.Yellow);*/
if ((this.sprite.GetPixel(i, j).A != Color.Transparent.A) && (entity.sprite.GetPixel(i2, j2).A != Color.Transparent.A))
{
return new int[] { i, j };
}
}
}
return new int[] { -1, -1 }; // this is how you declare an array
}
public void Damage(Entity explosion) // draws an explosion, ie. deletes pixels on the shield as it gets hit
{
int[,] points1 = GetBoundingPoints();
int[,] points2 = explosion.GetBoundingPoints();
int XOverlap = Math.Max(0, Math.Min(points1[3, 0], points2[3, 0]) - Math.Max(points1[0, 0], points2[0, 0])); // XOverlap is how many pixels overlap in the x direction
int YOverlap = Math.Max(0, Math.Min(points1[3, 1], points2[3, 1]) - Math.Max(points1[0, 1], points2[0, 1])); // YOverlap is how many pixels overlap in the y direction
int X1Start;
int Y1Start;
int X2Start;
int Y2Start;
// i believe the following if statements are to determine which pixels in our sprites need to be changed from '0' in their bitmaps, to '.' (to make them transparent)
if (points1[0, 0] < points2[0, 0]) // if the first sprite(top left, x value) is further left than the 2nd sprite(top left, x value)
{
X2Start = 0; // the top left, x value of the 2nd sprite already is the beginning of the intersection - no adjustment needed, aka '0'
X1Start = points2[0, 0] - points1[0, 0]; // example: if points1[0,0] = 30 and points2[0,0] = 38, then 38-30 = 8 pixels, so you'd want to add 8 to the x value of points1[0,0]
}
else // if the second sprite(top left, x value) is further left than the first sprite(top left, x value)
{
X1Start = 0; // basically the opposite of above is true, so the first sprite's top left x value is the beginning of the overlap
X2Start = points1[0, 0] - points2[0, 0]; // and the 2nd sprite's top left x value needs to add the difference between the two sprites' top left
// x values to itself to get the 'beginning of the intersection for the 2nd sprite's top left x value'
// i believe we will be using the sprite array and changing the values at the points of the array = points[0,0] + X1Start
}
if (points1[0, 1] < points2[0, 1])
{
Y2Start = 0;
Y1Start = points2[0, 1] - points1[0, 1];
}
else
{
Y1Start = 0;
Y2Start = points1[0, 1] - points2[0, 1];
}
int relativeY = 0;
int j = Y1Start;
int j2 = Y2Start;
for (; relativeY < YOverlap; relativeY++, j++, j2++) // iterating through both the shield and bullet sprites' y values pixel by pixel
{
int relativeX = 0;
int i = X1Start;
int i2 = X2Start;
for (; relativeX < XOverlap; relativeX++, i++, i2++)
{
if((this.sprite.GetPixel(i, j).A != Color.Transparent.A) && (explosion.sprite.GetPixel(i2, j2).A != Color.Transparent.A))
{
this.sprite.SetPixel(i, j, Color.Transparent);
}
}
}
}
public override void Update()
{
}
}
}