forked from RE-SS3D/SS3D
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multiple tile objects on a single tile (RE-SS3D#1269)
* add interface for different kind of tile location * allow support multiple wall mounts * simplify the loading of tile maps * add polymorphic serialization * using cysharp * Fix removing multiple tile objects at once * Adding doc * remake field public * some doc * readd map * some doc
- Loading branch information
Showing
26 changed files
with
613 additions
and
175 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
131 changes: 131 additions & 0 deletions
131
Assets/Scripts/SS3D/Systems/Tile/CardinalTileLocation.cs
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,131 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using UnityEngine; | ||
|
||
namespace SS3D.Systems.Tile | ||
{ | ||
/// <summary> | ||
/// Represent a Tile location able to contain up to 4 tile objects, in each cardinal directions. | ||
/// </summary> | ||
public class CardinalTileLocation : ITileLocation | ||
{ | ||
private TileLayer _layer; | ||
private int _x; | ||
private int _y; | ||
|
||
/// <summary> | ||
/// the four potential placed tile objects. 0 is for north, 1 is for east, 2 for south, 3 for west. | ||
/// </summary> | ||
private PlacedTileObject[] _cardinalPlacedTileObject = new PlacedTileObject[4]; | ||
|
||
/// <summary> | ||
/// The layer this location is on. | ||
/// </summary> | ||
public TileLayer Layer => _layer; | ||
|
||
public CardinalTileLocation(TileLayer layer, int x, int y) | ||
{ | ||
_layer = layer; | ||
_x = x; | ||
_y = y; | ||
} | ||
|
||
public void ClearAllPlacedObject() | ||
{ | ||
for (int i = 0; i < 4; i++) | ||
{ | ||
TryClearPlacedObject(IndexToDir(i)); | ||
} | ||
} | ||
|
||
public bool IsEmpty(Direction direction = Direction.North) | ||
{ | ||
if (!TileHelper.IsCardinalDirection(direction)) | ||
{ | ||
return false; | ||
} | ||
return _cardinalPlacedTileObject[DirToIndex(direction)] == null; | ||
} | ||
|
||
public bool IsFullyEmpty() | ||
{ | ||
return _cardinalPlacedTileObject.Where(x => x != null).Count() == 0; | ||
} | ||
|
||
public ISavedTileLocation Save() | ||
{ | ||
List<SavedPlacedTileObject> savedTileObjects= new List<SavedPlacedTileObject>(); | ||
foreach(PlacedTileObject tileObject in _cardinalPlacedTileObject.Where(x => x != null)) | ||
{ | ||
savedTileObjects.Add(tileObject.Save()); | ||
} | ||
|
||
return new SavedTileCardinalLocation(savedTileObjects, new Vector2Int(_x, _y), _layer); | ||
} | ||
|
||
public bool TryClearPlacedObject(Direction direction = Direction.North) | ||
{ | ||
if (!TileHelper.IsCardinalDirection(direction)) | ||
{ | ||
return false; | ||
} | ||
|
||
PlacedTileObject placedObject = _cardinalPlacedTileObject[DirToIndex(direction)]; | ||
if (placedObject != null) | ||
{ | ||
placedObject.DestroySelf(); | ||
placedObject = null; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public bool TryGetPlacedObject(out PlacedTileObject placedObject, Direction direction = Direction.North) | ||
{ | ||
if (!TileHelper.IsCardinalDirection(direction)) | ||
{ | ||
placedObject = null; | ||
return false; | ||
} | ||
|
||
PlacedTileObject currentPlacedObject = _cardinalPlacedTileObject[DirToIndex(direction)]; | ||
if (currentPlacedObject != null) | ||
{ | ||
placedObject = currentPlacedObject; | ||
return true; | ||
} | ||
placedObject = null; | ||
return false; | ||
} | ||
|
||
public void AddPlacedObject(PlacedTileObject tileObject, Direction direction = Direction.North) | ||
{ | ||
if (!TileHelper.CardinalDirections().Contains(direction)) | ||
{ | ||
return; | ||
} | ||
else | ||
{ | ||
_cardinalPlacedTileObject[DirToIndex(direction)] = tileObject; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Tie an index array to each cardinal direction. | ||
/// </summary> | ||
private int DirToIndex(Direction direction) | ||
{ | ||
return (int)direction / 2; | ||
} | ||
|
||
/// <summary> | ||
/// Tie an index array to each cardinal direction. | ||
/// </summary> | ||
private Direction IndexToDir(int i) | ||
{ | ||
return (Direction) (i*2); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Scripts/SS3D/Systems/Tile/CardinalTileLocation.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,24 @@ | ||
using SS3D.Systems.Tile; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
/// <summary> | ||
/// Interface for saving tile locations. | ||
/// Fields in classes implementing this interface should be public, at least the one that should be serialized. | ||
/// </summary> | ||
public interface ISavedTileLocation | ||
{ | ||
public List<SavedPlacedTileObject> GetPlacedObjects(); | ||
|
||
public Vector2Int Location | ||
{ | ||
get; | ||
set; | ||
} | ||
|
||
public TileLayer Layer | ||
{ | ||
get; | ||
set; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Scripts/SS3D/Systems/Tile/ISavedTileLocation.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.