- Home /
Question by
yashetv · Aug 16, 2020 at 08:08 PM ·
unity 2darchitecturedependencies
How to make good code architecture and dependencies
Hey, I'm making a tilebased game, and have a little problem with my code architecture. Everything works fine, but it's a mess in one point. So..
I have PlayerMovement and BoxMovement scripts which inherits from Move script. At this point everything seems to be ok. In Player Movement i call the "TryToMove" method from Move script.
Move script check if ahead of player is wall / box or air. If it's an air, obviously player can move If it's a wall, obciously player can't move but if its a box... i have to call method again for box. And check what is ahead of the box...
How I can do it without making a spaghetti code.. I know how stupid it looks, but i can't figure out better solution.
CLASS MOVE
public void TryToMove(Tilemap baseMap, Vector3Int tileAhead, Vector3Int playerDirection)
{
switch (TileCollision.CheckTile(baseMap, tileAhead))
{
case TileCollision.TileType.undefined:
Debug.LogError($"Not recognized object");
break;
case TileCollision.TileType.empty:
MoveTo(tileAhead);
break;
case TileCollision.TileType.box:
TileCollision.Move(baseMap, tileAhead, playerDirection);
MoveTo(tileAhead);
break;
case TileCollision.TileType.wall:
return;
}
}
public static class TileCollision
{
public enum TileType
{
undefined = 0,
empty = 1,
box = 2,
wall = 3
}
static TileBase tile;
public static TileType CheckTile(Tilemap baseMap, Vector3Int tileToCheck)
{
TileBase tile = baseMap.GetTile(tileToCheck);
if (tile == null)
{
return TileType.empty;
}
else if (tile.name == "BoxTile")
{
return TileType.box;
}
else if (tile.name == "Wall")
{
return TileType.wall;
}
else
{
return TileType.undefined;
}
}
public static void Move(Tilemap baseMap, Vector3Int tileAhead, Vector3Int playerDirection)
{
Vector3Int tileAheadBox = tileAhead + playerDirection;
GameObject boxTile = baseMap.GetInstantiatedObject(tileAhead);
BoxMovement boxMovement = boxTile.GetComponent<BoxMovement>();
boxMovement.tileAheadPlayer = tileAhead;
boxMovement.tileAheadBox = tileAheadBox;
boxMovement.TryToMove(baseMap, tileAheadBox, playerDirection);
}
Comment