55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using System.Collections.Generic;
|
|
using AsteroidGame.Entities.Structures.Scripts;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace AsteroidGame.Handlers
|
|
{
|
|
public class BuildingHandler : HandlerBase
|
|
{
|
|
[Header("Connections")]
|
|
[SerializeField] private new Camera camera ;
|
|
|
|
[Header("State")]
|
|
[SerializeField] private int buildingSelector;
|
|
|
|
[Header("Prefabs")]
|
|
[SerializeField] private List<StructureBase> buildings = new();
|
|
|
|
#region Private
|
|
|
|
[SerializeField] private List<StructureBase> activeBuildings;
|
|
private Vector3 _tempVec;
|
|
private Plane _buildPlane;
|
|
private StructureBase _tempSb;
|
|
|
|
#endregion
|
|
|
|
|
|
protected override void OnEnable()
|
|
{
|
|
base.OnEnable();
|
|
activeBuildings.Clear();
|
|
camera = Camera.main;
|
|
}
|
|
|
|
protected override void OnClick(InputAction.CallbackContext a)
|
|
{
|
|
PlaceBuilding();
|
|
}
|
|
|
|
private void PlaceBuilding()
|
|
{
|
|
_buildPlane = new Plane(Vector3.up, Vector3.zero);
|
|
Ray ray = camera.ScreenPointToRay(Mouse.current.position.ReadValue());
|
|
if (_buildPlane.Raycast(ray, out float distance))
|
|
{
|
|
_tempVec = ray.GetPoint(distance);
|
|
}
|
|
|
|
_tempSb = Instantiate(buildings[buildingSelector], _tempVec, Quaternion.identity, transform);
|
|
|
|
activeBuildings.Add(_tempSb);
|
|
}
|
|
}
|
|
} |