summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoredGuy <osome3717@gmail.com>2026-01-27 07:35:38 +0300
committerBoredGuy <osome3717@gmail.com>2026-01-27 07:35:38 +0300
commit57920a063e889fc024cee8e8f58a476c3dfc1b93 (patch)
tree8bd9308278c9fc269c3a60f6821000992ed77d1d
parentc18e714aebab35442347cd114bad66a014d8f7ca (diff)
Smoth step movement doeHEADmaster
-rw-r--r--DungeonSlime/Content/Content.mgcb19
-rw-r--r--DungeonSlime/Game1.cs173
-rw-r--r--MonoGameLibrary/Core.cs2
3 files changed, 121 insertions, 73 deletions
diff --git a/DungeonSlime/Content/Content.mgcb b/DungeonSlime/Content/Content.mgcb
index 15b8a51..ddc4c36 100644
--- a/DungeonSlime/Content/Content.mgcb
+++ b/DungeonSlime/Content/Content.mgcb
@@ -13,22 +13,3 @@
#---------------------------------- Content ---------------------------------#
-#begin Fonts/Ariel.spritefont
-/importer:FontDescriptionImporter
-/processor:FontDescriptionProcessor
-/processorParam:PremultiplyAlpha=True
-/processorParam:TextureFormat=Compressed
-/build:Fonts/Ariel.spritefont
-
-#begin Images/logo.png
-/importer:TextureImporter
-/processor:TextureProcessor
-/processorParam:ColorKeyColor=255,0,255,255
-/processorParam:ColorKeyEnabled=True
-/processorParam:GenerateMipmaps=False
-/processorParam:PremultiplyAlpha=True
-/processorParam:ResizeToPowerOfTwo=False
-/processorParam:MakeSquare=False
-/processorParam:TextureFormat=Color
-/build:Images/logo.png
-
diff --git a/DungeonSlime/Game1.cs b/DungeonSlime/Game1.cs
index abb6559..88bd262 100644
--- a/DungeonSlime/Game1.cs
+++ b/DungeonSlime/Game1.cs
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
-using System.Net;
-using System.Runtime.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
@@ -30,17 +28,18 @@ static class DirectionHelper
return direction switch
{
Direction.Up => new Vector2(0, -1),
- Direction.Down => new Vector2(0, 1),
- Direction.Left => new Vector2(-1, 0),
- Direction.Right => new Vector2(1, 0),
- _ => throw new ArgumentOutOfRangeException($"Invalid direction {direction}")
- };
+ Direction.Down => new Vector2(0, 1),
+ Direction.Left => new Vector2(-1, 0),
+ Direction.Right => new Vector2(1, 0),
+ _ => throw new ArgumentOutOfRangeException($"Invalid direction {direction}")
+ };
}
}
struct Segment {
public Vector2 pastPosition;
public Vector2 targetPosition;
+ public Direction heading;
public Vector2 GetPosition(float portionTraveled)
{
@@ -50,8 +49,8 @@ struct Segment {
class Snake
{
- public static readonly Point SegmentSize = new Point(100, 100);
- public Direction Heading { get; set; }
+ public static readonly Point SegmentSize = new Point(50, 50);
+ public Direction NextHeading { get; set; }
List<Segment> tail = [];
Segment head;
@@ -59,30 +58,63 @@ class Snake
Texture2D whitePixel;
public float Speed { get; set; }
- public Snake(Point postionOnGrid, Direction heading, Texture2D whitePixel)
+ public Snake(Point postionOnGrid, Direction heading, Texture2D whitePixel, float startSpeed)
{
- this.Heading = heading;
+ this.NextHeading = heading;
head = new Segment
{
- pastPosition = (postionOnGrid * SegmentSize).ToVector2()
+ pastPosition = (postionOnGrid * SegmentSize).ToVector2(),
+ heading = heading
};
head.targetPosition = head.pastPosition + GetHeadTargetOffset();
tail.Add(new Segment
{
pastPosition = (postionOnGrid * SegmentSize).ToVector2(),
+ heading = heading,
+ targetPosition = head.pastPosition
+ });
+
+ tail.Add(new Segment
+ {
+ pastPosition = (postionOnGrid * SegmentSize).ToVector2(),
+ heading = heading,
targetPosition = head.pastPosition
});
this.whitePixel = whitePixel;
- Speed = 2.0f;
+ Speed = startSpeed;
}
private Vector2 GetHeadTargetOffset()
{
- return DirectionHelper.ToNormalVector(Heading) * SegmentSize.ToVector2();
+ return DirectionHelper.ToNormalVector(head.heading) * SegmentSize.ToVector2();
+ }
+
+ private void AdvanceSnakeStep() {
+ //Snap all segments in tail to next grid position
+ if (tail.Count > 0)
+ {
+ for (int i = tail.Count - 1; i > 0; i--)
+ {
+ tail[i] = tail[i-1];
+ }
+
+ Segment tailSegment = tail[0];
+
+ tailSegment.pastPosition = head.pastPosition;
+ tailSegment.targetPosition = head.targetPosition;
+ tailSegment.heading = head.heading;
+
+ tail[0] = tailSegment;
+ }
+
+ //Start moving head to next grid position
+ head.heading = NextHeading;
+ head.pastPosition = head.targetPosition;
+ head.targetPosition = head.pastPosition + GetHeadTargetOffset();
}
public void Update(float dt)
@@ -90,46 +122,85 @@ class Snake
portionTraveled += Speed * dt;
//Keep leftover for consistent movment
- if (portionTraveled >= 1f)
+ while (portionTraveled >= 1f)
{
portionTraveled -= 1f;
+ AdvanceSnakeStep();
+ }
+ }
- //Snap all segments in tail to next grid position
- if (tail.Count > 0)
- {
- for (int i = tail.Count - 1; i > 0; i--)
- {
- tail[i] = tail[i-1];
- }
+ private Rectangle ClipHorizontal(Rectangle source, Rectangle clip) {
+ int x = source.X, width = source.Width;
- Segment tailSegment = tail[0];
+ if (source.X <= clip.X) {
+ width = clip.Left - source.Left;
+ } else {
+ x = clip.Right;
+ width = source.Right - x;
+ }
- tailSegment.pastPosition = head.pastPosition;
- tailSegment.targetPosition = head.targetPosition;
+ return new Rectangle(x, source.Y, width, source.Height);
+ }
- tail[0] = tailSegment;
- }
+ private Rectangle ClipVertical(Rectangle source, Rectangle clip) {
+ int y = source.Y, height = source.Height;
- //Snap head to next grid position
- head.pastPosition = head.targetPosition;
- head.targetPosition = head.pastPosition + GetHeadTargetOffset();
+ if (source.Y <= clip.Y) {
+ height = clip.Top - source.Top;
+ } else {
+ y = clip.Bottom;
+ height = source.Bottom - y;
}
- }
- public void DrawSegment(Segment s, SpriteBatch spriteBatch)
- {
- spriteBatch.Draw(
- whitePixel,
- new Rectangle(
- s.GetPosition(portionTraveled).ToPoint(),
- SegmentSize
- ),
- Color.White
- );
+ return new Rectangle(source.X, y, source.Width, height);
}
public void Draw(SpriteBatch spriteBatch)
{
+ var pastSegment = head;
+ foreach (var i in tail)
+ {
+ spriteBatch.Draw(
+ whitePixel,
+ new Rectangle(
+ i.GetPosition(portionTraveled).ToPoint(),
+ SegmentSize
+ ),
+ Color.White
+ );
+
+ Rectangle trail = new Rectangle(
+ pastSegment.pastPosition.ToPoint(),
+ SegmentSize
+ );
+ Rectangle pastSegmentBounds = new Rectangle(
+ pastSegment.GetPosition(portionTraveled).ToPoint(),
+ SegmentSize
+ );
+
+ Rectangle trailTrimmed;
+ if (pastSegment.heading == Direction.Left || pastSegment.heading == Direction.Right) {
+ trailTrimmed = ClipHorizontal(
+ source: trail,
+ clip: pastSegmentBounds
+ );
+ } else {
+ trailTrimmed = ClipVertical(
+ source: trail,
+ clip: pastSegmentBounds
+ );
+ }
+
+ //Draw last segments trail for smooth movement
+ spriteBatch.Draw(
+ whitePixel,
+ trailTrimmed,
+ Color.White
+ );
+
+ pastSegment = i;
+ }
+
spriteBatch.Draw(
whitePixel,
new Rectangle(
@@ -138,22 +209,18 @@ class Snake
),
Color.Red
);
-
- foreach (var i in tail)
- {
-
- }
}
}
public class Game1 : Core
{
+ private static readonly float StartSpeed = 3.0f;
Texture2D whitePixel;
Snake snake;
- public Game1() : base("Reaction Test", 1280, 720, false)
+ public Game1() : base("Smooth Snake Movement", 1280, 720, false)
{
-
+
}
protected override void Initialize()
@@ -163,7 +230,7 @@ public class Game1 : Core
whitePixel = new Texture2D(GraphicsDevice, 1, 1);
whitePixel.SetData<Color>([Color.White]);
- snake = new Snake(new Point(2, 2), Direction.Right, whitePixel);
+ snake = new Snake(new Point(2, 2), Direction.Right, whitePixel, StartSpeed);
}
protected override void LoadContent()
@@ -176,16 +243,16 @@ public class Game1 : Core
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
- snake.Heading = Direction.Down;
+ snake.NextHeading = Direction.Down;
} else if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
- snake.Heading = Direction.Up;
+ snake.NextHeading = Direction.Up;
} else if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
- snake.Heading = Direction.Left;
+ snake.NextHeading = Direction.Left;
} else if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
- snake.Heading = Direction.Right;
+ snake.NextHeading = Direction.Right;
}
snake.Update((float)gameTime.ElapsedGameTime.TotalSeconds);
diff --git a/MonoGameLibrary/Core.cs b/MonoGameLibrary/Core.cs
index 1f76b51..fddcc0c 100644
--- a/MonoGameLibrary/Core.cs
+++ b/MonoGameLibrary/Core.cs
@@ -29,7 +29,7 @@ public class Core : Game {
Graphics.PreferredBackBufferWidth = width;
Graphics.PreferredBackBufferHeight = height;
Graphics.IsFullScreen = fullscreen;
- Graphics.SynchronizeWithVerticalRetrace = false;
+ Graphics.SynchronizeWithVerticalRetrace = true;
Graphics.ApplyChanges();