Skip to content

Commit

Permalink
Define IWorld and IWorldService, and implement them. (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevzhao2 committed Jun 3, 2020
1 parent 2561000 commit aa5f767
Show file tree
Hide file tree
Showing 11 changed files with 1,525 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Orion/Npcs/INpcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace Orion.Npcs {
/// <summary>
/// Represents an item service. Provides access to NPC-related properties and methods.
/// Represents an NPC service. Provides access to NPC-related properties and methods.
/// </summary>
public interface INpcService {
/// <summary>
Expand Down
46 changes: 46 additions & 0 deletions src/Orion/World/IWorld.cs
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; }
}
}
29 changes: 29 additions & 0 deletions src/Orion/World/IWorldService.cs
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; }
}
}
61 changes: 61 additions & 0 deletions src/Orion/World/OrionWorld.cs
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);
}
}
}
Loading

0 comments on commit aa5f767

Please sign in to comment.