Skip to content

Commit ac3b6b2

Browse files
author
psilo
committed
Removed references to external utility-library.
Added nuget package MonoGame.Extended. Replaced some methods because of that change.
1 parent ef34251 commit ac3b6b2

File tree

13 files changed

+93
-74
lines changed

13 files changed

+93
-74
lines changed

CollisionGrid/CollisionGrid.Point.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void Add(T item, Point cell)
7575
{
7676
lock (lockObject)
7777
{
78-
var c = Clamp(cell);
78+
Point c = Clamp(cell);
7979
AddToGrid(item, c);
8080
AddToItems(item, c);
8181
}
@@ -142,13 +142,13 @@ public void Remove(Point cell)
142142
{
143143
lock (lockObject)
144144
{
145-
var c = Clamp(cell);
145+
Point c = Clamp(cell);
146146
List<T> l;
147147
Grid.TryGetValue(c, out l);
148148

149149
if (l != null)
150150
{
151-
foreach (var i in l)
151+
foreach (T i in l)
152152
{
153153
List<Point> pl;
154154
ItemDictionary.TryGetValue(i, out pl);

CollisionGrid/CollisionGrid.Rect.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
// For more information, please refer to <http://unlicense.org>
2626
// ***************************************************************************
2727

28-
using Utilities.Geometry;
28+
using MonoGame.Extended.Shapes;
2929

3030
namespace CollisionGrid
3131
{
3232
public partial class CollisionGrid<T>
3333
{
34-
public T[] Get(Rect aabb)
34+
public T[] Get(RectangleF aabb)
3535
{
3636
lock (lockObject)
3737
{
@@ -46,7 +46,7 @@ public T[] Get(Rect aabb)
4646
/// <returns>
4747
/// The item or default(T)
4848
/// </returns>
49-
public T First(Rect aabb)
49+
public T First(RectangleF aabb)
5050
{
5151
lock (lockObject)
5252
{
@@ -60,7 +60,7 @@ public T First(Rect aabb)
6060
/// </summary>
6161
/// <param name="item">The item to add</param>
6262
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in float-grid-coordinates.</param>
63-
public void Add(T item, Rect aabb)
63+
public void Add(T item, RectangleF aabb)
6464
{
6565
lock (lockObject)
6666
{
@@ -73,7 +73,7 @@ public void Add(T item, Rect aabb)
7373
/// If the items don't occupy another cell, they are removed as well.
7474
/// </summary>
7575
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in float-grid-coordinates.</param>
76-
public void Remove(Rect aabb)
76+
public void Remove(RectangleF aabb)
7777
{
7878
lock (lockObject)
7979
{
@@ -88,15 +88,15 @@ public void Remove(Rect aabb)
8888
/// </summary>
8989
/// <param name="item">The item to move</param>
9090
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in float-grid-coordinates.</param>
91-
public void Move(T item, Rect aabb)
91+
public void Move(T item, RectangleF aabb)
9292
{
9393
lock (lockObject)
9494
{
9595
Move(item, Rectangle(aabb));
9696
}
9797
}
9898

99-
public bool IsEmpty(Rect aabb)
99+
public bool IsEmpty(RectangleF aabb)
100100
{
101101
lock (lockObject)
102102
{

CollisionGrid/CollisionGrid.Rectangle.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ public partial class CollisionGrid<T>
3737

3838
private void FillList(Rectangle aabb)
3939
{
40-
var r = Clamp(aabb);
40+
Rectangle r = Clamp(aabb);
4141
lop.Clear();
42-
for (var y = 0; y < r.Size.Y; y++)
42+
for (int y = 0; y < r.Size.Y; y++)
4343
{
44-
for (var x = 0; x < r.Size.X; x++)
44+
for (int x = 0; x < r.Size.X; x++)
4545
{
4646
lop.Add(new Point(r.X + x, r.Y + y));
4747
}
@@ -54,9 +54,9 @@ public T[] Get(Rectangle aabb)
5454
{
5555
FillList(aabb);
5656
result.Clear();
57-
foreach (var p in lop)
57+
foreach (Point p in lop)
5858
{
59-
foreach (var i in Get(p))
59+
foreach (T i in Get(p))
6060
{
6161
if (!result.Contains(i))
6262
{
@@ -81,9 +81,9 @@ public T First(Rectangle aabb)
8181
{
8282
FillList(aabb);
8383
result.Clear();
84-
foreach (var p in lop)
84+
foreach (Point p in lop)
8585
{
86-
var content = First(p);
86+
T content = First(p);
8787
if (!content.Equals(default(T)))
8888
{
8989
return content;
@@ -104,7 +104,7 @@ public void Add(T item, Rectangle aabb)
104104
lock (lockObject)
105105
{
106106
FillList(aabb);
107-
foreach (var p in lop)
107+
foreach (Point p in lop)
108108
{
109109
Add(item, p);
110110
}
@@ -121,7 +121,7 @@ public void Remove(Rectangle aabb)
121121
lock (lockObject)
122122
{
123123
FillList(aabb);
124-
foreach (var p in lop)
124+
foreach (Point p in lop)
125125
{
126126
Remove(p);
127127
}
@@ -141,7 +141,7 @@ public void Move(T item, Rectangle aabb)
141141
{
142142
Remove(item);
143143
FillList(aabb);
144-
foreach (var p in lop)
144+
foreach (Point p in lop)
145145
{
146146
Add(item, p);
147147
}
@@ -153,7 +153,7 @@ public bool IsEmpty(Rectangle aabb)
153153
lock (lockObject)
154154
{
155155
FillList(aabb);
156-
foreach (var p in lop)
156+
foreach (Point p in lop)
157157
{
158158
if (!IsEmpty(p))
159159
{

CollisionGrid/CollisionGrid.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
// ***************************************************************************
2727

2828
using System.Collections.Generic;
29+
using JetBrains.Annotations;
2930
using Microsoft.Xna.Framework;
30-
using Utilities.Geometry;
31+
using MonoGame.Extended.Shapes;
3132

3233
namespace CollisionGrid
3334
{
35+
[PublicAPI]
3436
public partial class CollisionGrid<T>
3537
{
3638
private readonly object lockObject = new object();
@@ -94,7 +96,7 @@ public void Remove(T item)
9496
return;
9597
}
9698

97-
foreach (var p in pl)
99+
foreach (Point p in pl)
98100
{
99101
RemoveFromGrid(item, p);
100102
}
@@ -128,11 +130,11 @@ private void RemoveFromGrid(T item, Point cell)
128130

129131
public int ItemCount => Grid.Keys.Count;
130132

131-
private Rectangle Rectangle(Rect rect)
133+
private Rectangle Rectangle(RectangleF rect)
132134
{
133-
var tl = Cell(rect.TopLeft);
134-
var br = Cell(rect.BottomRight);
135-
var s = br - tl + new Point(1, 1);
135+
Point tl = Cell(rect.Location);
136+
Point br = Cell(rect.Location + rect.Size);
137+
Point s = br - tl + new Point(1, 1);
136138
return new Rectangle(tl, s);
137139
}
138140

@@ -143,15 +145,15 @@ private Point Cell(Vector2 position)
143145

144146
private Rectangle Clamp(Rectangle rectangle)
145147
{
146-
var tl = Clamp(rectangle.Location);
147-
var br = Clamp(rectangle.Location + rectangle.Size - new Point(1, 1));
148-
var s = br - tl + new Point(1, 1);
148+
Point tl = Clamp(rectangle.Location);
149+
Point br = Clamp(rectangle.Location + rectangle.Size - new Point(1, 1));
150+
Point s = br - tl + new Point(1, 1);
149151
return new Rectangle(tl, s);
150152
}
151153

152154
private Point Clamp(Point p)
153155
{
154-
var nx = p.X;
156+
int nx = p.X;
155157
if (nx >= NumberOfCellsX)
156158
{
157159
nx = NumberOfCellsX - 1;
@@ -161,7 +163,7 @@ private Point Clamp(Point p)
161163
nx = 0;
162164
}
163165

164-
var ny = p.Y;
166+
int ny = p.Y;
165167
if (ny >= NumberOfCellsY)
166168
{
167169
ny = NumberOfCellsY - 1;

CollisionGrid/CollisionGrid.csproj

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@
3030
<WarningLevel>4</WarningLevel>
3131
</PropertyGroup>
3232
<ItemGroup>
33+
<Reference Include="JetBrains.Annotations, Version=10.1.4.0, Culture=neutral, PublicKeyToken=1010a0d8d6380325, processorArchitecture=MSIL">
34+
<HintPath>..\packages\JetBrains.Annotations.10.1.4\lib\net20\JetBrains.Annotations.dll</HintPath>
35+
<Private>True</Private>
36+
</Reference>
37+
<Reference Include="MonoGame.Extended, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
38+
<HintPath>..\packages\MonoGame.Extended.0.5.149\lib\MonoGame.Extended.dll</HintPath>
39+
<Private>True</Private>
40+
</Reference>
3341
<Reference Include="MonoGame.Framework">
3442
<HintPath>..\Test\_EXTERNAL_LIBRARIES\MonoGame\MonoGame.Framework.dll</HintPath>
3543
</Reference>
@@ -40,9 +48,6 @@
4048
<Reference Include="Microsoft.CSharp" />
4149
<Reference Include="System.Data" />
4250
<Reference Include="System.Xml" />
43-
<Reference Include="Utilities">
44-
<HintPath>_EXTERNAL_LIBRARIES\Utilities.dll</HintPath>
45-
</Reference>
4651
</ItemGroup>
4752
<ItemGroup>
4853
<Compile Include="CollisionGrid.Rect.cs" />
@@ -53,7 +58,7 @@
5358
<Compile Include="Properties\AssemblyInfo.cs" />
5459
</ItemGroup>
5560
<ItemGroup>
56-
<Content Include="_EXTERNAL_LIBRARIES\Utilities.dll" />
61+
<None Include="packages.config" />
5762
</ItemGroup>
5863
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5964
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
-211 KB
Binary file not shown.

CollisionGrid/packages.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="JetBrains.Annotations" version="10.1.4" targetFramework="net45" />
4+
<package id="MonoGame.Extended" version="0.5.149" targetFramework="net45" />
5+
<package id="System.Runtime" version="4.0.0" targetFramework="net45" />
6+
</packages>

Test/DrawableGrid.cs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
// For more information, please refer to <http://unlicense.org>
2626
// ***************************************************************************
2727

28+
using System;
2829
using System.Collections.Generic;
2930
using CollisionGrid;
3031
using Microsoft.Xna.Framework;
3132
using Microsoft.Xna.Framework.Graphics;
32-
using Utilities;
33-
using Utilities.Randomizing;
33+
using MonoGame.Extended.Shapes;
3434

3535
namespace Test
3636
{
@@ -58,16 +58,16 @@ public DrawableGrid(Game game, SpriteBatch spriteBatch, float width, float heigh
5858
public override void Initialize()
5959
{
6060
base.Initialize();
61-
var rand = RandomizerController.Randomizer;
62-
for (var i = 0; i < NUMBER_OF_SPRITES; i++)
61+
Random rand = new Random(1);
62+
for (int i = 0; i < NUMBER_OF_SPRITES; i++)
6363
{
64-
var s = new Sprite(Game, SpriteBatch, new Point((int) width, (int) height));
65-
s.Trajectory = new Vector2(rand.RandomBetween(-1f, 1f), rand.RandomBetween(-1f, 1f));
64+
Sprite s = new Sprite(Game, SpriteBatch, new Point((int) width, (int) height));
65+
s.Trajectory = new Vector2(rand.Next(-1000, 1000)/1000f, rand.Next(-1000, 1000)/1000f);
6666
s.Trajectory.Normalize();
67-
s.Position = new Vector2(rand.RandomBetween(0f, 700f), rand.RandomBetween(0f, 700f));
68-
s.Velocity = rand.RandomBetween(.1f, 4f);
69-
s.Width = rand.RandomBetween(5, 53);
70-
s.Height = rand.RandomBetween(5, 53);
67+
s.Position = new Vector2(rand.Next(0, 700), rand.Next(0, 700));
68+
s.Velocity = rand.Next(100, 4000)/1000f;
69+
s.Width = rand.Next(5, 53);
70+
s.Height = rand.Next(5, 53);
7171

7272
s.Initialize();
7373
Game.Components.Add(s);
@@ -78,7 +78,7 @@ public override void Initialize()
7878
public override void Update(GameTime gameTime)
7979
{
8080
base.Update(gameTime);
81-
foreach (var s in sprites)
81+
foreach (Sprite s in sprites)
8282
{
8383
Grid.Move(s, s.GetAabb());
8484
}
@@ -89,28 +89,34 @@ public override void Draw(GameTime gameTime)
8989
if (Visible)
9090
{
9191
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
92-
var r = new Rectangle((int) Position.X, (int) Position.Y, (int) width, (int) height);
92+
Rectangle r = new Rectangle((int) Position.X, (int) Position.Y, (int) width, (int) height);
9393
SpriteBatch.DrawRectangle(r, Color.Yellow);
94-
for (var x = 0; x < Grid.NumberOfCellsX; x++)
94+
for (int x = 0; x < Grid.NumberOfCellsX; x++)
9595
{
96-
for (var y = 0; y < Grid.NumberOfCellsY; y++)
96+
for (int y = 0; y < Grid.NumberOfCellsY; y++)
9797
{
98-
var cell = new Rectangle((int) (Position.X + x*Grid.CellWidth),
98+
Rectangle cell = new Rectangle((int) (Position.X + x*Grid.CellWidth),
9999
(int) (Position.Y + y*Grid.CellHeight),
100100
(int) Grid.CellWidth, (int) Grid.CellHeight);
101-
var l = Grid.Get(new Point(x, y)).Length;
101+
int l = Grid.Get(new Point(x, y)).Length;
102+
103+
Color c;
102104
if (l > 0)
103105
{
104-
var f = l/4f;
106+
float f = l/4f;
105107
if (f > 1)
106108
{
107109
f = 1;
108110
}
109-
SpriteBatch.FillRectangle(cell, Utils.SetTransparencyOnColor(Color.Red, f));
111+
c = Color.Red;
112+
c.A = (byte) (255f*f);
113+
SpriteBatch.FillRectangle(cell, c);
110114
}
111115
else
112116
{
113-
SpriteBatch.DrawRectangle(cell, Utils.SetTransparencyOnColor(Color.Yellow, .5f));
117+
c = Color.Yellow;
118+
c.A = (byte) (255f*.5f);
119+
SpriteBatch.DrawRectangle(cell, c);
114120
}
115121
}
116122
}

Test/Main.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
using System;
2929
using Microsoft.Xna.Framework;
3030
using Microsoft.Xna.Framework.Graphics;
31-
using Utilities;
3231

3332
namespace Test
3433
{
@@ -47,19 +46,14 @@ public Main()
4746
GraphicsDeviceManager.PreferredBackBufferWidth = MIN_SCREEN_RESOLUTION_WIDTH;
4847
GraphicsDeviceManager.PreferredBackBufferHeight = MIN_SCREEN_RESOLUTION_HEIGHT;
4948
GraphicsDeviceManager.IsFullScreen = false;
49+
50+
Window.AllowUserResizing = true;
51+
Window.Position = Point.Zero;
5052
}
5153

5254
protected override void Initialize()
5355
{
54-
var screenBounds = Utils.InitGraphicsMode(this, GraphicsDeviceManager, MIN_SCREEN_RESOLUTION_WIDTH,
55-
MIN_SCREEN_RESOLUTION_HEIGHT, false);
56-
if (screenBounds == null)
57-
{
58-
Console.Out.WriteLine("Severe error opening and initializing window.");
59-
Exit();
60-
}
61-
62-
var grid = new DrawableGrid(this, new SpriteBatch(GraphicsDevice), 700, 700, 40, 40);
56+
DrawableGrid grid = new DrawableGrid(this, new SpriteBatch(GraphicsDevice), 700, 700, 40, 40);
6357
Components.Add(grid);
6458
grid.Initialize();
6559
}

0 commit comments

Comments
 (0)