Skip to content

Commit

Permalink
Refactors and sets stage for collision detection (#36)
Browse files Browse the repository at this point in the history
* Fixes crash

* Adds collision system

* refactors

* refactors names
  • Loading branch information
MaxEdwards20 authored Apr 7, 2024
1 parent bae25f9 commit 1c88f68
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/Client/ClientMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using System.Collections.Generic;
using Client.Components;
using Client.Systems;
using Shared.Components;
using Shared.Systems;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
using System.IO.IsolatedStorage;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;
using Shared.Components;

namespace Shared.Components
namespace Client.Components
{
[DataContract(Name = "Controls")]
public class Controls : Component
Expand Down
12 changes: 6 additions & 6 deletions src/Client/GameModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

using Client.Components;
using Shared.Components;
using Shared.Entities;

Expand All @@ -21,7 +21,7 @@ public class GameModel
private Systems.KeyboardInput m_systemKeyboardInput;
private Systems.MouseInput m_systemMouseInput;
private Systems.Interpolation m_systemInterpolation;
private Systems.WormRenderer _mSystemWormRenderer;
private Systems.WormRenderer m_systemWormRenderer;
private Controls m_controls;
private GraphicsDeviceManager m_graphics;
private SpriteFont m_font;
Expand All @@ -44,7 +44,7 @@ public void update(TimeSpan elapsedTime)

public void render(TimeSpan elapsedTime, SpriteBatch spriteBatch)
{
_mSystemWormRenderer.render(elapsedTime, spriteBatch);
m_systemWormRenderer.render(elapsedTime, spriteBatch);
}

/// <summary>
Expand All @@ -59,7 +59,7 @@ public bool initialize(ContentManager contentManager, Controls controls, Graphic
m_entities = new Dictionary<uint, Entity>();
m_systemInterpolation = new Systems.Interpolation();
m_systemCamera = new Systems.Camera(new Vector2(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight));
_mSystemWormRenderer = new Systems.WormRenderer(m_systemCamera, graphics, m_font);
m_systemWormRenderer = new Systems.WormRenderer(m_systemCamera, graphics, m_font);
m_systemNetwork = new Systems.Network();

m_systemNetwork.registerNewEntityHandler(handleNewEntity);
Expand Down Expand Up @@ -166,7 +166,7 @@ private void addEntity(Entity entity)
m_entities[entity.id] = entity;
m_systemKeyboardInput.add(entity);
m_systemMouseInput.add(entity);
_mSystemWormRenderer.add(entity);
m_systemWormRenderer.add(entity);
m_systemNetwork.add(entity);
m_systemInterpolation.add(entity);
m_systemCamera.add(entity);
Expand All @@ -183,7 +183,7 @@ private void removeEntity(uint id)
m_systemKeyboardInput.remove(id);
m_systemMouseInput.remove(id);
m_systemNetwork.remove(id);
_mSystemWormRenderer.remove(id);
m_systemWormRenderer.remove(id);
m_systemInterpolation.remove(id);
m_systemCamera.remove(id);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Client/Menu/ControlSettingsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Shared.Components;
using Shared.Entities;
using Shared.Systems;
using Client.Components;
using Client.Systems;

namespace Client.Menu
{
Expand All @@ -21,7 +23,7 @@ public class ControlSettingsView : GameStateView
private ControlStateEnum updatingKey = ControlStateEnum.None;
private bool isUpdatingKey = false;
private Controls m_controls;
private ControlsPersistence _mControlsPersistence = new ControlsPersistence();
private ControlsPersistence m_controlsPersistence = new ControlsPersistence();

public enum ControlStateEnum
{
Expand Down Expand Up @@ -81,7 +83,7 @@ public override void update(GameTime gameTime)
break;
}
// Now we persist any changes
_mControlsPersistence.SaveControls(m_controls);
m_controlsPersistence.SaveControls(m_controls);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Client/Menu/GamePlayView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Xna.Framework.Graphics;
using Client.IO;
using System.Runtime.Serialization;
using Client.Components;
using Microsoft.Xna.Framework.Input;
using Shared.Components;

Expand Down
1 change: 1 addition & 0 deletions src/Client/Menu/MenuKeyboardInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Client.Menu;
using Shared.Components;
using Client.Components;


namespace Client.IO
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using System;
using System.IO;
using Microsoft.Xna.Framework.Input;
using Shared.Components;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;
using Client.Components;


namespace Shared.Systems;
namespace Client.Systems;

public class ControlsPersistence: System
public class ControlsPersistence: Shared.Systems.System
{
// Now we want to make all of these control settings persist across game sessions. We will use the same serialization technique we used for the high scores.
private bool saving = false;
Expand Down
1 change: 1 addition & 0 deletions src/Client/Systems/KeyboardInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using Shared.Components;
using Client.Components;

namespace Client.Systems
{
Expand Down
1 change: 1 addition & 0 deletions src/Client/Systems/MouseInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Shared.Entities;
using System;
using System.Collections.Generic;
using Client.Components;
using Shared.Components;

namespace Client.Systems
Expand Down
7 changes: 5 additions & 2 deletions src/Client/Systems/WormMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Client.Systems;

public class WormMovement : Shared.Systems.System
public class WormMovement : Shared.Systems.System
{
public override void update(TimeSpan elapsedTime)
public WormMovement() : base()
{
}
public override void update(TimeSpan elapsedTime)
{
}
}
47 changes: 47 additions & 0 deletions src/Server/Systems/CollisionDetection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.Xna.Framework;

namespace Server.Systems;

public class CollisionDetection : Shared.Systems.System
{
public CollisionDetection() :
base(
typeof(Shared.Components.Collision)
)
{
}


public override void update(TimeSpan elapsedTime)
{
throw new NotImplementedException();
}

// Reference: https://stackoverflow.com/questions/37224912/circle-line-segment-collision
private static bool CircleLineIntersect(Point pt1, Point pt2, float circleRadius, Vector2 circlePosition)
{
Vector2 v1 = new Vector2((float)(pt2.X - pt1.X), (float)(pt2.X - pt1.X));
Vector2 v2 = new Vector2((float) pt1.X - circlePosition.X, (float)(pt1.X - circlePosition.Y));
float b = -2 * (v1.X * v2.X + v1.Y * v2.Y);
float c = 2 * (v1.X * v1.X + v1.Y * v1.Y);
float d = (float)Math.Sqrt(b * b - 2 * c * (v2.X * v2.X + v2.Y * v2.Y - circleRadius * circleRadius));
if (float.IsNaN(d)) // no intercept
{
return false;
}
// These represent the unit distance of point one and two on the line
float u1 = (b - d) / c;
float u2 = (b + d) / c;
if (u1 <= 1 && u1 >= 0) // If point on the line segment
{
return true;
}
if (u2 <= 1 && u2 >= 0) // If point on the line segment
{
return true;
}
return false;
}
}


9 changes: 6 additions & 3 deletions src/Shared/Entities/Spice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ namespace Shared.Entities;

public class Spice
{
public Entity create(Color color, Vector2 position, float size)
private Random random = new Random();
public Entity create(Color color, Vector2 position)
{
int power = random.Next(1, 10);
float size = 10;
Entity entity = new Entity();
entity.add(new Appearance("Textures/spice")); // TODO: Make this a spice texture
entity.add(new Position(position));
entity.add(new Size(new Vector2(size, size)));
entity.add(new Size(new Vector2(size * power, size * power)));
entity.add(new Collision());
entity.add(new SpicePower(0));
entity.add(new SpicePower(power));
return entity;
}
}

0 comments on commit 1c88f68

Please sign in to comment.