Description:
Add parameter to Line for width.
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
@@ -0,0 +1,379 | |||
|
1 | using Microsoft.Xna.Framework; | |
|
2 | using Microsoft.Xna.Framework.Audio; | |
|
3 | using Microsoft.Xna.Framework.Input; | |
|
4 | using Microsoft.Xna.Framework.Graphics; | |
|
5 | using Microsoft.Xna.Framework.Media; | |
|
6 | ||
|
7 | using System; | |
|
8 | using System.IO; | |
|
9 | using SpriteFontPlus; | |
|
10 | using isometricparkfna; | |
|
11 | ||
|
12 | struct Entity | |
|
13 | { | |
|
14 | public Vector2 loc; | |
|
15 | public Vector2 velocity; | |
|
16 | ||
|
17 | private Rectangle _boundingBox; | |
|
18 | ||
|
19 | public Rectangle BoundingBox | |
|
20 | { | |
|
21 | ||
|
22 | get | |
|
23 | { | |
|
24 | Rectangle calculatedRectangle = _boundingBox; | |
|
25 | calculatedRectangle.Location += new Point((int)loc.X, (int)loc.Y); | |
|
26 | return calculatedRectangle; | |
|
27 | } | |
|
28 | set { _boundingBox = value; } | |
|
29 | } | |
|
30 | ||
|
31 | public Entity(Vector2 loc, Vector2 velocity) | |
|
32 | { | |
|
33 | this.loc = loc; | |
|
34 | this.velocity = velocity; | |
|
35 | this._boundingBox = Rectangle.Empty; | |
|
36 | } | |
|
37 | ||
|
38 | public Entity(Vector2 loc, Vector2 velocity, Rectangle boundingBox) | |
|
39 | { | |
|
40 | this.loc = loc; | |
|
41 | this.velocity = velocity; | |
|
42 | this._boundingBox = boundingBox; | |
|
43 | } | |
|
44 | } | |
|
45 | ||
|
46 | static class Camera | |
|
47 | { | |
|
48 | static public Vector2 Location = Vector2.Zero; | |
|
49 | } | |
|
50 | ||
|
51 | class FNAGame : Game | |
|
52 | { | |
|
53 | private KeyboardState keyboardPrev = new KeyboardState(); | |
|
54 | ||
|
55 | private SpriteBatch batch; | |
|
56 | private Texture2D texture; | |
|
57 | private Texture2D paddle; | |
|
58 | private Texture2D ball; | |
|
59 | private SoundEffect sound; | |
|
60 | private Song music; | |
|
61 | private SpriteFont font; | |
|
62 | ||
|
63 | int frameRate = 0; | |
|
64 | int frameCounter = 0; | |
|
65 | TimeSpan elapsedTime = TimeSpan.Zero; | |
|
66 | ||
|
67 | private const int width = 1280; | |
|
68 | private const int height = 640; | |
|
69 | ||
|
70 | ||
|
71 | private Entity paddleEntity = new Entity(Vector2.Zero, Vector2.Zero, | |
|
72 | new Rectangle(36, 0, 20, 100)); | |
|
73 | private Entity ballEntity = new Entity(new Vector2(height, 360), | |
|
74 | new Vector2(2.0f, 2.0f), | |
|
75 | new Rectangle(36, 36, 20, 20)); | |
|
76 | private Entity aiPaddleEntity = new Entity(new Vector2(width - 100, 0), Vector2.Zero, | |
|
77 | new Rectangle(36, 0, 20, 100)); | |
|
78 | ||
|
79 | private const float friction = 0.1f; | |
|
80 | private const float inputVelocityDelta = friction + 0.1f; | |
|
81 | ||
|
82 | ||
|
83 | private int playerScore = 0; | |
|
84 | private int aiScore = 0; | |
|
85 | ||
|
86 | //new tile stuff | |
|
87 | TileMap myMap = new TileMap(); | |
|
88 | int squaresAcross = 25; | |
|
89 | int squaresDown = 25; | |
|
90 | int baseOffsetX = -14; | |
|
91 | int baseOffsetY = -14; | |
|
92 | ||
|
93 | ||
|
94 | private static void Main(string[] args) | |
|
95 | { | |
|
96 | using FNAGame g = new FNAGame(); | |
|
97 | g.Run(); | |
|
98 | } | |
|
99 | ||
|
100 | public static bool Between(float val, float x, float y) | |
|
101 | { | |
|
102 | return ((x < val && val < y) || (y < val && val < x)); | |
|
103 | ||
|
104 | } | |
|
105 | ||
|
106 | public Entity Move(ref Entity orig, bool score) | |
|
107 | { | |
|
108 | orig.loc += orig.velocity; | |
|
109 | ||
|
110 | if (orig.loc.Y <= 0 && orig.velocity.Y < 0) | |
|
111 | { | |
|
112 | orig.velocity.Y *= -1; | |
|
113 | orig.loc.Y = 0; | |
|
114 | } | |
|
115 | else if (orig.loc.Y >= (height) && orig.velocity.Y > 0) | |
|
116 | { | |
|
117 | orig.velocity.Y *= -1; | |
|
118 | orig.loc.Y = (720 - 100); | |
|
119 | } | |
|
120 | ||
|
121 | if (orig.loc.X <= 0 && orig.velocity.X < 0) | |
|
122 | { | |
|
123 | orig.velocity.X *= -1; | |
|
124 | orig.loc.X = 0; | |
|
125 | ||
|
126 | if (score) | |
|
127 | { | |
|
128 | playerScore += 1; | |
|
129 | } | |
|
130 | } | |
|
131 | else if (orig.loc.X >= width && orig.velocity.X > 0) | |
|
132 | { | |
|
133 | orig.velocity.X *= -1; | |
|
134 | orig.loc.X = width; | |
|
135 | ||
|
136 | if (score) | |
|
137 | { | |
|
138 | aiScore += 1; | |
|
139 | } | |
|
140 | } | |
|
141 | ||
|
142 | return orig; | |
|
143 | } | |
|
144 | ||
|
145 | ||
|
146 | ||
|
147 | private FNAGame() | |
|
148 | { | |
|
149 | GraphicsDeviceManager gdm = new GraphicsDeviceManager(this) | |
|
150 | { | |
|
151 | ||
|
152 | // Typically you would load a config here... | |
|
153 | PreferredBackBufferWidth = width, | |
|
154 | PreferredBackBufferHeight = 720, | |
|
155 | IsFullScreen = false, | |
|
156 | SynchronizeWithVerticalRetrace = true | |
|
157 | }; | |
|
158 | //gdm.SynchronizeWithVerticalRetrace = false; | |
|
159 | IsFixedTimeStep = false; | |
|
160 | ||
|
161 | Content.RootDirectory = "Content"; | |
|
162 | ||
|
163 | ||
|
164 | } | |
|
165 | ||
|
166 | protected override void Initialize() | |
|
167 | { | |
|
168 | /* This is a nice place to start up the engine, after | |
|
169 | * loading configuration stuff in the constructor | |
|
170 | */ | |
|
171 | ||
|
172 | base.Initialize(); | |
|
173 | } | |
|
174 | ||
|
175 | protected override void LoadContent() | |
|
176 | { | |
|
177 | // Load textures, sounds, and so on in here... | |
|
178 | // Create the batch... | |
|
179 | batch = new SpriteBatch(GraphicsDevice); | |
|
180 | ||
|
181 | // ... then load a texture from ./Content/FNATexture.png | |
|
182 | texture = Content.Load<Texture2D>("FNATexture"); | |
|
183 | paddle = Content.Load<Texture2D>("paddle"); | |
|
184 | ball = Content.Load<Texture2D>("ball"); | |
|
185 | sound = Content.Load<SoundEffect>("FNASound"); | |
|
186 | music = Content.Load<Song>("IfImWrong"); | |
|
187 | Tile.TileSetTexture = Content.Load<Texture2D>(@"part4_tileset"); | |
|
188 | ||
|
189 | ||
|
190 | Line.initialize(GraphicsDevice); | |
|
191 | ||
|
192 | ||
|
193 | ||
|
194 | var fontBakeResult = TtfFontBaker.Bake(File.OpenRead(@"Content/DroidSans.ttf"), | |
|
195 | 25, | |
|
196 | 1024, | |
|
197 | 1024, | |
|
198 | new[] | |
|
199 | { | |
|
200 | CharacterRange.BasicLatin, | |
|
201 | CharacterRange.Latin1Supplement, | |
|
202 | CharacterRange.LatinExtendedA, | |
|
203 | CharacterRange.Cyrillic | |
|
204 | } | |
|
205 | ); | |
|
206 | ||
|
207 | font = fontBakeResult.CreateSpriteFont(GraphicsDevice); | |
|
208 | //DynamicSpriteFont font = DynamicSpriteFont.FromTtf(File.ReadAllBytes(@"Content/DroidSans.ttf"), 20); | |
|
209 | ||
|
210 | } | |
|
211 | ||
|
212 | protected override void UnloadContent() | |
|
213 | { | |
|
214 | // Clean up after yourself! | |
|
215 | batch.Dispose(); | |
|
216 | texture.Dispose(); | |
|
217 | sound.Dispose(); | |
|
218 | music.Dispose(); | |
|
219 | } | |
|
220 | ||
|
221 | ||
|
222 | protected float Decrement(float value, float delta) | |
|
223 | { | |
|
224 | float magnitude = Math.Abs(value); | |
|
225 | ||
|
226 | //If distance from zero is less than our delta, | |
|
227 | //go to zero to prevent overshooting: | |
|
228 | if (magnitude < delta) | |
|
229 | { | |
|
230 | return 0.0f; | |
|
231 | } | |
|
232 | else if (value > 0) | |
|
233 | { | |
|
234 | return value - delta; | |
|
235 | } | |
|
236 | else if (value < 0) | |
|
237 | { | |
|
238 | return value + delta; | |
|
239 | } | |
|
240 | else | |
|
241 | { | |
|
242 | return 0.0f; | |
|
243 | } | |
|
244 | } | |
|
245 | ||
|
246 | protected override void Update(GameTime gameTime) | |
|
247 | { | |
|
248 | ||
|
249 | float volume = 1.0f; | |
|
250 | float pitch = 0.0f; | |
|
251 | float pan = 0.0f; | |
|
252 | ||
|
253 | // Run game logic in here. Do NOT render anything here! | |
|
254 | KeyboardState keyboardCur = Keyboard.GetState(); | |
|
255 | if (keyboardCur.IsKeyDown(Keys.Q) && keyboardPrev.IsKeyUp(Keys.Q)) | |
|
256 | { | |
|
257 | System.Console.WriteLine("Quitting"); | |
|
258 | Environment.Exit(0); | |
|
259 | } | |
|
260 | ||
|
261 | ||
|
262 | if (keyboardCur.IsKeyDown(Keys.Space) && keyboardPrev.IsKeyUp(Keys.Space)) | |
|
263 | { | |
|
264 | sound.Play(volume, pitch, pan); | |
|
265 | } | |
|
266 | //if (keyboardCur.IsKeyDown(Keys.P) && keyboardPrev.IsKeyUp(Keys.P)) | |
|
267 | // { | |
|
268 | // this.player_state = !this.player_state; | |
|
269 | // } | |
|
270 | ||
|
271 | //if (player_state ) | |
|
272 | //{ | |
|
273 | // MediaPlayer.Play(music); | |
|
274 | //} | |
|
275 | ||
|
276 | ||
|
277 | elapsedTime += gameTime.ElapsedGameTime; | |
|
278 | ||
|
279 | if (elapsedTime > TimeSpan.FromSeconds(1)) | |
|
280 | { | |
|
281 | elapsedTime -= TimeSpan.FromSeconds(1); | |
|
282 | frameRate = frameCounter; | |
|
283 | frameCounter = 0; | |
|
284 | } | |
|
285 | ||
|
286 | base.Update(gameTime); | |
|
287 | ||
|
288 | ||
|
289 | } | |
|
290 | ||
|
291 | protected override void Draw(GameTime gameTime) | |
|
292 | { | |
|
293 | // Render stuff in here. Do NOT run game logic in here! | |
|
294 | ||
|
295 | frameCounter++; | |
|
296 | ||
|
297 | string fps = string.Format("fps: {0}", frameRate); | |
|
298 | ||
|
299 | ||
|
300 | ||
|
301 | ||
|
302 | GraphicsDevice.Clear(Color.CornflowerBlue); | |
|
303 | batch.Begin(); | |
|
304 | ||
|
305 | Line.drawLine(batch, new Vector2(0, 1), new Vector2(1, 50), Color.Black); | |
|
306 | Line.drawLine(batch, new Vector2(0, 50), new Vector2(1, 100), Color.White); | |
|
307 | Line.drawLine(batch, new Vector2(500, 500), new Vector2(500, 1000), Color.White); | |
|
308 | ||
|
309 | ||
|
310 | //New tile stuff | |
|
311 | Vector2 firstSquare = new Vector2(Camera.Location.X / 32, | |
|
312 | Camera.Location.Y / 32); | |
|
313 | int firstX = (int)firstSquare.X; | |
|
314 | int firstY = (int)firstSquare.Y; | |
|
315 | ||
|
316 | Vector2 squareOffset = new Vector2(Camera.Location.X % 32, | |
|
317 | Camera.Location.Y % 32); | |
|
318 | ||
|
319 | int offsetX = (int)squareOffset.X; | |
|
320 | int offsetY = (int)squareOffset.Y; | |
|
321 | ||
|
322 | ||
|
323 | for (int y = 0; y < this.squaresDown; y++) | |
|
324 | { | |
|
325 | int rowOffset = 0; | |
|
326 | if ((firstY + y) % 2 == 1) | |
|
327 | rowOffset = Tile.OddRowXOffset; | |
|
328 | ||
|
329 | for (int x = 0; x < this.squaresAcross; x++) | |
|
330 | { | |
|
331 | batch.Draw( | |
|
332 | Tile.TileSetTexture, | |
|
333 | new Rectangle( | |
|
334 | (x * Tile.TileStepX) - offsetX + rowOffset + baseOffsetX, | |
|
335 | (y * Tile.TileStepY) - offsetY + baseOffsetY, | |
|
336 | Tile.TileWidth, Tile.TileHeight), | |
|
337 | Tile.GetSourceRectangle(1), | |
|
338 | Color.White); | |
|
339 | } | |
|
340 | } | |
|
341 | ||
|
342 | //Gridlines | |
|
343 | //Lines going down and to the right: | |
|
344 | for (int x = (-this.squaresAcross); x < this.squaresAcross; x++) | |
|
345 | { | |
|
346 | int rowOffset = 0; | |
|
347 | ||
|
348 | float startX = (x * Tile.TileStepX) + baseOffsetX - (Tile.TileStepX / 2); | |
|
349 | ||
|
350 | Vector2 start = new Vector2(startX, -baseOffsetY+4); | |
|
351 | Vector2 stop = new Vector2(startX + this.squaresAcross* Tile.TileStepX/2, | |
|
352 | this.squaresDown*Tile.TileStepY- baseOffsetY+4); | |
|
353 | ||
|
354 | Line.drawLine(batch, start, stop, Color.White); | |
|
355 | ||
|
356 | } | |
|
357 | //Lines going down and to the left: | |
|
358 | for (int x = 0; x < (2*this.squaresAcross); x++) | |
|
359 | { | |
|
360 | ||
|
361 | float startX = (x * Tile.TileStepX) + baseOffsetX - (Tile.TileStepX / 2); | |
|
362 | ||
|
363 | Vector2 start_reverse = new Vector2(startX, -baseOffsetY + 4); | |
|
364 | Vector2 stop_reverse = new Vector2(startX + -(this.squaresAcross * Tile.TileStepX / 2), | |
|
365 | (this.squaresDown * Tile.TileStepY) - baseOffsetY + 4); | |
|
366 | ||
|
367 | Line.drawLine(batch, start_reverse, stop_reverse, Color.White); | |
|
368 | ||
|
369 | } | |
|
370 | ||
|
371 | batch.DrawString(font, fps, new Vector2(33, 33), Color.Black); | |
|
372 | batch.DrawString(font, fps, new Vector2(32, 32), Color.White); | |
|
373 | batch.End(); | |
|
374 | ||
|
375 | base.Draw(gameTime); | |
|
376 | } | |
|
377 | ||
|
378 | ||
|
379 | } No newline at end of file |
@@ -15,9 +15,14 | |||
|
15 | 15 | |
|
16 | 16 | public static void drawLine(SpriteBatch batch, Vector2 start, Vector2 stop, Color color) |
|
17 | 17 | { |
|
18 | drawLine(batch, start, stop, color, 1); | |
|
19 | } | |
|
20 | ||
|
21 | public static void drawLine(SpriteBatch batch, Vector2 start, Vector2 stop, Color color, int width) | |
|
22 | { | |
|
18 | 23 | Vector2 line = stop - start; |
|
19 | 24 | float angle = (float)Math.Atan2((double)line.Y, (double) line.X); |
|
20 |
Rectangle rect = new Rectangle((int)start.X, (int)start.Y, (int)Math.Round(line.Length()), |
|
|
25 | Rectangle rect = new Rectangle((int)start.X, (int)start.Y, (int)Math.Round(line.Length()), width); | |
|
21 | 26 | batch.Draw(Line.PixelTexture, rect, null, color, angle, new Vector2(0,0), SpriteEffects.None, 0); |
|
22 | 27 | |
|
23 | 28 | } |
@@ -28,7 +28,7 | |||
|
28 | 28 | <LangVersion>8.0</LangVersion> |
|
29 | 29 | </PropertyGroup> |
|
30 | 30 | <ItemGroup> |
|
31 |
<Compile Include=" |
|
|
31 | <Compile Include="FNAGame.cs" /> | |
|
32 | 32 | <Compile Include="Tile.cs" /> |
|
33 | 33 | <Compile Include="TileMap.cs" /> |
|
34 | 34 | <Compile Include="Line.cs" /> |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
You need to be logged in to leave comments.
Login now