-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenDrawer.cs
195 lines (162 loc) · 8.73 KB
/
ScreenDrawer.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlyingJavabo
{
class ScreenDrawer
{
private const double radian = 0.0032;
private DukeManager dukeManager;
private JavaButtonManager buttonManager;
private System.Drawing.Image dukeImage = Properties.Resources.duke;
private System.Drawing.Image buttonImage = Properties.Resources.javabutton;
private System.Drawing.Size graphicSize;
private System.Drawing.Size tileSize;
private System.Drawing.Size dukeSize;
private System.Drawing.Size buttonSize;
private System.Drawing.Size moveSpaceSize;
private System.Drawing.Rectangle skyRect;
private System.Drawing.Bitmap backgroundImage;
public ScreenDrawer(int width, int height)
{
graphicSize = new System.Drawing.Size(width, height);
tileSize = new System.Drawing.Size(width / 10, height / 10);
dukeSize = new System.Drawing.Size(width / 10, width / 10);
buttonSize = new System.Drawing.Size(width / 5, width / 25);
backgroundImage = new System.Drawing.Bitmap(graphicSize.Width, graphicSize.Height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(backgroundImage);
g.FillRectangle(System.Drawing.Brushes.White, 0, 0, width, height);
DrawSky(g, graphicSize, System.Drawing.Color.LightCoral, System.Drawing.Color.White);
System.Drawing.Point point = DrawFloor(g, graphicSize, tileSize, radian, System.Drawing.Brushes.Lime, System.Drawing.Brushes.Honeydew);
moveSpaceSize = new System.Drawing.Size(point.X * 2, point.Y);
skyRect = new System.Drawing.Rectangle(0, 0, graphicSize.Width, graphicSize.Height / 2);
dukeManager = new DukeManager(moveSpaceSize.Height / 16);
buttonManager = new JavaButtonManager(moveSpaceSize.Height / 4);
}
public void Draw(System.Drawing.Graphics graphics)
{
System.Drawing.Bitmap image = new System.Drawing.Bitmap(graphicSize.Width, graphicSize.Height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.DrawImage(backgroundImage, 0, 0);
dukeManager.Move(moveSpaceSize);
DrawDuke(g, moveSpaceSize, dukeSize, dukeManager, radian, dukeImage);
buttonManager.Move(skyRect, moveSpaceSize.Height);
DrawButton(g, buttonSize, buttonManager, buttonImage);
graphics.DrawImage(image, 0, 0);
}
#region Draw Background
private System.Drawing.Point DrawFloor(System.Drawing.Graphics graphics, System.Drawing.Size graphicSize, System.Drawing.Size tileSize, double radian, System.Drawing.Brush brushA, System.Drawing.Brush brushB)
{
double COSA = Math.Cos(radian);
double SINA = Math.Sin(radian);
System.Drawing.Point point = new System.Drawing.Point();
bool isWidth = false;
for(int i = 0; !isWidth; i++)
{
bool isHeight = false;
for(int j = 0; !isHeight; j++)
{
System.Drawing.Point[] points = new System.Drawing.Point[] {
new System.Drawing.Point(tileSize.Width*i, tileSize.Height*j),
new System.Drawing.Point(tileSize.Width*(i+1), tileSize.Height*j),
new System.Drawing.Point(tileSize.Width*(i+1), tileSize.Height*(j+1)),
new System.Drawing.Point(tileSize.Width*i, tileSize.Height*(j+1))
};
System.Drawing.Point temp = points[0];
System.Drawing.Point[] pointsa = new System.Drawing.Point[4];
System.Drawing.Point[] pointsb = new System.Drawing.Point[4];
for(int k = 0; k < 4; k++)
{
points[k] = rotateX(points[k], COSA, SINA);
pointsa[k] = translate(scale(points[k], -1, -1), graphicSize.Width / 2, graphicSize.Height);
pointsb[k] = translate(scale(points[k], 1, -1), graphicSize.Width / 2, graphicSize.Height);
}
bool isHalf = (i + j) % 2 == 0;
System.Drawing.Brush brusha = isHalf ? brushA : brushB;
System.Drawing.Brush brushb = !isHalf ? brushA : brushB;
graphics.FillPolygon(brusha, pointsa);
graphics.FillPolygon(brushb, pointsb);
isWidth = points[0].X > graphicSize.Width / 2;
isHeight = points[0].Y > graphicSize.Height / 2 || points[2].Y == points[0].Y;
if(j == 0 && !isWidth)
{
point.X = temp.X;
}
if(i == 0 && points[2].Y > points[0].Y + tileSize.Height / 10)
{
point.Y = temp.Y;
}
}
}
return point;
}
private void DrawSky(System.Drawing.Graphics graphics, System.Drawing.Size graphicSize, System.Drawing.Color colorA, System.Drawing.Color colorB)
{
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, graphicSize.Width, graphicSize.Height * 15 / 32);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(rect, colorA, colorB, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
graphics.FillRectangle(brush, rect);
}
#endregion
private void DrawDuke(System.Drawing.Graphics graphics, System.Drawing.Size moveSpaceSize, System.Drawing.Size dukeSize, DukeManager manager, double radian, System.Drawing.Image image)
{
double COSA = Math.Cos(radian);
double SINA = Math.Sin(radian);
for (int i = 0; i < manager.DukeNumber; i++)
{
System.Drawing.Point duke = translate(manager.GetDukePoint(i), -moveSpaceSize.Width / 2, 0);
System.Drawing.Point size = new System.Drawing.Point(duke.X, duke.Y - dukeSize.Height);
duke = rotateX(duke, COSA, SINA);
duke = scale(duke, 1, -1);
duke = translate(duke, graphicSize.Width / 2, moveSpaceSize.Height);
size = rotateX(size, COSA, SINA);
size = scale(size, 1, -1);
size = translate(size, graphicSize.Width / 2, moveSpaceSize.Height);
int sizeInt = size.Y - duke.Y;
if(sizeInt * 4 < dukeSize.Height)
{
continue;
}
duke.X -= sizeInt / 2;
duke.Y += sizeInt;
graphics.DrawImage(image, new System.Drawing.Rectangle(duke, new System.Drawing.Size(sizeInt, sizeInt)));
}
}
private void DrawButton(System.Drawing.Graphics graphics, System.Drawing.Size buttonSize, JavaButtonManager manager, System.Drawing.Image image)
{
for (int i = 0; i < manager.ButtonNumber; i++)
{
System.Drawing.Point button = manager.GetButtonPoint(i);
int depth = manager.GetButtonDepth(i);
if (depth == 0)
{
continue;
}
int width = buttonSize.Width / depth;
int height = buttonSize.Height / depth;
graphics.DrawImage(image, new System.Drawing.Rectangle(button.X - width / 2, button.Y - height / 2, width, height));
}
}
#region Point Transformation
private System.Drawing.Point translate(System.Drawing.Point point, int x, int y)
{
return new System.Drawing.Point(point.X + x, point.Y + y);
}
private System.Drawing.Point scale(System.Drawing.Point point, double x, double y)
{
return new System.Drawing.Point((int)(point.X * x), (int)(point.Y * y));
}
private System.Drawing.Point rotateX(System.Drawing.Point point, double radian)
{
return rotateX(point, Math.Cos(radian), Math.Sin(radian));
}
private System.Drawing.Point rotateX(System.Drawing.Point point, double cosa, double sina)
{
double ya = point.Y * cosa - sina;
double za = point.Y * sina + cosa;
return new System.Drawing.Point((int)(point.X / za), (int)(ya / za));
}
#endregion
}
}