-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define IWorld and IWorldService, and implement them. (#58)
- Loading branch information
Showing
11 changed files
with
1,525 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2020 Pryaxis & Orion Contributors | ||
// | ||
// This file is part of Orion. | ||
// | ||
// Orion is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Orion is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Orion. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
using Orion.Entities; | ||
using Orion.World.Tiles; | ||
|
||
namespace Orion.World { | ||
/// <summary> | ||
/// Represents a Terraria world. | ||
/// </summary> | ||
public interface IWorld : IAnnotatable { | ||
/// <summary> | ||
/// Gets a reference to the tile at the given coordinates. | ||
/// </summary> | ||
/// <param name="x">The X coordinate.</param> | ||
/// <param name="y">The Y coordinate.</param> | ||
/// <returns>A reference to the tile at the given coordinates.</returns> | ||
ref Tile this[int x, int y] { get; } | ||
|
||
/// <summary> | ||
/// Gets the world width. | ||
/// </summary> | ||
/// <value>The world width.</value> | ||
int Width { get; } | ||
|
||
/// <summary> | ||
/// Gets the world height. | ||
/// </summary> | ||
/// <value>The world height.</value> | ||
int Height { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) 2020 Pryaxis & Orion Contributors | ||
// | ||
// This file is part of Orion. | ||
// | ||
// Orion is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Orion is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Orion. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
namespace Orion.World { | ||
/// <summary> | ||
/// Represents a world service. Provides access to world-related properties and methods. | ||
/// </summary> | ||
public interface IWorldService { | ||
/// <summary> | ||
/// Gets the world. | ||
/// </summary> | ||
/// <value>The world.</value> | ||
IWorld World { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) 2020 Pryaxis & Orion Contributors | ||
// | ||
// This file is part of Orion. | ||
// | ||
// Orion is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Orion is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Orion. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using Orion.Entities; | ||
using Orion.World.Tiles; | ||
|
||
namespace Orion.World { | ||
internal sealed unsafe class OrionWorld : AnnotatableObject, IDisposable, IWorld { | ||
// Store a pointer to the `Tile` array in unmanaged memory so that the array doesn't need to be pinned. | ||
private readonly unsafe Tile* _tiles; | ||
|
||
public OrionWorld(int width, int height) { | ||
Debug.Assert(width > 0); | ||
Debug.Assert(height > 0); | ||
|
||
Width = width; | ||
Height = height; | ||
|
||
_tiles = (Tile*)Marshal.AllocHGlobal(sizeof(Tile) * width * height); | ||
} | ||
|
||
~OrionWorld() { | ||
Marshal.FreeHGlobal((IntPtr)_tiles); | ||
} | ||
|
||
public ref Tile this[int x, int y] { | ||
get { | ||
Debug.Assert(x >= 0 && x < Width); | ||
Debug.Assert(y >= 0 && y < Height); | ||
|
||
var offset = y * Width + x; | ||
return ref _tiles[offset]; | ||
} | ||
} | ||
|
||
public int Width { get; } | ||
public int Height { get; } | ||
|
||
public void Dispose() { | ||
Marshal.FreeHGlobal((IntPtr)_tiles); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
Oops, something went wrong.