153 lines
5.0 KiB
C#
153 lines
5.0 KiB
C#
using NUnit.Framework.Constraints;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.Tilemaps;
|
|
using UnityEngine.UI;
|
|
using static Unity.Cinemachine.IInputAxisOwner.AxisDescriptor;
|
|
using static UnityEngine.RuleTile.TilingRuleOutput;
|
|
|
|
[CreateAssetMenu(menuName = "Custom/BuilderHandler", fileName = "NewBuilderHandler")]
|
|
public class BuildHandler : ScriptableObject {
|
|
[Header("Build Dependencies")]
|
|
public GameObject PrefabToSpawn;
|
|
public GameObject PrefabToSpawnPreviewAsset;
|
|
|
|
[Header("VFX Dependencies")]
|
|
public GameObject VfxExplosion;
|
|
private GameObject VfxExplosionParentInstance;
|
|
private List<ParticleSystem> VfxExplosionParticles = new List<ParticleSystem>();
|
|
|
|
private GameObject PreviewObj;
|
|
private Vector3 SnappedPosition;
|
|
private bool BuildMode = false;
|
|
private Vector2 SpriteSize;
|
|
private CapsuleDirection2D CapsuleDirection = CapsuleDirection2D.Vertical;
|
|
private bool Initialized = false;
|
|
|
|
void Awake() {
|
|
Debug.Log(PreviewObj.name);
|
|
}
|
|
|
|
public void Initialize() {
|
|
PreviewObj = Instantiate(PrefabToSpawnPreviewAsset, new Vector3(), Quaternion.identity);
|
|
SpriteRenderer sprite = PreviewObj.GetComponent<SpriteRenderer>();
|
|
Sprite s = PreviewObj.GetComponent<SpriteRenderer>().sprite;
|
|
Vector2 pivot = s.pivot / s.pixelsPerUnit;
|
|
SpriteSize = s.rect.size / s.pixelsPerUnit;
|
|
if (SpriteSize.x > SpriteSize.y)
|
|
CapsuleDirection = CapsuleDirection2D.Horizontal;
|
|
|
|
VfxExplosionParticles.Clear();
|
|
if (VfxExplosion) {
|
|
VfxExplosionParentInstance = Instantiate(VfxExplosion, Vector3.one, Quaternion.identity);
|
|
var foundParticleSystems = VfxExplosionParentInstance.GetComponentsInChildren<ParticleSystem>();
|
|
VfxExplosionParticles.AddRange(foundParticleSystems);
|
|
VfxExplosionParticles.ForEach((x) => x.transform.localScale = Vector3.one);
|
|
}
|
|
|
|
Initialized = true;
|
|
}
|
|
|
|
public void SetBuildMode(bool mode) {
|
|
//if (!PreviewObj)
|
|
// CreatePreviewObj();
|
|
PreviewObj.SetActive(mode);
|
|
BuildMode = mode;
|
|
if (mode) Debug.Log("BuildMode ON");
|
|
}
|
|
|
|
Vector3 GetSnappedPosition(Vector3 worldPos, float gridSize) {
|
|
float x = Mathf.Round(worldPos.x / gridSize) * gridSize;
|
|
float y = Mathf.Round(worldPos.y / gridSize) * gridSize;
|
|
//Debug.Log($"{x}, {y}");
|
|
return new Vector3(x, y, 0);
|
|
}
|
|
|
|
public void UpdatePreview(float gridSize) {
|
|
if (!BuildMode) return;
|
|
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
|
Vector3 offset = new Vector3(0, 0, 3);
|
|
mousePos += offset;
|
|
SnappedPosition = GetSnappedPosition(mousePos, gridSize / 2);
|
|
PreviewObj.transform.position = SnappedPosition;
|
|
}
|
|
|
|
|
|
public void PlaceObject() {
|
|
if (!BuildMode) return;
|
|
if (!IsTileUnderMouse()) return;
|
|
|
|
Collider2D[] hits = Physics2D.OverlapCapsuleAll(SnappedPosition, SpriteSize * 0.8f, CapsuleDirection, 0);
|
|
foreach (var hit in hits) {
|
|
if (hit.CompareTag("Structural")) {
|
|
Debug.Log("Hit building object");
|
|
return;
|
|
} else {
|
|
Debug.Log($"Tag:{hit.tag}");
|
|
}
|
|
}
|
|
|
|
hits = Physics2D.OverlapCapsuleAll(SnappedPosition, SpriteSize, CapsuleDirection, 0);
|
|
foreach (var hit in hits) {
|
|
if (hit.GetComponent<TilemapCollider2D>()) {
|
|
Debug.Log("Hit terrain tile");
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (VfxExplosionParentInstance) {
|
|
VfxExplosionParentInstance.transform.position = SnappedPosition;
|
|
VfxExplosionParticles.ForEach((x) => x.Play());
|
|
}
|
|
GameObject newObj = Instantiate(PrefabToSpawn, SnappedPosition, Quaternion.identity);
|
|
SpriteRenderer image = newObj.GetComponent<SpriteRenderer>();
|
|
}
|
|
|
|
public bool IsTileUnderMouse() {
|
|
Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
|
worldPos.z = 0f;
|
|
|
|
GameObject groundGO = GameObject.FindGameObjectWithTag("Ground");
|
|
if (!groundGO) return false;
|
|
|
|
Tilemap tilemap = groundGO.GetComponent<Tilemap>();
|
|
if (!tilemap) return false;
|
|
|
|
Vector3Int cellPos = tilemap.WorldToCell(worldPos);
|
|
TileBase tile = tilemap.GetTile(cellPos);
|
|
if (!tile) return false;
|
|
return tile;
|
|
//Bounds tileBounds = tilemap.GetBoundsLocal(cellPos);
|
|
//Collider2D[] hits = Physics2D.OverlapBoxAll(tilemap.GetCellCenterWorld(cellPos), tilemap.cellSize * 0.9f, 0f);
|
|
|
|
//return (hits.Length == 0);
|
|
}
|
|
|
|
|
|
public void DrawGizmos() {
|
|
if (!Initialized) return;
|
|
|
|
Vector2 center = SnappedPosition;
|
|
Vector2 size = SpriteSize;
|
|
float radius = size.x / 2 * 0.8f;
|
|
float height = size.y / 2 * 0.8f;
|
|
|
|
if (SpriteSize.x != SpriteSize.y) {
|
|
Vector2 top = center + Vector2.up * (height / 2 - radius);
|
|
Vector2 bottom = center + Vector2.down * (height / 2 - radius);
|
|
|
|
Gizmos.color = Color.green;
|
|
Gizmos.DrawWireCube(center, new Vector2(size.x, height - 2 * radius));
|
|
Gizmos.DrawWireSphere(top, radius);
|
|
Gizmos.DrawWireSphere(bottom, radius);
|
|
} else {
|
|
Gizmos.color = Color.green;
|
|
Gizmos.DrawWireSphere(center, radius);
|
|
}
|
|
}
|
|
}
|