Commit d5d15ee4 authored by Martin Mihálik's avatar Martin Mihálik 🔥
Browse files

Simplified Building

parent 66c6d4a8
......@@ -16,7 +16,7 @@ GameObject:
- component: {fileID: 2654140422281934945}
m_Layer: 11
m_Name: Wall
m_TagString: Untagged
m_TagString: Wall
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
......
......@@ -414,6 +414,7 @@ GameObject:
- component: {fileID: 512397506}
- component: {fileID: 512397508}
- component: {fileID: 512397507}
- component: {fileID: 512397509}
m_Layer: 7
m_Name: Player
m_TagString: Untagged
......@@ -618,6 +619,20 @@ MonoBehaviour:
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!114 &512397509
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 512397500}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: bea89f73c7d0f4f4eaa902784e4b227c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Settings: {fileID: 1654006372}
m_BuildingOffset: {x: 0, y: 2}
--- !u!1 &519420028
GameObject:
m_ObjectHideFlags: 0
......@@ -1331,7 +1346,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d4a190b256f554c43815bcb0719d68d2, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Duration: 10
m_Duration: 60
--- !u!1 &1654006367
GameObject:
m_ObjectHideFlags: 0
......@@ -1492,7 +1507,6 @@ GameObject:
- component: {fileID: 1695424173}
- component: {fileID: 1695424176}
- component: {fileID: 1695424175}
- component: {fileID: 1695424174}
m_Layer: 11
m_Name: Walls
m_TagString: Repeatable
......@@ -1514,21 +1528,6 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1695424174
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1695424172}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: bea89f73c7d0f4f4eaa902784e4b227c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Settings: {fileID: 1654006372}
m_Player: {fileID: 512397504}
m_BuildingOffset: {x: 0, y: 2}
--- !u!66 &1695424175
CompositeCollider2D:
m_ObjectHideFlags: 0
......
......@@ -6,9 +6,31 @@ public class RubbleCollector : MonoBehaviour
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.GetComponent<RubbleCollectible>() != null)
Collect(collision.gameObject);
}
private void Collect(GameObject piece)
{
if (piece.GetComponent<RubbleCollectible>() != null)
{
Pieces += 1;
}
}
}
private void Spend(uint amount)
{
Pieces -= amount;
}
public bool TrySpend(uint amount)
{
if (Pieces < amount)
{
return false;
}
Spend(amount);
return true;
}
}
using UnityEngine;
public class WallSpawner : MonoBehaviour
public class WallBuilder : MonoBehaviour
{
[SerializeField] private Settings m_Settings;
[SerializeField] private Transform m_Player;
[SerializeField] private Vector2 m_BuildingOffset;
private Settings Settings { get => m_Settings; }
private RubbleCollector m_Materials;
private void Start()
{
m_Materials = GetComponent<RubbleCollector>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.W))
if (Input.GetButtonDown("Build"))
{
SpawnWall(m_Player.position + new Vector3(m_BuildingOffset.x, m_BuildingOffset.y, 0));
Vector2 basePosition = gameObject.transform.position;
BuildWall(basePosition + m_BuildingOffset);
}
}
private void SpawnWall(Vector2 position)
private void BuildWall(Vector2 position)
{
var cell = Settings.Walls.Grid.WorldToCell(position);
if (!IsFree(cell))
{
return;
}
if (!m_Materials.TrySpend(Settings.Walls.BuildingCost))
{
return;
}
SpawnWall(cell);
}
private void SpawnWall(Vector3Int cell)
{
var snappedPosition = Settings.Walls.Grid.GetCellCenterWorld(cell);
var wall = Instantiate(Settings.Walls.Prefab, snappedPosition, Quaternion.identity, Settings.Walls.Parent);
......@@ -28,4 +52,19 @@ public class WallSpawner : MonoBehaviour
damageTaker.StartingHealth = Settings.Walls.StartingHealth;
}
private bool IsFree(Vector3Int cell)
{
foreach (var wall in GameObject.FindGameObjectsWithTag("Wall"))
{
var candidate = Settings.Walls.Grid.WorldToCell(wall.gameObject.transform.position);
if (candidate == cell)
{
return false;
}
}
return true;
}
}
......@@ -38,13 +38,13 @@ InputManager:
axis: 0
joyNum: 0
- serializedVersion: 3
m_Name: Fire1
m_Name: Build
descriptiveName:
descriptiveNegativeName:
negativeButton:
positiveButton: left ctrl
positiveButton: b
altNegativeButton:
altPositiveButton: mouse 0
altPositiveButton:
gravity: 1000
dead: 0.001
sensitivity: 1000
......
......@@ -7,6 +7,8 @@ TagManager:
- Repeatable
- Meteor
- Rubble
- Explosion
- Wall
layers:
- Default
- TransparentFX
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment