Skip to content

Commit

Permalink
another bunch of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
stilnat committed Nov 21, 2023
1 parent 3d523bc commit bfea028
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 87 deletions.
86 changes: 86 additions & 0 deletions Assets/Scripts/SS3D/Systems/Tile/TileMapCreator/BuildGhost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using SS3D.Data;
using SS3D.Data.Enums;
using SS3D.Systems.Tile;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SS3D.Systems.Tile.TileMapCreator
{

public enum BuildMatMode
{
Valid,
Invalid,
Delete
}

public class BuildGhost
{
public GameObject ghost;
public Vector3 position;
public Direction direction;

public BuildGhost(GameObject ghostObject, Vector3 targetPosition, Direction dir)
{
ghost = ghostObject;
position = targetPosition;
direction = dir;
}

/// <summary>
/// Chooses which material to set on the ghost based on which mode we are building.
/// </summary>
/// <param name="mode"></param>
public void ChangeGhostColor(BuildMatMode mode)
{
Material ghostMat = null;

switch (mode)
{
case BuildMatMode.Valid:
ghostMat = Assets.Get<Material>((int)AssetDatabases.Materials, (int)MaterialsIds.ValidConstruction);
break;

case BuildMatMode.Invalid:
ghostMat = Assets.Get<Material>((int)AssetDatabases.Materials, (int)MaterialsIds.InvalidConstruction);
break;

case BuildMatMode.Delete:
ghostMat = Assets.Get<Material>((int)AssetDatabases.Materials, (int)MaterialsIds.DeleteConstruction);
break;
}


foreach (MeshRenderer mr in ghost.GetComponentsInChildren<MeshRenderer>())
{
Material[] materials = mr.materials;
for (int i = 0; i < materials.Length; i++)
{
materials[i] = ghostMat;
}

mr.materials = materials;
}
}

public void UpdateRotationAndPosition()
{
// Small offset is added so that meshes don't overlap with already placed objects.
ghost.transform.position = Vector3.Lerp(ghost.transform.position, position + new Vector3(0, 0.1f, 0), Time.deltaTime * 15f);
ghost.transform.rotation = Quaternion.Lerp(ghost.transform.rotation, Quaternion.Euler(0, TileHelper.GetRotationAngle(direction), 0), Time.deltaTime * 15f);
}

public void SetNextRotation()
{
if (ghost.TryGetComponent(out ICustomGhostRotation customRotationComponent))
{
direction = customRotationComponent.GetNextDirection(direction);
}
else
{
direction = TileHelper.GetNextCardinalDir(direction);
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/SS3D/Systems/Tile/TileMapCreator/BuildGhost.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class BuildGhostManager : NetworkActor
private Controls.TileCreatorActions _controls;


private bool _itemPlacement = false;
private bool _isPlacingItem = false;

private Vector3 _lastSnappedPosition;

Expand All @@ -53,13 +53,6 @@ public class BuildGhostManager : NetworkActor
[SerializeField]
private TileMapCreator _menu;

public enum BuildMatMode
{
Valid,
Invalid,
Delete
}

protected override void OnStart()
{
base.OnStart();
Expand All @@ -83,7 +76,7 @@ public void Update()

ActivateGhosts();

Vector3 position = TileHelper.GetPointedPosition(!_itemPlacement);
Vector3 position = TileHelper.GetPointedPosition(!_isPlacingItem);
// Move buildGhost, that sticks to the mouse. Currently it exists only if player is not dragging.
if (_ghosts.Count == 1)
{
Expand Down Expand Up @@ -170,75 +163,6 @@ public void DestroyGhosts()
_ghosts.Clear();
}

public class BuildGhost
{
public GameObject ghost;
public Vector3 position;
public Direction direction;

public BuildGhost(GameObject ghostObject, Vector3 targetPosition, Direction dir)
{
ghost = ghostObject;
position = targetPosition;
direction = dir;
}

/// <summary>
/// Chooses which material to set on the ghost based on which mode we are building.
/// </summary>
/// <param name="mode"></param>
public void ChangeGhostColor(BuildMatMode mode)
{
Material ghostMat = null;

switch (mode)
{
case BuildMatMode.Valid:
ghostMat = Assets.Get<Material>((int)AssetDatabases.Materials, (int)MaterialsIds.ValidConstruction);
break;

case BuildMatMode.Invalid:
ghostMat = Assets.Get<Material>((int)AssetDatabases.Materials, (int)MaterialsIds.InvalidConstruction);
break;

case BuildMatMode.Delete:
ghostMat = Assets.Get<Material>((int)AssetDatabases.Materials, (int)MaterialsIds.DeleteConstruction);
break;
}


foreach (MeshRenderer mr in ghost.GetComponentsInChildren<MeshRenderer>())
{
Material[] materials = mr.materials;
for (int i = 0; i < materials.Length; i++)
{
materials[i] = ghostMat;
}

mr.materials = materials;
}
}

public void UpdateRotationAndPosition()
{
// Small offset is added so that meshes don't overlap with already placed objects.
ghost.transform.position = Vector3.Lerp(ghost.transform.position, position + new Vector3(0, 0.1f, 0), Time.deltaTime * 15f);
ghost.transform.rotation = Quaternion.Lerp(ghost.transform.rotation, Quaternion.Euler(0, TileHelper.GetRotationAngle(direction), 0), Time.deltaTime * 15f);
}

public void SetNextRotation()
{
if (ghost.TryGetComponent(out ICustomGhostRotation customRotationComponent))
{
direction = customRotationComponent.GetNextDirection(direction);
}
else
{
direction = TileHelper.GetNextCardinalDir(direction);
}
}
}

private void HandleDeleteButton()
{
_isDeleting = true;
Expand All @@ -257,7 +181,7 @@ private void HandleRotate(InputAction.CallbackContext context)
private void HandlePlaceStarted(InputAction.CallbackContext context)
{
// Dragging is disabled for items
if (_itemPlacement)
if (_isPlacingItem)
return;

_isDragging = true;
Expand Down Expand Up @@ -285,21 +209,21 @@ private void HandlePlacePerformed(InputAction.CallbackContext context)
DestroyGhosts();

if (_selectedObject == null) return;
CreateGhost(_selectedObject.prefab, TileHelper.GetPointedPosition(!_itemPlacement));
CreateGhost(_selectedObject.prefab, TileHelper.GetPointedPosition(!_isPlacingItem));
}

public void SetSelectedObject(GenericObjectSo genericObjectSo)
{
_itemPlacement = genericObjectSo switch
_isPlacingItem = genericObjectSo switch
{
TileObjectSo => false,
ItemObjectSo => true,
_ => _itemPlacement,
_ => _isPlacingItem,
};
_selectedObject = genericObjectSo;

DestroyGhosts();
CreateGhost(genericObjectSo.prefab, TileHelper.GetPointedPosition(!_itemPlacement));
CreateGhost(genericObjectSo.prefab, TileHelper.GetPointedPosition(!_isPlacingItem));
}

private void HandleReplace(InputAction.CallbackContext context)
Expand Down Expand Up @@ -328,7 +252,7 @@ private void PlaceOnGhosts()
/// </summary>
private void DeleteOnGhosts()
{
if (_itemPlacement)
if (_isPlacingItem)
{
FindAndDeleteItem();
}
Expand All @@ -344,13 +268,13 @@ private void DeleteOnGhosts()
/// <summary>
/// Update material of buildGhost based build (or anything else) mode and ghosts position
/// </summary>
public void RefreshGhost(BuildGhost buildGhost)
private void RefreshGhost(BuildGhost buildGhost)
{
if (_isDeleting)
{
buildGhost.ChangeGhostColor(BuildMatMode.Delete);
}
else if (_itemPlacement)
else if (_isPlacingItem)
{
buildGhost.ChangeGhostColor(BuildMatMode.Valid);
}
Expand Down Expand Up @@ -390,7 +314,7 @@ private void LineDrag(Vector3 position)
}

[ServerRpc(RequireOwnership = false)]
public void RpcSendCanBuild(string tileObjectSoName, Vector3 placePosition, Direction dir, bool replaceExisting, NetworkConnection conn)
private void RpcSendCanBuild(string tileObjectSoName, Vector3 placePosition, Direction dir, bool replaceExisting, NetworkConnection conn)
{

var tileSystem = Subsystems.Get<TileSystem>();
Expand Down

0 comments on commit bfea028

Please sign in to comment.