diff --git a/CollisionGrid.sln.DotSettings b/CollisionGrid.sln.DotSettings new file mode 100644 index 0000000..70cd1e4 --- /dev/null +++ b/CollisionGrid.sln.DotSettings @@ -0,0 +1,27 @@ + + *************************************************************************** +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to <http://unlicense.org> +*************************************************************************** \ No newline at end of file diff --git a/CollisionGrid/CollisionGrid.Point.cs b/CollisionGrid/CollisionGrid.Point.cs index d2055a5..70e7ca9 100644 --- a/CollisionGrid/CollisionGrid.Point.cs +++ b/CollisionGrid/CollisionGrid.Point.cs @@ -1,183 +1,195 @@ // *************************************************************************** -// Copyright (c) 2016 by Unterrainer Informatik OG. -// This source is licensed to Unterrainer Informatik OG. -// All rights reserved. -// -// In other words: -// YOU MUST NOT COPY, USE, CHANGE OR REDISTRIBUTE ANY ART, MUSIC, CODE OR -// OTHER DATA, CONTAINED WITHIN THESE DIRECTORIES WITHOUT THE EXPRESS -// PERMISSION OF Unterrainer Informatik OG. -// --------------------------------------------------------------------------- -// Programmer: G U, -// Created: 2016-03-30 +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to // *************************************************************************** using System.Collections.Generic; -using System.Linq; using Microsoft.Xna.Framework; namespace CollisionGrid { - public partial class CollisionGrid - { - public T[] Get(Point cell) - { - lock (lockObject) - { - List contents; - Grid.TryGetValue(Clamp(cell), out contents); - if (contents == null) - { - return new T[0]; - } - return contents.ToArray(); - } - } + public partial class CollisionGrid + { + public T[] Get(Point cell) + { + lock (lockObject) + { + List contents; + Grid.TryGetValue(Clamp(cell), out contents); + if (contents == null) + { + return new T[0]; + } + return contents.ToArray(); + } + } - /// - /// Gets the first item encountered on the given cell. - /// - /// The cell to search - /// The item or default(T) - public T First(Point cell) - { - lock (lockObject) - { - List contents; - Grid.TryGetValue(Clamp(cell), out contents); - if (contents != null && contents.Count > 0) - { - return contents[0]; - } - return default(T); - } - } + /// + /// Gets the first item encountered on the given cell. + /// + /// The cell to search + /// The item or default(T) + public T First(Point cell) + { + lock (lockObject) + { + List contents; + Grid.TryGetValue(Clamp(cell), out contents); + if (contents != null && contents.Count > 0) + { + return contents[0]; + } + return default(T); + } + } - /// - /// Adds a given item to a given cell. - /// If the cell already contains the item, it is not added a second time. - /// - /// The item to add - /// The cell to add the item to - public void Add(T item, Point cell) - { - lock (lockObject) - { - Point c = Clamp(cell); - AddToGrid(item, c); - AddToItems(item, c); - } - } + /// + /// Adds a given item to a given cell. + /// If the cell already contains the item, it is not added a second time. + /// + /// The item to add + /// The cell to add the item to + public void Add(T item, Point cell) + { + lock (lockObject) + { + var c = Clamp(cell); + AddToGrid(item, c); + AddToItems(item, c); + } + } - private void AddToGrid(T item, Point cell) - { - List l; - Grid.TryGetValue(cell, out l); - if (l == null) - { - if (ListOfItemQueue.Count > 0) - { - l = ListOfItemQueue.Dequeue(); - } - else - { - l = new List(); - } - l.Add(item); - Grid.Add(cell, l); - } - else - { - if (!l.Contains(item)) - { - l.Add(item); - } - } - } + private void AddToGrid(T item, Point cell) + { + List l; + Grid.TryGetValue(cell, out l); + if (l == null) + { + if (ListOfItemQueue.Count > 0) + { + l = ListOfItemQueue.Dequeue(); + } + else + { + l = new List(); + } + l.Add(item); + Grid.Add(cell, l); + } + else + { + if (!l.Contains(item)) + { + l.Add(item); + } + } + } - private void AddToItems(T item, Point cell) - { - List pl; - ItemDictionary.TryGetValue(item, out pl); - if (pl == null) - { - if (ListOfPointQueue.Count > 0) - { - pl = ListOfPointQueue.Dequeue(); - } - else - { - pl = new List(); - } - pl.Add(cell); - ItemDictionary.Add(item, pl); - } - else - { - if (!pl.Contains(cell)) - { - pl.Add(cell); - } - } - } + private void AddToItems(T item, Point cell) + { + List pl; + ItemDictionary.TryGetValue(item, out pl); + if (pl == null) + { + if (ListOfPointQueue.Count > 0) + { + pl = ListOfPointQueue.Dequeue(); + } + else + { + pl = new List(); + } + pl.Add(cell); + ItemDictionary.Add(item, pl); + } + else + { + if (!pl.Contains(cell)) + { + pl.Add(cell); + } + } + } - /// - /// Removes all items from the given cell. - /// If the items don't occupy another cell, they are removed as well. - /// - /// The cell to remove items from - public void Remove(Point cell) - { - lock (lockObject) - { - Point c = Clamp(cell); - List l; - Grid.TryGetValue(c, out l); + /// + /// Removes all items from the given cell. + /// If the items don't occupy another cell, they are removed as well. + /// + /// The cell to remove items from + public void Remove(Point cell) + { + lock (lockObject) + { + var c = Clamp(cell); + List l; + Grid.TryGetValue(c, out l); - if (l != null) - { - foreach (T i in l) - { - List pl; - ItemDictionary.TryGetValue(i, out pl); - if (pl != null) - { - pl.Remove(c); - if (pl.Count == 0) - { - ListOfPointQueue.Enqueue(pl); - ItemDictionary.Remove(i); - } - } - } - l.Clear(); - ListOfItemQueue.Enqueue(l); - Grid.Remove(cell); - } - } - } + if (l != null) + { + foreach (var i in l) + { + List pl; + ItemDictionary.TryGetValue(i, out pl); + if (pl != null) + { + pl.Remove(c); + if (pl.Count == 0) + { + ListOfPointQueue.Enqueue(pl); + ItemDictionary.Remove(i); + } + } + } + l.Clear(); + ListOfItemQueue.Enqueue(l); + Grid.Remove(cell); + } + } + } - /// - /// Removes all occurrences of the given item and re-adds it at the new given cell. - /// If the item hasn't been in the grid before, this will just add it. - /// - /// The item to move - /// The cell to move it to - public void Move(T item, Point cell) - { - lock (lockObject) - { - Remove(item); - Add(item, cell); - } - } + /// + /// Removes all occurrences of the given item and re-adds it at the new given cell. + /// If the item hasn't been in the grid before, this will just add it. + /// + /// The item to move + /// The cell to move it to + public void Move(T item, Point cell) + { + lock (lockObject) + { + Remove(item); + Add(item, cell); + } + } - public bool IsEmpty(Point cell) - { - lock (lockObject) - { - return Get(cell).Length == 0; - } - } - } + public bool IsEmpty(Point cell) + { + lock (lockObject) + { + return Get(cell).Length == 0; + } + } + } } \ No newline at end of file diff --git a/CollisionGrid/CollisionGrid.Rect.cs b/CollisionGrid/CollisionGrid.Rect.cs index 42cfe86..76f0979 100644 --- a/CollisionGrid/CollisionGrid.Rect.cs +++ b/CollisionGrid/CollisionGrid.Rect.cs @@ -1,93 +1,107 @@ // *************************************************************************** -// Copyright (c) 2016 by Unterrainer Informatik OG. -// This source is licensed to Unterrainer Informatik OG. -// All rights reserved. -// -// In other words: -// YOU MUST NOT COPY, USE, CHANGE OR REDISTRIBUTE ANY ART, MUSIC, CODE OR -// OTHER DATA, CONTAINED WITHIN THESE DIRECTORIES WITHOUT THE EXPRESS -// PERMISSION OF Unterrainer Informatik OG. -// --------------------------------------------------------------------------- -// Programmer: G U, -// Created: 2016-04-01 +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to // *************************************************************************** using Utilities.Geometry; namespace CollisionGrid { - public partial class CollisionGrid - { - public T[] Get(Rect aabb) - { - lock (lockObject) - { - return Get(Rectangle(aabb)); - } - } + public partial class CollisionGrid + { + public T[] Get(Rect aabb) + { + lock (lockObject) + { + return Get(Rectangle(aabb)); + } + } - /// - /// Gets the first item encountered in the cells that are hit by the given Axis-Aligned-Bounding-Box. - /// - /// The Axis-Aligned-Bounding-Box given in int-cell-coordinates - /// - /// The item or default(T) - /// - public T First(Rect aabb) - { - lock (lockObject) - { - return First(Rectangle(aabb)); - } - } + /// + /// Gets the first item encountered in the cells that are hit by the given Axis-Aligned-Bounding-Box. + /// + /// The Axis-Aligned-Bounding-Box given in int-cell-coordinates + /// + /// The item or default(T) + /// + public T First(Rect aabb) + { + lock (lockObject) + { + return First(Rectangle(aabb)); + } + } - /// - /// Adds a given item to the cells that are hit by the given Axis-Aligned-Bounding-Box. - /// If the cell already contains the item, it is not added a second time. - /// - /// The item to add - /// The Axis-Aligned-Bounding-Box given in float-grid-coordinates. - public void Add(T item, Rect aabb) - { - lock (lockObject) - { - Add(item, Rectangle(aabb)); - } - } + /// + /// Adds a given item to the cells that are hit by the given Axis-Aligned-Bounding-Box. + /// If the cell already contains the item, it is not added a second time. + /// + /// The item to add + /// The Axis-Aligned-Bounding-Box given in float-grid-coordinates. + public void Add(T item, Rect aabb) + { + lock (lockObject) + { + Add(item, Rectangle(aabb)); + } + } - /// - /// Removes all items from the cells that are hit by the given Axis-Aligned-Bounding-Box. - /// If the items don't occupy another cell, they are removed as well. - /// - /// The Axis-Aligned-Bounding-Box given in float-grid-coordinates. - public void Remove(Rect aabb) - { - lock (lockObject) - { - Remove(Rectangle(aabb)); - } - } + /// + /// Removes all items from the cells that are hit by the given Axis-Aligned-Bounding-Box. + /// If the items don't occupy another cell, they are removed as well. + /// + /// The Axis-Aligned-Bounding-Box given in float-grid-coordinates. + public void Remove(Rect aabb) + { + lock (lockObject) + { + Remove(Rectangle(aabb)); + } + } - /// - /// Removes all occurrences of the given item and re-adds it at the new cells that are hit by the given Axis-Aligned-Bounding-Box. - /// If the item hasn't been in the grid before, this will just add it. - /// - /// The item to move - /// The Axis-Aligned-Bounding-Box given in float-grid-coordinates. - public void Move(T item, Rect aabb) - { - lock (lockObject) - { - Move(item, Rectangle(aabb)); - } - } + /// + /// Removes all occurrences of the given item and re-adds it at the new cells that are hit by the given + /// Axis-Aligned-Bounding-Box. + /// If the item hasn't been in the grid before, this will just add it. + /// + /// The item to move + /// The Axis-Aligned-Bounding-Box given in float-grid-coordinates. + public void Move(T item, Rect aabb) + { + lock (lockObject) + { + Move(item, Rectangle(aabb)); + } + } - public bool IsEmpty(Rect aabb) - { - lock (lockObject) - { - return IsEmpty(Rectangle(aabb)); - } - } - } + public bool IsEmpty(Rect aabb) + { + lock (lockObject) + { + return IsEmpty(Rectangle(aabb)); + } + } + } } \ No newline at end of file diff --git a/CollisionGrid/CollisionGrid.Rectangle.cs b/CollisionGrid/CollisionGrid.Rectangle.cs index b0ba961..7de9f7b 100644 --- a/CollisionGrid/CollisionGrid.Rectangle.cs +++ b/CollisionGrid/CollisionGrid.Rectangle.cs @@ -1,15 +1,28 @@ // *************************************************************************** -// Copyright (c) 2016 by Unterrainer Informatik OG. -// This source is licensed to Unterrainer Informatik OG. -// All rights reserved. -// -// In other words: -// YOU MUST NOT COPY, USE, CHANGE OR REDISTRIBUTE ANY ART, MUSIC, CODE OR -// OTHER DATA, CONTAINED WITHIN THESE DIRECTORIES WITHOUT THE EXPRESS -// PERMISSION OF Unterrainer Informatik OG. -// --------------------------------------------------------------------------- -// Programmer: G U, -// Created: 2016-04-01 +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to // *************************************************************************** using System.Collections.Generic; @@ -17,137 +30,138 @@ namespace CollisionGrid { - public partial class CollisionGrid - { - private readonly List lop = new List(); - private readonly List result = new List(); + public partial class CollisionGrid + { + private readonly List lop = new List(); + private readonly List result = new List(); - private void FillList(Rectangle aabb) - { - Rectangle r = Clamp(aabb); - lop.Clear(); - for (int y = 0; y < r.Size.Y; y++) - { - for (int x = 0; x < r.Size.X; x++) - { - lop.Add(new Point(r.X + x, r.Y + y)); - } - } - } + private void FillList(Rectangle aabb) + { + var r = Clamp(aabb); + lop.Clear(); + for (var y = 0; y < r.Size.Y; y++) + { + for (var x = 0; x < r.Size.X; x++) + { + lop.Add(new Point(r.X + x, r.Y + y)); + } + } + } - public T[] Get(Rectangle aabb) - { - lock (lockObject) - { - FillList(aabb); - result.Clear(); - foreach (Point p in lop) - { - foreach (T i in Get(p)) - { - if (!result.Contains(i)) - { - result.Add(i); - } - } - } - return result.ToArray(); - } - } + public T[] Get(Rectangle aabb) + { + lock (lockObject) + { + FillList(aabb); + result.Clear(); + foreach (var p in lop) + { + foreach (var i in Get(p)) + { + if (!result.Contains(i)) + { + result.Add(i); + } + } + } + return result.ToArray(); + } + } - /// - /// Gets the first item encountered in the cells that are hit by the given Axis-Aligned-Bounding-Box. - /// - /// The Axis-Aligned-Bounding-Box given in int-cell-coordinates - /// - /// The item or default(T) - /// - public T First(Rectangle aabb) - { - lock (lockObject) - { - FillList(aabb); - result.Clear(); - foreach (Point p in lop) - { - T content = First(p); - if (!content.Equals(default(T))) - { - return content; - } - } - return default(T); - } - } + /// + /// Gets the first item encountered in the cells that are hit by the given Axis-Aligned-Bounding-Box. + /// + /// The Axis-Aligned-Bounding-Box given in int-cell-coordinates + /// + /// The item or default(T) + /// + public T First(Rectangle aabb) + { + lock (lockObject) + { + FillList(aabb); + result.Clear(); + foreach (var p in lop) + { + var content = First(p); + if (!content.Equals(default(T))) + { + return content; + } + } + return default(T); + } + } - /// - /// Adds a given item to the cells that are hit by the given Axis-Aligned-Bounding-Box. - /// If the cell already contains the item, it is not added a second time. - /// - /// The item to add - /// The Axis-Aligned-Bounding-Box given in int-cell-coordinates - public void Add(T item, Rectangle aabb) - { - lock (lockObject) - { - FillList(aabb); - foreach (Point p in lop) - { - Add(item, p); - } - } - } + /// + /// Adds a given item to the cells that are hit by the given Axis-Aligned-Bounding-Box. + /// If the cell already contains the item, it is not added a second time. + /// + /// The item to add + /// The Axis-Aligned-Bounding-Box given in int-cell-coordinates + public void Add(T item, Rectangle aabb) + { + lock (lockObject) + { + FillList(aabb); + foreach (var p in lop) + { + Add(item, p); + } + } + } - /// - /// Removes all items from the cells that are hit by the given Axis-Aligned-Bounding-Box. - /// If the items don't occupy another cell, they are removed as well. - /// - /// The Axis-Aligned-Bounding-Box given in int-cell-coordinates - public void Remove(Rectangle aabb) - { - lock (lockObject) - { - FillList(aabb); - foreach (Point p in lop) - { - Remove(p); - } - } - } + /// + /// Removes all items from the cells that are hit by the given Axis-Aligned-Bounding-Box. + /// If the items don't occupy another cell, they are removed as well. + /// + /// The Axis-Aligned-Bounding-Box given in int-cell-coordinates + public void Remove(Rectangle aabb) + { + lock (lockObject) + { + FillList(aabb); + foreach (var p in lop) + { + Remove(p); + } + } + } - /// - /// Removes all occurrences of the given item and re-adds it at the new cells that are hit by the given Axis-Aligned-Bounding-Box. - /// If the item hasn't been in the grid before, this will just add it. - /// - /// The item to move - /// The Axis-Aligned-Bounding-Box given in int-cell-coordinates - public void Move(T item, Rectangle aabb) - { - lock (lockObject) - { - Remove(item); - FillList(aabb); - foreach (Point p in lop) - { - Add(item, p); - } - } - } + /// + /// Removes all occurrences of the given item and re-adds it at the new cells that are hit by the given + /// Axis-Aligned-Bounding-Box. + /// If the item hasn't been in the grid before, this will just add it. + /// + /// The item to move + /// The Axis-Aligned-Bounding-Box given in int-cell-coordinates + public void Move(T item, Rectangle aabb) + { + lock (lockObject) + { + Remove(item); + FillList(aabb); + foreach (var p in lop) + { + Add(item, p); + } + } + } - public bool IsEmpty(Rectangle aabb) - { - lock (lockObject) - { - FillList(aabb); - foreach (Point p in lop) - { - if (!IsEmpty(p)) - { - return false; - } - } - return true; - } - } - } + public bool IsEmpty(Rectangle aabb) + { + lock (lockObject) + { + FillList(aabb); + foreach (var p in lop) + { + if (!IsEmpty(p)) + { + return false; + } + } + return true; + } + } + } } \ No newline at end of file diff --git a/CollisionGrid/CollisionGrid.Vector2.cs b/CollisionGrid/CollisionGrid.Vector2.cs index 897be31..79251ad 100644 --- a/CollisionGrid/CollisionGrid.Vector2.cs +++ b/CollisionGrid/CollisionGrid.Vector2.cs @@ -1,93 +1,106 @@ // *************************************************************************** -// Copyright (c) 2016 by Unterrainer Informatik OG. -// This source is licensed to Unterrainer Informatik OG. -// All rights reserved. -// -// In other words: -// YOU MUST NOT COPY, USE, CHANGE OR REDISTRIBUTE ANY ART, MUSIC, CODE OR -// OTHER DATA, CONTAINED WITHIN THESE DIRECTORIES WITHOUT THE EXPRESS -// PERMISSION OF Unterrainer Informatik OG. -// --------------------------------------------------------------------------- -// Programmer: G U, -// Created: 2016-04-01 +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to // *************************************************************************** using Microsoft.Xna.Framework; namespace CollisionGrid { - public partial class CollisionGrid - { - public T[] Get(Vector2 position) - { - lock (lockObject) - { - return Get(Cell(position)); - } - } + public partial class CollisionGrid + { + public T[] Get(Vector2 position) + { + lock (lockObject) + { + return Get(Cell(position)); + } + } - /// - /// Gets the first item encountered in the cell that contains the given position. - /// - /// The position - /// - /// The item or default(T) - /// - public T First(Vector2 position) - { - lock (lockObject) - { - return First(Cell(position)); - } - } + /// + /// Gets the first item encountered in the cell that contains the given position. + /// + /// The position + /// + /// The item or default(T) + /// + public T First(Vector2 position) + { + lock (lockObject) + { + return First(Cell(position)); + } + } - /// - /// Adds a given item to a cell that contains the given position. - /// If the cell already contains the item, it is not added a second time. - /// - /// The item to add - /// The position. - public void Add(T item, Vector2 position) - { - lock (lockObject) - { - Add(item, Cell(position)); - } - } + /// + /// Adds a given item to a cell that contains the given position. + /// If the cell already contains the item, it is not added a second time. + /// + /// The item to add + /// The position. + public void Add(T item, Vector2 position) + { + lock (lockObject) + { + Add(item, Cell(position)); + } + } - /// - /// Removes all items from the cell that contains the given position. - /// If the items don't occupy another cell, they are removed as well. - /// - /// The position. - public void Remove(Vector2 position) - { - lock (lockObject) - { - Remove(Cell(position)); - } - } + /// + /// Removes all items from the cell that contains the given position. + /// If the items don't occupy another cell, they are removed as well. + /// + /// The position. + public void Remove(Vector2 position) + { + lock (lockObject) + { + Remove(Cell(position)); + } + } - /// - /// Removes all occurrences of the given item and re-adds it at the new cell that contains the given position. - /// If the item hasn't been in the grid before, this will just add it. - /// - /// The item to move - /// The position. - public void Move(T item, Vector2 position) - { - lock (lockObject) - { - Move(item, Cell(position)); - } - } + /// + /// Removes all occurrences of the given item and re-adds it at the new cell that contains the given position. + /// If the item hasn't been in the grid before, this will just add it. + /// + /// The item to move + /// The position. + public void Move(T item, Vector2 position) + { + lock (lockObject) + { + Move(item, Cell(position)); + } + } - public bool IsEmpty(Vector2 position) - { - lock (lockObject) - { - return IsEmpty(Cell(position)); - } - } - } + public bool IsEmpty(Vector2 position) + { + lock (lockObject) + { + return IsEmpty(Cell(position)); + } + } + } } \ No newline at end of file diff --git a/CollisionGrid/CollisionGrid.cs b/CollisionGrid/CollisionGrid.cs index b76c0d3..6acfe92 100644 --- a/CollisionGrid/CollisionGrid.cs +++ b/CollisionGrid/CollisionGrid.cs @@ -1,191 +1,191 @@ // *************************************************************************** -// Copyright (c) 2016 by Unterrainer Informatik OG. -// This source is licensed to Unterrainer Informatik OG. -// All rights reserved. -// -// In other words: -// YOU MUST NOT COPY, USE, CHANGE OR REDISTRIBUTE ANY ART, MUSIC, CODE OR -// OTHER DATA, CONTAINED WITHIN THESE DIRECTORIES WITHOUT THE EXPRESS -// PERMISSION OF Unterrainer Informatik OG. -// --------------------------------------------------------------------------- -// Programmer: G U, -// Created: 2016-03-30 +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to // *************************************************************************** -using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Utilities.Geometry; namespace CollisionGrid { - public partial class CollisionGrid - { - private readonly object lockObject = new object(); - - public float Width { get; private set; } - public float Height { get; private set; } - - public float CellWidth { get; private set; } - public float CellHeight { get; private set; } - - public int NumberOfCellsX { get; private set; } - public int NumberOfCellsY { get; private set; } - - private Dictionary> Grid { get; set; } - private Dictionary> ItemDictionary { get; set; } - private Queue> ListOfPointQueue { get; set; } - private Queue> ListOfItemQueue { get; set; } - - public CollisionGrid(float width, float height, int numberOfCellsX, int numberOfCellsY) - { - Width = width; - Height = height; - NumberOfCellsX = numberOfCellsX; - NumberOfCellsY = numberOfCellsY; - CellWidth = Width/NumberOfCellsX; - CellHeight = Height/NumberOfCellsY; - - ItemDictionary = new Dictionary>(); - Grid = new Dictionary>(); - - ListOfPointQueue = new Queue>(); - ListOfItemQueue = new Queue>(); - } - - public Point[] Get(T item) - { - lock (lockObject) - { - List pl; - ItemDictionary.TryGetValue(item, out pl); - if (pl == null) - { - return new Point[0]; - } - return pl.ToArray(); - } - } - - /// - /// Removes the given item from the grid. - /// - /// The item to remove - public void Remove(T item) - { - lock (lockObject) - { - List pl; - ItemDictionary.TryGetValue(item, out pl); - if (pl == null) - { - return; - } - - foreach (Point p in pl) - { - RemoveFromGrid(item, p); - } - - pl.Clear(); - ListOfPointQueue.Enqueue(pl); - ItemDictionary.Remove(item); - } - } - - private void RemoveFromGrid(T item, Point cell) - { - List tl; - Grid.TryGetValue(cell, out tl); - if (tl != null) - { - tl.Remove(item); - if (tl.Count == 0) - { - ListOfItemQueue.Enqueue(tl); - Grid.Remove(cell); - } - } - } - - public IEnumerable Items - { - get { return ItemDictionary.Keys; } - } - - public IEnumerable OccupiedCells - { - get { return Grid.Keys; } - } - - public int OccupiedCellCount - { - get { return ItemDictionary.Keys.Count; } - } - - public int ItemCount - { - get { return Grid.Keys.Count; } - } - - private Rectangle Rectangle(Rect rect) - { - Point tl = Cell(rect.TopLeft); - Point br = Cell(rect.BottomRight); - Point s = br - tl + new Point(1, 1); - return new Rectangle(tl, s); - } - - private Point Cell(Vector2 position) - { - return new Point((int)(position.X / CellWidth), (int)(position.Y / CellHeight)); - } - - private Rectangle Clamp(Rectangle rectangle) - { - Point tl = Clamp(rectangle.Location); - Point br = Clamp(rectangle.Location + rectangle.Size - new Point(1, 1)); - Point s = br - tl + new Point(1, 1); - return new Rectangle(tl, s); - } - - private Point Clamp(Point p) - { - int nx = p.X; - if (nx >= NumberOfCellsX) - { - nx = NumberOfCellsX - 1; - } - if (nx < 0) - { - nx = 0; - } - - int ny = p.Y; - if (ny >= NumberOfCellsY) - { - ny = NumberOfCellsY - 1; - } - if (ny < 0) - { - ny = 0; - } - return new Point(nx, ny); - } - - public void Dispose() - { - lock (lockObject) - { - ListOfPointQueue.Clear(); - ListOfPointQueue = null; - ListOfItemQueue.Clear(); - ListOfItemQueue = null; - Grid.Clear(); - Grid = null; - ItemDictionary.Clear(); - ItemDictionary = null; - } - } - } + public partial class CollisionGrid + { + private readonly object lockObject = new object(); + + public float Width { get; } + public float Height { get; } + + public float CellWidth { get; } + public float CellHeight { get; } + + public int NumberOfCellsX { get; } + public int NumberOfCellsY { get; } + + private Dictionary> Grid { get; set; } + private Dictionary> ItemDictionary { get; set; } + private Queue> ListOfPointQueue { get; set; } + private Queue> ListOfItemQueue { get; set; } + + public CollisionGrid(float width, float height, int numberOfCellsX, int numberOfCellsY) + { + Width = width; + Height = height; + NumberOfCellsX = numberOfCellsX; + NumberOfCellsY = numberOfCellsY; + CellWidth = Width/NumberOfCellsX; + CellHeight = Height/NumberOfCellsY; + + ItemDictionary = new Dictionary>(); + Grid = new Dictionary>(); + + ListOfPointQueue = new Queue>(); + ListOfItemQueue = new Queue>(); + } + + public Point[] Get(T item) + { + lock (lockObject) + { + List pl; + ItemDictionary.TryGetValue(item, out pl); + if (pl == null) + { + return new Point[0]; + } + return pl.ToArray(); + } + } + + /// + /// Removes the given item from the grid. + /// + /// The item to remove + public void Remove(T item) + { + lock (lockObject) + { + List pl; + ItemDictionary.TryGetValue(item, out pl); + if (pl == null) + { + return; + } + + foreach (var p in pl) + { + RemoveFromGrid(item, p); + } + + pl.Clear(); + ListOfPointQueue.Enqueue(pl); + ItemDictionary.Remove(item); + } + } + + private void RemoveFromGrid(T item, Point cell) + { + List tl; + Grid.TryGetValue(cell, out tl); + if (tl != null) + { + tl.Remove(item); + if (tl.Count == 0) + { + ListOfItemQueue.Enqueue(tl); + Grid.Remove(cell); + } + } + } + + public IEnumerable Items => ItemDictionary.Keys; + + public IEnumerable OccupiedCells => Grid.Keys; + + public int OccupiedCellCount => ItemDictionary.Keys.Count; + + public int ItemCount => Grid.Keys.Count; + + private Rectangle Rectangle(Rect rect) + { + var tl = Cell(rect.TopLeft); + var br = Cell(rect.BottomRight); + var s = br - tl + new Point(1, 1); + return new Rectangle(tl, s); + } + + private Point Cell(Vector2 position) + { + return new Point((int) (position.X/CellWidth), (int) (position.Y/CellHeight)); + } + + private Rectangle Clamp(Rectangle rectangle) + { + var tl = Clamp(rectangle.Location); + var br = Clamp(rectangle.Location + rectangle.Size - new Point(1, 1)); + var s = br - tl + new Point(1, 1); + return new Rectangle(tl, s); + } + + private Point Clamp(Point p) + { + var nx = p.X; + if (nx >= NumberOfCellsX) + { + nx = NumberOfCellsX - 1; + } + if (nx < 0) + { + nx = 0; + } + + var ny = p.Y; + if (ny >= NumberOfCellsY) + { + ny = NumberOfCellsY - 1; + } + if (ny < 0) + { + ny = 0; + } + return new Point(nx, ny); + } + + public void Dispose() + { + lock (lockObject) + { + ListOfPointQueue.Clear(); + ListOfPointQueue = null; + ListOfItemQueue.Clear(); + ListOfItemQueue = null; + Grid.Clear(); + Grid = null; + ItemDictionary.Clear(); + ItemDictionary = null; + } + } + } } \ No newline at end of file diff --git a/Test/DrawableGrid.cs b/Test/DrawableGrid.cs index f5cc4e9..a91ec2a 100644 --- a/Test/DrawableGrid.cs +++ b/Test/DrawableGrid.cs @@ -1,15 +1,28 @@ // *************************************************************************** -// Copyright (c) 2016 by Unterrainer Informatik OG. -// This source is licensed to Unterrainer Informatik OG. -// All rights reserved. -// -// In other words: -// YOU MUST NOT COPY, USE, CHANGE OR REDISTRIBUTE ANY ART, MUSIC, CODE OR -// OTHER DATA, CONTAINED WITHIN THESE DIRECTORIES WITHOUT THE EXPRESS -// PERMISSION OF Unterrainer Informatik OG. -// --------------------------------------------------------------------------- -// Programmer: G U, -// Created: 2016-03-30 +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to // *************************************************************************** using System.Collections.Generic; @@ -17,97 +30,98 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Utilities; -using Utilities.Geometry; using Utilities.Randomizing; namespace Test { - internal class DrawableGrid : DrawableGameComponent - { - private const int NUMBER_OF_SPRITES = 50; + internal class DrawableGrid : DrawableGameComponent + { + private const int NUMBER_OF_SPRITES = 50; - public CollisionGrid Grid; - public Vector2 Position { get; set; } - public SpriteBatch SpriteBatch { get; set; } + public CollisionGrid Grid; + public Vector2 Position { get; set; } + public SpriteBatch SpriteBatch { get; } - private readonly List sprites = new List(); - private readonly float width; - private readonly float height; + private readonly List sprites = new List(); + private readonly float width; + private readonly float height; - public DrawableGrid(Game game, SpriteBatch spriteBatch, float width, float height, int numberOfCellsX, int numberOfCellsY) : base(game) - { - SpriteBatch = spriteBatch; - this.width = width; - this.height = height; - Grid = new CollisionGrid(width, height, numberOfCellsX, numberOfCellsY); - } + public DrawableGrid(Game game, SpriteBatch spriteBatch, float width, float height, int numberOfCellsX, + int numberOfCellsY) : base(game) + { + SpriteBatch = spriteBatch; + this.width = width; + this.height = height; + Grid = new CollisionGrid(width, height, numberOfCellsX, numberOfCellsY); + } - public override void Initialize() - { - base.Initialize(); - IRandomNumberGenerator rand = RandomizerController.Randomizer; - for (int i = 0; i < NUMBER_OF_SPRITES; i++) - { - Sprite s = new Sprite(Game, SpriteBatch, new Point((int)width, (int)height)); - s.Trajectory = new Vector2(rand.RandomBetween(-1f, 1f), rand.RandomBetween(-1f, 1f)); - s.Trajectory.Normalize(); - s.Position = new Vector2(rand.RandomBetween(0f, 700f), rand.RandomBetween(0f, 700f)); - s.Velocity = rand.RandomBetween(.1f, 4f); - s.Width = rand.RandomBetween(5, 53); - s.Height = rand.RandomBetween(5, 53); + public override void Initialize() + { + base.Initialize(); + var rand = RandomizerController.Randomizer; + for (var i = 0; i < NUMBER_OF_SPRITES; i++) + { + var s = new Sprite(Game, SpriteBatch, new Point((int) width, (int) height)); + s.Trajectory = new Vector2(rand.RandomBetween(-1f, 1f), rand.RandomBetween(-1f, 1f)); + s.Trajectory.Normalize(); + s.Position = new Vector2(rand.RandomBetween(0f, 700f), rand.RandomBetween(0f, 700f)); + s.Velocity = rand.RandomBetween(.1f, 4f); + s.Width = rand.RandomBetween(5, 53); + s.Height = rand.RandomBetween(5, 53); - s.Initialize(); - Game.Components.Add(s); - sprites.Add(s); - } - } + s.Initialize(); + Game.Components.Add(s); + sprites.Add(s); + } + } - public override void Update(GameTime gameTime) - { - base.Update(gameTime); - foreach (Sprite s in sprites) - { - Grid.Move(s, s.GetAABB()); - } - } + public override void Update(GameTime gameTime) + { + base.Update(gameTime); + foreach (var s in sprites) + { + Grid.Move(s, s.GetAabb()); + } + } - public override void Draw(GameTime gameTime) - { - if (Visible) - { - SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied); - Rectangle r = new Rectangle((int)Position.X, (int)Position.Y, (int)width, (int)height); - SpriteBatch.DrawRectangle(r, Color.Yellow); - for (int x = 0; x < Grid.NumberOfCellsX; x++) - { - for (int y = 0; y < Grid.NumberOfCellsY; y++) - { - Rectangle cell = new Rectangle((int) (Position.X + x*Grid.CellWidth), (int) (Position.Y + y*Grid.CellHeight), - (int) Grid.CellWidth, (int) Grid.CellHeight); - int l = Grid.Get(new Point(x, y)).Length; - if (l > 0) - { - float f = l/4f; - if (f > 1) - { - f = 1; - } - SpriteBatch.FillRectangle(cell, Utils.SetTransparencyOnColor(Color.Red, f)); - } - else - { - SpriteBatch.DrawRectangle(cell, Utils.SetTransparencyOnColor(Color.Yellow, .5f)); - } - } - } - SpriteBatch.End(); - } - } + public override void Draw(GameTime gameTime) + { + if (Visible) + { + SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied); + var r = new Rectangle((int) Position.X, (int) Position.Y, (int) width, (int) height); + SpriteBatch.DrawRectangle(r, Color.Yellow); + for (var x = 0; x < Grid.NumberOfCellsX; x++) + { + for (var y = 0; y < Grid.NumberOfCellsY; y++) + { + var cell = new Rectangle((int) (Position.X + x*Grid.CellWidth), + (int) (Position.Y + y*Grid.CellHeight), + (int) Grid.CellWidth, (int) Grid.CellHeight); + var l = Grid.Get(new Point(x, y)).Length; + if (l > 0) + { + var f = l/4f; + if (f > 1) + { + f = 1; + } + SpriteBatch.FillRectangle(cell, Utils.SetTransparencyOnColor(Color.Red, f)); + } + else + { + SpriteBatch.DrawRectangle(cell, Utils.SetTransparencyOnColor(Color.Yellow, .5f)); + } + } + } + SpriteBatch.End(); + } + } - protected override void UnloadContent() - { - base.UnloadContent(); - Grid.Dispose(); - } - } + protected override void UnloadContent() + { + base.UnloadContent(); + Grid.Dispose(); + } + } } \ No newline at end of file diff --git a/Test/Main.cs b/Test/Main.cs index 3b80861..659aec3 100644 --- a/Test/Main.cs +++ b/Test/Main.cs @@ -1,61 +1,73 @@ // *************************************************************************** -// Copyright (c) 2016 by Unterrainer Informatik OG. -// This source is licensed to Unterrainer Informatik OG. -// All rights reserved. -// -// In other words: -// YOU MUST NOT COPY, USE, CHANGE OR REDISTRIBUTE ANY ART, MUSIC, CODE OR -// OTHER DATA, CONTAINED WITHIN THESE DIRECTORIES WITHOUT THE EXPRESS -// PERMISSION OF Unterrainer Informatik OG. -// --------------------------------------------------------------------------- -// Programmer: G U, -// Created: 2016-03-30 +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to // *************************************************************************** using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Utilities; -using Utilities.Randomizing; namespace Test { - internal class Main : Game - { - public const int MIN_SCREEN_RESOLUTION_WIDTH = 1024; - public const int MIN_SCREEN_RESOLUTION_HEIGHT = 768; - - public GraphicsDeviceManager GraphicsDeviceManager; - - public Main() - { - Content.RootDirectory = "Content"; - - GraphicsDeviceManager = new GraphicsDeviceManager(this); - GraphicsDeviceManager.PreferredBackBufferWidth = MIN_SCREEN_RESOLUTION_WIDTH; - GraphicsDeviceManager.PreferredBackBufferHeight = MIN_SCREEN_RESOLUTION_HEIGHT; - GraphicsDeviceManager.IsFullScreen = false; - } - - protected override void Initialize() - { - Rectangle? screenBounds = Utils.InitGraphicsMode(this, GraphicsDeviceManager, MIN_SCREEN_RESOLUTION_WIDTH, - MIN_SCREEN_RESOLUTION_HEIGHT, false); - if (screenBounds == null) - { - Console.Out.WriteLine("Severe error opening and initializing window."); - Exit(); - } - - DrawableGrid grid = new DrawableGrid(this, new SpriteBatch(GraphicsDevice), 700, 700, 40, 40); - Components.Add(grid); - grid.Initialize(); - } - - protected override void Draw(GameTime gameTime) - { - GraphicsDevice.Clear(Color.Black); - base.Draw(gameTime); - } - } + internal class Main : Game + { + public const int MIN_SCREEN_RESOLUTION_WIDTH = 1024; + public const int MIN_SCREEN_RESOLUTION_HEIGHT = 768; + + public GraphicsDeviceManager GraphicsDeviceManager; + + public Main() + { + Content.RootDirectory = "Content"; + + GraphicsDeviceManager = new GraphicsDeviceManager(this); + GraphicsDeviceManager.PreferredBackBufferWidth = MIN_SCREEN_RESOLUTION_WIDTH; + GraphicsDeviceManager.PreferredBackBufferHeight = MIN_SCREEN_RESOLUTION_HEIGHT; + GraphicsDeviceManager.IsFullScreen = false; + } + + protected override void Initialize() + { + var screenBounds = Utils.InitGraphicsMode(this, GraphicsDeviceManager, MIN_SCREEN_RESOLUTION_WIDTH, + MIN_SCREEN_RESOLUTION_HEIGHT, false); + if (screenBounds == null) + { + Console.Out.WriteLine("Severe error opening and initializing window."); + Exit(); + } + + var grid = new DrawableGrid(this, new SpriteBatch(GraphicsDevice), 700, 700, 40, 40); + Components.Add(grid); + grid.Initialize(); + } + + protected override void Draw(GameTime gameTime) + { + GraphicsDevice.Clear(Color.Black); + base.Draw(gameTime); + } + } } \ No newline at end of file diff --git a/Test/Program.cs b/Test/Program.cs index bf0f08b..693da43 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -1,32 +1,44 @@ // *************************************************************************** -// Copyright (c) 2013 by Unterrainer Informatik OG. -// This source is licensed to Unterrainer Informatik OG. -// All rights reserved. -// -// In other words: -// YOU MUST NOT COPY, USE, CHANGE OR REDISTRIBUTE ANY ART, MUSIC, CODE OR -// OTHER DATA, CONTAINED WITHIN THESE DIRECTORIES WITHOUT THE EXPRESS -// PERMISSION OF Unterrainer Informatik OG. +// This is free and unencumbered software released into the public domain. // -// Classes using other, less restrictive licenses are explicitly marked. -// --------------------------------------------------------------------------- -// Programmer: G U, -// Created: 2013-03-31 +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to // *************************************************************************** namespace Test { - internal static class Program - { - /// - /// The main entry point for the application. - /// - private static void Main(string[] args) - { - using (Main game = new Main()) - { - game.Run(); - } - } - } + internal static class Program + { + /// + /// The main entry point for the application. + /// + // ReSharper disable once UnusedParameter.Local + private static void Main(string[] args) + { + using (var game = new Main()) + { + game.Run(); + } + } + } } \ No newline at end of file diff --git a/Test/Sprite.cs b/Test/Sprite.cs index 4bf1732..d4e3120 100644 --- a/Test/Sprite.cs +++ b/Test/Sprite.cs @@ -1,19 +1,30 @@ // *************************************************************************** -// Copyright (c) 2016 by Unterrainer Informatik OG. -// This source is licensed to Unterrainer Informatik OG. -// All rights reserved. -// -// In other words: -// YOU MUST NOT COPY, USE, CHANGE OR REDISTRIBUTE ANY ART, MUSIC, CODE OR -// OTHER DATA, CONTAINED WITHIN THESE DIRECTORIES WITHOUT THE EXPRESS -// PERMISSION OF Unterrainer Informatik OG. -// --------------------------------------------------------------------------- -// Programmer: G U, -// Created: 2016-03-30 +// This is free and unencumbered software released into the public domain. +// +// Anyone is free to copy, modify, publish, use, compile, sell, or +// distribute this software, either in source code form or as a compiled +// binary, for any purpose, commercial or non-commercial, and by any +// means. +// +// In jurisdictions that recognize copyright laws, the author or authors +// of this software dedicate any and all copyright interest in the +// software to the public domain. We make this dedication for the benefit +// of the public at large and to the detriment of our heirs and +// successors. We intend this dedication to be an overt act of +// relinquishment in perpetuity of all present and future rights to this +// software under copyright law. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// For more information, please refer to // *************************************************************************** -using System; -using System.Data.Odbc; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Utilities; @@ -21,65 +32,65 @@ namespace Test { - internal class Sprite : DrawableGameComponent - { - public SpriteBatch SpriteBatch { get; set; } + internal class Sprite : DrawableGameComponent + { + public SpriteBatch SpriteBatch { get; } - public Vector2 Position { get; set; } - public Vector2 Trajectory { get; set; } - public float Velocity { get; set; } + public Vector2 Position { get; set; } + public Vector2 Trajectory { get; set; } + public float Velocity { get; set; } - public float Width { get; set; } - public float Height { get; set; } + public float Width { get; set; } + public float Height { get; set; } - private Point bounds; + private Point bounds; - public Sprite(Game game, SpriteBatch spriteBatch, Point bounds) : base(game) - { - SpriteBatch = spriteBatch; - this.bounds = bounds; - Width = 11; - Height = 11; - } + public Sprite(Game game, SpriteBatch spriteBatch, Point bounds) : base(game) + { + SpriteBatch = spriteBatch; + this.bounds = bounds; + Width = 11; + Height = 11; + } - public override void Update(GameTime gameTime) - { - base.Update(gameTime); + public override void Update(GameTime gameTime) + { + base.Update(gameTime); - Position = Position + Trajectory*Velocity; + Position = Position + Trajectory*Velocity; - if (Position.X <= 0) - { - Position = new Vector2(0, Position.Y); - Trajectory = new Vector2(-Trajectory.X, Trajectory.Y); - } - if (Position.X >= bounds.X) - { - Position = new Vector2(bounds.X, Position.Y); - Trajectory = new Vector2(-Trajectory.X, Trajectory.Y); - } - if (Position.Y <= 0) - { - Position = new Vector2(Position.X, 0); - Trajectory = new Vector2(Trajectory.X, -Trajectory.Y); - } - if (Position.Y >= bounds.Y) - { - Position = new Vector2(Position.X, bounds.Y); - Trajectory = new Vector2(Trajectory.X, -Trajectory.Y); - } - } + if (Position.X <= 0) + { + Position = new Vector2(0, Position.Y); + Trajectory = new Vector2(-Trajectory.X, Trajectory.Y); + } + if (Position.X >= bounds.X) + { + Position = new Vector2(bounds.X, Position.Y); + Trajectory = new Vector2(-Trajectory.X, Trajectory.Y); + } + if (Position.Y <= 0) + { + Position = new Vector2(Position.X, 0); + Trajectory = new Vector2(Trajectory.X, -Trajectory.Y); + } + if (Position.Y >= bounds.Y) + { + Position = new Vector2(Position.X, bounds.Y); + Trajectory = new Vector2(Trajectory.X, -Trajectory.Y); + } + } - public Rect GetAABB() - { - return new Rect(Position.X - Width/2f, Position.Y - Height/2f, Width, Height); - } + public Rect GetAabb() + { + return new Rect(Position.X - Width/2f, Position.Y - Height/2f, Width, Height); + } - public override void Draw(GameTime gameTime) - { - SpriteBatch.Begin(); - SpriteBatch.FillRectangle(GetAABB().ToRectangle(), Color.White); - SpriteBatch.End(); - } - } + public override void Draw(GameTime gameTime) + { + SpriteBatch.Begin(); + SpriteBatch.FillRectangle(GetAabb().ToRectangle(), Color.White); + SpriteBatch.End(); + } + } } \ No newline at end of file