# HG changeset patch # User alys # Date 2020-12-08 06:16:16 # Node ID 3169ee75a7e3c590df7b9ab557f74ee3f17ffecd # Parent e33f209de7e55b5a3d7c91618dc7444000186841 Add parameter to Line for width. diff --git a/isometric-park-fna/FNAGame.cs b/isometric-park-fna/FNAGame.cs new file mode 100644 --- /dev/null +++ b/isometric-park-fna/FNAGame.cs @@ -0,0 +1,379 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Audio; +using Microsoft.Xna.Framework.Input; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Media; + +using System; +using System.IO; +using SpriteFontPlus; +using isometricparkfna; + +struct Entity +{ + public Vector2 loc; + public Vector2 velocity; + + private Rectangle _boundingBox; + + public Rectangle BoundingBox + { + + get + { + Rectangle calculatedRectangle = _boundingBox; + calculatedRectangle.Location += new Point((int)loc.X, (int)loc.Y); + return calculatedRectangle; + } + set { _boundingBox = value; } + } + + public Entity(Vector2 loc, Vector2 velocity) + { + this.loc = loc; + this.velocity = velocity; + this._boundingBox = Rectangle.Empty; + } + + public Entity(Vector2 loc, Vector2 velocity, Rectangle boundingBox) + { + this.loc = loc; + this.velocity = velocity; + this._boundingBox = boundingBox; + } +} + +static class Camera +{ + static public Vector2 Location = Vector2.Zero; +} + +class FNAGame : Game +{ + private KeyboardState keyboardPrev = new KeyboardState(); + + private SpriteBatch batch; + private Texture2D texture; + private Texture2D paddle; + private Texture2D ball; + private SoundEffect sound; + private Song music; + private SpriteFont font; + + int frameRate = 0; + int frameCounter = 0; + TimeSpan elapsedTime = TimeSpan.Zero; + + private const int width = 1280; + private const int height = 640; + + + private Entity paddleEntity = new Entity(Vector2.Zero, Vector2.Zero, + new Rectangle(36, 0, 20, 100)); + private Entity ballEntity = new Entity(new Vector2(height, 360), + new Vector2(2.0f, 2.0f), + new Rectangle(36, 36, 20, 20)); + private Entity aiPaddleEntity = new Entity(new Vector2(width - 100, 0), Vector2.Zero, + new Rectangle(36, 0, 20, 100)); + + private const float friction = 0.1f; + private const float inputVelocityDelta = friction + 0.1f; + + + private int playerScore = 0; + private int aiScore = 0; + + //new tile stuff + TileMap myMap = new TileMap(); + int squaresAcross = 25; + int squaresDown = 25; + int baseOffsetX = -14; + int baseOffsetY = -14; + + + private static void Main(string[] args) + { + using FNAGame g = new FNAGame(); + g.Run(); + } + + public static bool Between(float val, float x, float y) + { + return ((x < val && val < y) || (y < val && val < x)); + + } + + public Entity Move(ref Entity orig, bool score) + { + orig.loc += orig.velocity; + + if (orig.loc.Y <= 0 && orig.velocity.Y < 0) + { + orig.velocity.Y *= -1; + orig.loc.Y = 0; + } + else if (orig.loc.Y >= (height) && orig.velocity.Y > 0) + { + orig.velocity.Y *= -1; + orig.loc.Y = (720 - 100); + } + + if (orig.loc.X <= 0 && orig.velocity.X < 0) + { + orig.velocity.X *= -1; + orig.loc.X = 0; + + if (score) + { + playerScore += 1; + } + } + else if (orig.loc.X >= width && orig.velocity.X > 0) + { + orig.velocity.X *= -1; + orig.loc.X = width; + + if (score) + { + aiScore += 1; + } + } + + return orig; + } + + + + private FNAGame() + { + GraphicsDeviceManager gdm = new GraphicsDeviceManager(this) + { + + // Typically you would load a config here... + PreferredBackBufferWidth = width, + PreferredBackBufferHeight = 720, + IsFullScreen = false, + SynchronizeWithVerticalRetrace = true + }; + //gdm.SynchronizeWithVerticalRetrace = false; + IsFixedTimeStep = false; + + Content.RootDirectory = "Content"; + + + } + + protected override void Initialize() + { + /* This is a nice place to start up the engine, after + * loading configuration stuff in the constructor + */ + + base.Initialize(); + } + + protected override void LoadContent() + { + // Load textures, sounds, and so on in here... + // Create the batch... + batch = new SpriteBatch(GraphicsDevice); + + // ... then load a texture from ./Content/FNATexture.png + texture = Content.Load("FNATexture"); + paddle = Content.Load("paddle"); + ball = Content.Load("ball"); + sound = Content.Load("FNASound"); + music = Content.Load("IfImWrong"); + Tile.TileSetTexture = Content.Load(@"part4_tileset"); + + + Line.initialize(GraphicsDevice); + + + + var fontBakeResult = TtfFontBaker.Bake(File.OpenRead(@"Content/DroidSans.ttf"), + 25, + 1024, + 1024, + new[] + { + CharacterRange.BasicLatin, + CharacterRange.Latin1Supplement, + CharacterRange.LatinExtendedA, + CharacterRange.Cyrillic + } + ); + + font = fontBakeResult.CreateSpriteFont(GraphicsDevice); + //DynamicSpriteFont font = DynamicSpriteFont.FromTtf(File.ReadAllBytes(@"Content/DroidSans.ttf"), 20); + + } + + protected override void UnloadContent() + { + // Clean up after yourself! + batch.Dispose(); + texture.Dispose(); + sound.Dispose(); + music.Dispose(); + } + + + protected float Decrement(float value, float delta) + { + float magnitude = Math.Abs(value); + + //If distance from zero is less than our delta, + //go to zero to prevent overshooting: + if (magnitude < delta) + { + return 0.0f; + } + else if (value > 0) + { + return value - delta; + } + else if (value < 0) + { + return value + delta; + } + else + { + return 0.0f; + } + } + + protected override void Update(GameTime gameTime) + { + + float volume = 1.0f; + float pitch = 0.0f; + float pan = 0.0f; + + // Run game logic in here. Do NOT render anything here! + KeyboardState keyboardCur = Keyboard.GetState(); + if (keyboardCur.IsKeyDown(Keys.Q) && keyboardPrev.IsKeyUp(Keys.Q)) + { + System.Console.WriteLine("Quitting"); + Environment.Exit(0); + } + + + if (keyboardCur.IsKeyDown(Keys.Space) && keyboardPrev.IsKeyUp(Keys.Space)) + { + sound.Play(volume, pitch, pan); + } + //if (keyboardCur.IsKeyDown(Keys.P) && keyboardPrev.IsKeyUp(Keys.P)) + // { + // this.player_state = !this.player_state; + // } + + //if (player_state ) + //{ + // MediaPlayer.Play(music); + //} + + + elapsedTime += gameTime.ElapsedGameTime; + + if (elapsedTime > TimeSpan.FromSeconds(1)) + { + elapsedTime -= TimeSpan.FromSeconds(1); + frameRate = frameCounter; + frameCounter = 0; + } + + base.Update(gameTime); + + + } + + protected override void Draw(GameTime gameTime) + { + // Render stuff in here. Do NOT run game logic in here! + + frameCounter++; + + string fps = string.Format("fps: {0}", frameRate); + + + + + GraphicsDevice.Clear(Color.CornflowerBlue); + batch.Begin(); + + Line.drawLine(batch, new Vector2(0, 1), new Vector2(1, 50), Color.Black); + Line.drawLine(batch, new Vector2(0, 50), new Vector2(1, 100), Color.White); + Line.drawLine(batch, new Vector2(500, 500), new Vector2(500, 1000), Color.White); + + + //New tile stuff + Vector2 firstSquare = new Vector2(Camera.Location.X / 32, + Camera.Location.Y / 32); + int firstX = (int)firstSquare.X; + int firstY = (int)firstSquare.Y; + + Vector2 squareOffset = new Vector2(Camera.Location.X % 32, + Camera.Location.Y % 32); + + int offsetX = (int)squareOffset.X; + int offsetY = (int)squareOffset.Y; + + + for (int y = 0; y < this.squaresDown; y++) + { + int rowOffset = 0; + if ((firstY + y) % 2 == 1) + rowOffset = Tile.OddRowXOffset; + + for (int x = 0; x < this.squaresAcross; x++) + { + batch.Draw( + Tile.TileSetTexture, + new Rectangle( + (x * Tile.TileStepX) - offsetX + rowOffset + baseOffsetX, + (y * Tile.TileStepY) - offsetY + baseOffsetY, + Tile.TileWidth, Tile.TileHeight), + Tile.GetSourceRectangle(1), + Color.White); + } + } + + //Gridlines + //Lines going down and to the right: + for (int x = (-this.squaresAcross); x < this.squaresAcross; x++) + { + int rowOffset = 0; + + float startX = (x * Tile.TileStepX) + baseOffsetX - (Tile.TileStepX / 2); + + Vector2 start = new Vector2(startX, -baseOffsetY+4); + Vector2 stop = new Vector2(startX + this.squaresAcross* Tile.TileStepX/2, + this.squaresDown*Tile.TileStepY- baseOffsetY+4); + + Line.drawLine(batch, start, stop, Color.White); + + } + //Lines going down and to the left: + for (int x = 0; x < (2*this.squaresAcross); x++) + { + + float startX = (x * Tile.TileStepX) + baseOffsetX - (Tile.TileStepX / 2); + + Vector2 start_reverse = new Vector2(startX, -baseOffsetY + 4); + Vector2 stop_reverse = new Vector2(startX + -(this.squaresAcross * Tile.TileStepX / 2), + (this.squaresDown * Tile.TileStepY) - baseOffsetY + 4); + + Line.drawLine(batch, start_reverse, stop_reverse, Color.White); + + } + + batch.DrawString(font, fps, new Vector2(33, 33), Color.Black); + batch.DrawString(font, fps, new Vector2(32, 32), Color.White); + batch.End(); + + base.Draw(gameTime); + } + + +} \ No newline at end of file diff --git a/isometric-park-fna/Line.cs b/isometric-park-fna/Line.cs --- a/isometric-park-fna/Line.cs +++ b/isometric-park-fna/Line.cs @@ -15,9 +15,14 @@ public static void drawLine(SpriteBatch batch, Vector2 start, Vector2 stop, Color color) { + drawLine(batch, start, stop, color, 1); + } + + public static void drawLine(SpriteBatch batch, Vector2 start, Vector2 stop, Color color, int width) + { Vector2 line = stop - start; float angle = (float)Math.Atan2((double)line.Y, (double) line.X); - Rectangle rect = new Rectangle((int)start.X, (int)start.Y, (int)Math.Round(line.Length()), 1); + Rectangle rect = new Rectangle((int)start.X, (int)start.Y, (int)Math.Round(line.Length()), width); batch.Draw(Line.PixelTexture, rect, null, color, angle, new Vector2(0,0), SpriteEffects.None, 0); } diff --git a/isometric-park-fna/bin/Debug/Content/DroidSans.ttf b/isometric-park-fna/bin/Debug/Content/DroidSans.ttf deleted file mode 100644 index 2537cc3aeea190d4877cfe74ec1cadd229a4cead..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 GIT binary patch literal 0 Hc$@ - - - - - - - - - - - - - - - - - - - diff --git a/isometric-park-fna/bin/Debug/FNA.pdb b/isometric-park-fna/bin/Debug/FNA.pdb deleted file mode 100644 index 2bb4abbede8f9150cc25a74412c2fcf6e64c8c09..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 GIT binary patch literal 0 Hc$@8.0 - + diff --git a/isometric-park-fna/obj/Debug/.NETFramework,Version=v4.7.2.AssemblyAttributes.cs b/isometric-park-fna/obj/Debug/.NETFramework,Version=v4.7.2.AssemblyAttributes.cs deleted file mode 100644 --- a/isometric-park-fna/obj/Debug/.NETFramework,Version=v4.7.2.AssemblyAttributes.cs +++ /dev/null @@ -1,4 +0,0 @@ -// -using System; -using System.Reflection; -[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")] diff --git a/isometric-park-fna/obj/Debug/isometric-park-fna.csproj.CopyComplete b/isometric-park-fna/obj/Debug/isometric-park-fna.csproj.CopyComplete deleted file mode 100644 diff --git a/isometric-park-fna/obj/Debug/isometric-park-fna.csproj.CoreCompileInputs.cache b/isometric-park-fna/obj/Debug/isometric-park-fna.csproj.CoreCompileInputs.cache deleted file mode 100644 --- a/isometric-park-fna/obj/Debug/isometric-park-fna.csproj.CoreCompileInputs.cache +++ /dev/null @@ -1,1 +0,0 @@ -524d27a21584321321e63091a9c064e9e3759444 diff --git a/isometric-park-fna/obj/Debug/isometric-park-fna.csproj.FileListAbsolute.txt b/isometric-park-fna/obj/Debug/isometric-park-fna.csproj.FileListAbsolute.txt deleted file mode 100644 --- a/isometric-park-fna/obj/Debug/isometric-park-fna.csproj.FileListAbsolute.txt +++ /dev/null @@ -1,21 +0,0 @@ -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/FNA.dll.config -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/Content/FNATexture.png -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/Content/FNASound.wav -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/Content/IfImWrong.mp3 -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/Content/Menlo.ttf -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/Content/DroidSans.ttf -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/Content/ball.png -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/Content/paddle.png -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/Content/part4_tileset.png -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/Content/IfImWrong.ogg -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/isometric-park-fna.exe -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/isometric-park-fna.pdb -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/FNA.dll -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/SpriteFontPlus.dll -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/FNA.pdb -/Users/alys/repos/isometric-park-fna/isometric-park-fna/bin/Debug/SpriteFontPlus.pdb -/Users/alys/repos/isometric-park-fna/isometric-park-fna/obj/Debug/isometric-park-fna.csprojAssemblyReference.cache -/Users/alys/repos/isometric-park-fna/isometric-park-fna/obj/Debug/isometric-park-fna.csproj.CoreCompileInputs.cache -/Users/alys/repos/isometric-park-fna/isometric-park-fna/obj/Debug/isometric-park-fna.csproj.CopyComplete -/Users/alys/repos/isometric-park-fna/isometric-park-fna/obj/Debug/isometric-park-fna.exe -/Users/alys/repos/isometric-park-fna/isometric-park-fna/obj/Debug/isometric-park-fna.pdb diff --git a/isometric-park-fna/obj/Debug/isometric-park-fna.csprojAssemblyReference.cache b/isometric-park-fna/obj/Debug/isometric-park-fna.csprojAssemblyReference.cache deleted file mode 100644 index 80b008bdf0811ff31bfe2e6ce8b5e2e368af5469..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 GIT binary patch literal 0 Hc$@