Create object pooling system
Optimize VFX and builder handling mechanics
This commit is contained in:
parent
e727c12ec1
commit
a52bbb0849
@ -119,6 +119,19 @@ TextureImporter:
|
|||||||
ignorePlatformSupport: 0
|
ignorePlatformSupport: 0
|
||||||
androidETC2FallbackOverride: 0
|
androidETC2FallbackOverride: 0
|
||||||
forceMaximumCompressionQuality_BC6H_BC7: 0
|
forceMaximumCompressionQuality_BC6H_BC7: 0
|
||||||
|
- serializedVersion: 4
|
||||||
|
buildTarget: WebGL
|
||||||
|
maxTextureSize: 2048
|
||||||
|
resizeAlgorithm: 0
|
||||||
|
textureFormat: -1
|
||||||
|
textureCompression: 1
|
||||||
|
compressionQuality: 50
|
||||||
|
crunchedCompression: 0
|
||||||
|
allowsAlphaSplitting: 0
|
||||||
|
overridden: 0
|
||||||
|
ignorePlatformSupport: 0
|
||||||
|
androidETC2FallbackOverride: 0
|
||||||
|
forceMaximumCompressionQuality_BC6H_BC7: 0
|
||||||
spriteSheet:
|
spriteSheet:
|
||||||
serializedVersion: 2
|
serializedVersion: 2
|
||||||
sprites:
|
sprites:
|
||||||
|
|||||||
102767
Assets/Scenes/HomeTown.unity
102767
Assets/Scenes/HomeTown.unity
File diff suppressed because it is too large
Load Diff
@ -1,129 +1,113 @@
|
|||||||
using NUnit.Framework.Constraints;
|
//using NUnit.Framework.Constraints;
|
||||||
using System;
|
//using System;
|
||||||
using System.Collections;
|
//using System.Collections;
|
||||||
using System.Collections.Generic;
|
//using System.Collections.Generic;
|
||||||
using System.Linq;
|
//using System.Linq;
|
||||||
using UnityEngine;
|
//using UnityEngine;
|
||||||
using UnityEngine.EventSystems;
|
//using UnityEngine.EventSystems;
|
||||||
using UnityEngine.Tilemaps;
|
//using UnityEngine.Tilemaps;
|
||||||
using UnityEngine.UI;
|
//using UnityEngine.UI;
|
||||||
using static Unity.Cinemachine.IInputAxisOwner.AxisDescriptor;
|
//using static Unity.Cinemachine.IInputAxisOwner.AxisDescriptor;
|
||||||
using static UnityEngine.RuleTile.TilingRuleOutput;
|
//using static UnityEngine.RuleTile.TilingRuleOutput;
|
||||||
|
|
||||||
[CreateAssetMenu(menuName = "Custom/BuilderHandler", fileName = "NewBuilderHandler")]
|
//[CreateAssetMenu(menuName = "Custom/BuilderHandler", fileName = "NewBuilderHandler")]
|
||||||
public class BuildHandler : ScriptableObject {
|
//public class BuildHandler : ScriptableObject {
|
||||||
[Header("Build Dependencies")]
|
// [Header("Build Dependencies")]
|
||||||
public GameObject PrefabToSpawn;
|
// public GameObject PrefabToSpawn;
|
||||||
public GameObject PrefabToSpawnPreviewAsset;
|
// public GameObject PrefabToSpawnPreviewAsset;
|
||||||
|
|
||||||
[Header("VFX Dependencies")]
|
// [Header("VFX Dependencies")]
|
||||||
public VfxHandlerBase Sparkle;
|
// public VfxHandlerBase Sparkle;
|
||||||
|
|
||||||
private GameObject PreviewObj;
|
// private GameObject PreviewObj;
|
||||||
private Vector3 SnappedPosition;
|
// private Vector3 SnappedPosition;
|
||||||
private bool BuildMode = false;
|
// private bool BuildMode = false;
|
||||||
private Vector2 SpriteSize;
|
// private Vector2 SpriteSize;
|
||||||
private CapsuleDirection2D CapsuleDirection = CapsuleDirection2D.Vertical;
|
// private CapsuleDirection2D CapsuleDirection = CapsuleDirection2D.Vertical;
|
||||||
|
|
||||||
|
|
||||||
private void OnEnable() {
|
// private void OnEnable() {
|
||||||
InitializePreviewObj();
|
// InitializePreviewObj();
|
||||||
}
|
// }
|
||||||
|
|
||||||
private void InitializePreviewObj() {
|
// private void InitializePreviewObj() {
|
||||||
PreviewObj = Instantiate(PrefabToSpawnPreviewAsset, new Vector3(), Quaternion.identity);
|
// PreviewObj = Instantiate(PrefabToSpawnPreviewAsset, new Vector3(), Quaternion.identity);
|
||||||
PreviewObj.SetActive(false);
|
// PreviewObj.SetActive(false);
|
||||||
SpriteRenderer sprite = PreviewObj.GetComponent<SpriteRenderer>();
|
// SpriteRenderer sprite = PreviewObj.GetComponent<SpriteRenderer>();
|
||||||
Sprite s = PreviewObj.GetComponent<SpriteRenderer>().sprite;
|
// Sprite s = PreviewObj.GetComponent<SpriteRenderer>().sprite;
|
||||||
Vector2 pivot = s.pivot / s.pixelsPerUnit;
|
// Vector2 pivot = s.pivot / s.pixelsPerUnit;
|
||||||
SpriteSize = s.rect.size / s.pixelsPerUnit;
|
// SpriteSize = s.rect.size / s.pixelsPerUnit;
|
||||||
if (SpriteSize.x > SpriteSize.y)
|
// if (SpriteSize.x > SpriteSize.y)
|
||||||
CapsuleDirection = CapsuleDirection2D.Horizontal;
|
// CapsuleDirection = CapsuleDirection2D.Horizontal;
|
||||||
}
|
// }
|
||||||
|
|
||||||
public void SetBuildMode(bool mode) {
|
// public void SetBuildMode(bool mode) {
|
||||||
if (PreviewObj == null) InitializePreviewObj();
|
// if (PreviewObj == null) InitializePreviewObj();
|
||||||
PreviewObj.SetActive(mode);
|
// PreviewObj.SetActive(mode);
|
||||||
BuildMode = mode;
|
// BuildMode = mode;
|
||||||
}
|
// }
|
||||||
|
|
||||||
Vector3 GetSnappedPosition(Vector3 worldPos, float gridSize) {
|
// Vector3 GetSnappedPosition(Vector3 worldPos, float gridSize) {
|
||||||
float x = Mathf.Round(worldPos.x / gridSize) * gridSize;
|
// float x = Mathf.Round(worldPos.x / gridSize) * gridSize;
|
||||||
float y = Mathf.Round(worldPos.y / gridSize) * gridSize;
|
// float y = Mathf.Round(worldPos.y / gridSize) * gridSize;
|
||||||
return new Vector3(x, y, 0);
|
// return new Vector3(x, y, 0);
|
||||||
}
|
// }
|
||||||
|
|
||||||
public void UpdatePreview(float gridSize) {
|
// public void UpdatePreview(float gridSize) {
|
||||||
if (!BuildMode) return;
|
// if (!BuildMode) return;
|
||||||
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
// Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||||
Vector3 offset = new Vector3(0, 0, 3);
|
// Vector3 offset = new Vector3(0, 0, 3);
|
||||||
mousePos += offset;
|
// mousePos += offset;
|
||||||
SnappedPosition = GetSnappedPosition(mousePos, gridSize / 2);
|
// SnappedPosition = GetSnappedPosition(mousePos, gridSize / 2);
|
||||||
PreviewObj.transform.position = SnappedPosition;
|
// PreviewObj.transform.position = SnappedPosition;
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
public void PlaceObject() {
|
// public void PlaceObject() {
|
||||||
if (!BuildMode) return;
|
// if (!BuildMode) return;
|
||||||
if (!IsTileUnderMouse()) 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Sparkle?.PlayAll(SnappedPosition);
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void DrawGizmos() {
|
|
||||||
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) {
|
// Sparkle?.PlayAll(SnappedPosition);
|
||||||
Vector2 top = center + Vector2.up * (height / 2 - radius);
|
// GameObject newObj = Instantiate(PrefabToSpawn, SnappedPosition, Quaternion.identity);
|
||||||
Vector2 bottom = center + Vector2.down * (height / 2 - radius);
|
// SpriteRenderer image = newObj.GetComponent<SpriteRenderer>();
|
||||||
|
// }
|
||||||
|
|
||||||
Gizmos.color = Color.green;
|
// public bool IsTileUnderMouse() {
|
||||||
Gizmos.DrawWireCube(center, new Vector2(size.x, height - 2 * radius));
|
// Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||||
Gizmos.DrawWireSphere(top, radius);
|
// worldPos.z = 0f;
|
||||||
Gizmos.DrawWireSphere(bottom, radius);
|
|
||||||
} else {
|
// GameObject groundGO = GameObject.FindGameObjectWithTag("Ground");
|
||||||
Gizmos.color = Color.green;
|
// if (!groundGO) return false;
|
||||||
Gizmos.DrawWireSphere(center, radius);
|
|
||||||
}
|
// 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;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// public void DrawGizmos() {
|
||||||
|
// 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);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|||||||
17
Assets/Scriptables/Player/PlayerBuilderSettings.asset
Normal file
17
Assets/Scriptables/Player/PlayerBuilderSettings.asset
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
%YAML 1.1
|
||||||
|
%TAG !u! tag:unity3d.com,2011:
|
||||||
|
--- !u!114 &11400000
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 0}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: 35818ec7b9b5656418c9f966ac9dea4c, type: 3}
|
||||||
|
m_Name: PlayerBuilderSettings
|
||||||
|
m_EditorClassIdentifier:
|
||||||
|
ObjToSpawn: {fileID: 3569570202871523113, guid: bb6fb8e00f9331e4e8f33f5c2563ba8e, type: 3}
|
||||||
|
ObjAsPreview: {fileID: 3569570202871523113, guid: 997fb62ab3397bf4dbae538691a1d668, type: 3}
|
||||||
|
Vfx: {fileID: 4566952472781634859, guid: 4d965c8262ff5834a878255a7345baac, type: 3}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 3e3bb42a7bad3df48b92c65f5e38b160
|
||||||
|
NativeFormatImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
mainObjectFileID: 11400000
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -10,15 +10,15 @@ using UnityEngine.UI;
|
|||||||
|
|
||||||
[SelectionBase]
|
[SelectionBase]
|
||||||
public class Player : MonoBehaviour {
|
public class Player : MonoBehaviour {
|
||||||
[Header("Asset/Prefab Dependencies")]
|
[Header("Asset/Prefab")]
|
||||||
[SerializeField] public BuildHandler Build;
|
[SerializeField] public BuilderHandlerRunner Builder;
|
||||||
|
|
||||||
[Header("Movement Dependencies")]
|
[Header("Movement")]
|
||||||
[SerializeField] private Rigidbody2D RigidBody;
|
[SerializeField] private Rigidbody2D RigidBody;
|
||||||
[SerializeField] private Animator Animator;
|
[SerializeField] private Animator Animator;
|
||||||
[SerializeField] private SpriteRenderer Renderer;
|
[SerializeField] private SpriteRenderer Renderer;
|
||||||
|
|
||||||
[Header("Character Class Dependencies")]
|
[Header("Character Class")]
|
||||||
[SerializeField] private ParticleSystem Aura;
|
[SerializeField] private ParticleSystem Aura;
|
||||||
[SerializeField] private GameObject[] ClassIndicators;
|
[SerializeField] private GameObject[] ClassIndicators;
|
||||||
|
|
||||||
@ -28,8 +28,9 @@ public class Player : MonoBehaviour {
|
|||||||
[SerializeField] private float DriftSpeed = 60;
|
[SerializeField] private float DriftSpeed = 60;
|
||||||
[SerializeField] private float DriftFactorial = 0.85f;
|
[SerializeField] private float DriftFactorial = 0.85f;
|
||||||
|
|
||||||
[Header("VFX Dependencies")]
|
[Header("VFX")]
|
||||||
public VfxHandlerBase DashEffect;
|
[SerializeField] private GameObject VfxDash;
|
||||||
|
private VfxHandlerBase VfxDashHandler;
|
||||||
|
|
||||||
private Vector2 MoveDirection = Vector2.zero;
|
private Vector2 MoveDirection = Vector2.zero;
|
||||||
private Directions FaceDir = Directions.Down;
|
private Directions FaceDir = Directions.Down;
|
||||||
@ -45,14 +46,14 @@ public class Player : MonoBehaviour {
|
|||||||
private enum Directions { Left, Right, Up, Down }
|
private enum Directions { Left, Right, Up, Down }
|
||||||
|
|
||||||
void Awake() {
|
void Awake() {
|
||||||
//Build.Initialize();
|
Builder = GetComponent<BuilderHandlerRunner>();
|
||||||
|
VfxDashHandler = new VfxHandlerBase(VfxDash, 5, 5);
|
||||||
SetClass(1);
|
SetClass(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Update() {
|
private void Update() {
|
||||||
GatherInput();
|
GatherInput();
|
||||||
KeyPressActions();
|
KeyPressActions();
|
||||||
Build.UpdatePreview(1);
|
|
||||||
}
|
}
|
||||||
private void GatherInput() {
|
private void GatherInput() {
|
||||||
MoveDirection.x = Input.GetAxisRaw("Horizontal");
|
MoveDirection.x = Input.GetAxisRaw("Horizontal");
|
||||||
@ -71,10 +72,6 @@ public class Player : MonoBehaviour {
|
|||||||
} else if (Input.GetKeyDown(KeyCode.F4)) {
|
} else if (Input.GetKeyDown(KeyCode.F4)) {
|
||||||
SetClass(3);
|
SetClass(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Input.GetMouseButtonDown(0)) {
|
|
||||||
Build.PlaceObject();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Dash(Vector2 direction) {
|
private void Dash(Vector2 direction) {
|
||||||
@ -84,14 +81,14 @@ public class Player : MonoBehaviour {
|
|||||||
RigidBody.linearVelocity = direction.normalized * MoveSpeed * DashMultiplier;
|
RigidBody.linearVelocity = direction.normalized * MoveSpeed * DashMultiplier;
|
||||||
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 180;
|
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 180;
|
||||||
if (direction.normalized != Vector2.zero)
|
if (direction.normalized != Vector2.zero)
|
||||||
DashEffect?.PlayAll(this.transform.position, Quaternion.Euler(0f, 0f, angle));
|
VfxDashHandler.PlayAll(this.transform.position, Quaternion.Euler(0f, 0f, angle));
|
||||||
DriftDirection = direction.normalized;
|
DriftDirection = direction.normalized;
|
||||||
Drift = DriftSpeed;
|
Drift = DriftSpeed;
|
||||||
isDashing = false;
|
isDashing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetClass(int classIdx) {
|
private void SetClass(int classIdx) {
|
||||||
Build.SetBuildMode(classIdx == 3);
|
Builder.SetBuildMode(classIdx == 3);
|
||||||
|
|
||||||
foreach (var (indicator, i) in ClassIndicators.Select((obj, i) => (obj, i)))
|
foreach (var (indicator, i) in ClassIndicators.Select((obj, i) => (obj, i)))
|
||||||
if (i != classIdx) {
|
if (i != classIdx) {
|
||||||
@ -122,7 +119,6 @@ public class Player : MonoBehaviour {
|
|||||||
if (Drift > 0.2f) {
|
if (Drift > 0.2f) {
|
||||||
RigidBody.linearVelocity += DriftDirection * Drift;
|
RigidBody.linearVelocity += DriftDirection * Drift;
|
||||||
Drift *= DriftFactorial;
|
Drift *= DriftFactorial;
|
||||||
print(Drift);
|
|
||||||
} else {
|
} else {
|
||||||
Drift = 0f;
|
Drift = 0f;
|
||||||
}
|
}
|
||||||
@ -145,7 +141,6 @@ public class Player : MonoBehaviour {
|
|||||||
|
|
||||||
|
|
||||||
void OnDrawGizmos() {
|
void OnDrawGizmos() {
|
||||||
Build.DrawGizmos();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
8
Assets/Scripts/Runtime/Characters/Player.meta
Normal file
8
Assets/Scripts/Runtime/Characters/Player.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: a15e0de9d5de68f44801e1cd226828ad
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
8
Assets/Scripts/Runtime/Characters/Player/Building.meta
Normal file
8
Assets/Scripts/Runtime/Characters/Player/Building.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 68fd86b597472bb409f75d9ff3db1bee
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -0,0 +1,127 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.Tilemaps;
|
||||||
|
|
||||||
|
public class BuilderHandlerRunner : MonoBehaviour {
|
||||||
|
public BuilderSettings Settings;
|
||||||
|
private GameObjectPool GameObjPool;
|
||||||
|
|
||||||
|
private GameObject PreviewObj;
|
||||||
|
private bool BuildMode = false;
|
||||||
|
private Vector2 SpriteSize;
|
||||||
|
private CapsuleDirection2D CapsuleDir;
|
||||||
|
private float GridSize = 0.5f;
|
||||||
|
private Collider2D[] bufferSmall = new Collider2D[10];
|
||||||
|
private Collider2D[] bufferLarge = new Collider2D[20];
|
||||||
|
private ContactFilter2D ContactFilter;
|
||||||
|
private Vector3 SnappedPos;
|
||||||
|
private VfxHandlerBase VfxHandler;
|
||||||
|
|
||||||
|
void Awake() {
|
||||||
|
GameObjPool = new GameObjectPool(Settings.ObjToSpawn);
|
||||||
|
PreviewObj = Instantiate(Settings.ObjAsPreview);
|
||||||
|
PreviewObj.SetActive(false);
|
||||||
|
|
||||||
|
var sr = PreviewObj.GetComponent<SpriteRenderer>();
|
||||||
|
var s = sr.sprite;
|
||||||
|
SpriteSize = s.rect.size / s.pixelsPerUnit;
|
||||||
|
CapsuleDir = SpriteSize.x > SpriteSize.y
|
||||||
|
? CapsuleDirection2D.Horizontal
|
||||||
|
: CapsuleDirection2D.Vertical;
|
||||||
|
|
||||||
|
ContactFilter = new ContactFilter2D();
|
||||||
|
//ContactFilter.useLayerMask = true;
|
||||||
|
//ContactFilter.layerMask = LayerMask.GetMask("Structural", "Terrain"); // only check these layers
|
||||||
|
ContactFilter.useTriggers = false; // ignore triggers
|
||||||
|
ContactFilter.useDepth = true;
|
||||||
|
ContactFilter.SetDepth(-Mathf.Infinity, Mathf.Infinity);
|
||||||
|
|
||||||
|
VfxHandler = new VfxHandlerBase(Settings.Vfx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Update() {
|
||||||
|
if (!BuildMode) return;
|
||||||
|
|
||||||
|
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||||
|
mousePos.z = 0;
|
||||||
|
SnappedPos = new Vector3(
|
||||||
|
Mathf.Round(mousePos.x / GridSize) * GridSize,
|
||||||
|
Mathf.Round(mousePos.y / GridSize) * GridSize,
|
||||||
|
0f);
|
||||||
|
PreviewObj.transform.position = SnappedPos;
|
||||||
|
|
||||||
|
if (Input.GetMouseButtonDown(0)) PlaceObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void SetBuildMode(bool on) {
|
||||||
|
BuildMode = on;
|
||||||
|
PreviewObj.SetActive(on);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlaceObject() {
|
||||||
|
if (InvalidPlacement()) return;
|
||||||
|
|
||||||
|
|
||||||
|
VfxHandler.PlayAll(SnappedPos);
|
||||||
|
var obj = GameObjPool.Get(SnappedPos, Quaternion.identity);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool InvalidPlacement() {
|
||||||
|
//Collider2D[] hits = Physics2D.OverlapCapsuleAll(pos, SpriteSize * 0.8f, CapsuleDir, 0);
|
||||||
|
//foreach (var hit in hits)
|
||||||
|
// if (hit.CompareTag("Structural"))
|
||||||
|
// return false;
|
||||||
|
|
||||||
|
|
||||||
|
//hits = Physics2D.OverlapCapsuleAll(pos, SpriteSize, CapsuleDir, 0);
|
||||||
|
//foreach (var hit in hits)
|
||||||
|
// if (hit.GetComponent<TilemapCollider2D>())
|
||||||
|
// return false;
|
||||||
|
|
||||||
|
int count = Physics2D.OverlapCapsule(SnappedPos, SpriteSize * 0.8f, CapsuleDir, 0f, ContactFilter, bufferSmall);
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
if (bufferSmall[i].CompareTag("Structural"))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
count = Physics2D.OverlapCapsule(SnappedPos, SpriteSize, CapsuleDir, 0f, ContactFilter, bufferLarge);
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
if (bufferLarge[i].GetComponent<TilemapCollider2D>())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
|
||||||
|
Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||||
|
worldPos.z = 0f;
|
||||||
|
|
||||||
|
GameObject groundGO = GameObject.FindGameObjectWithTag("Ground");
|
||||||
|
if (!groundGO) return true;
|
||||||
|
|
||||||
|
Tilemap tilemap = groundGO.GetComponent<Tilemap>();
|
||||||
|
if (!tilemap) return true;
|
||||||
|
|
||||||
|
Vector3Int cellPos = tilemap.WorldToCell(worldPos);
|
||||||
|
TileBase tile = tilemap.GetTile(cellPos);
|
||||||
|
if (!tile) return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnDrawGizmos() {
|
||||||
|
Vector2 center = SnappedPos;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 21acbec1ba673c54eb910f3f98a4dea8
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
[CreateAssetMenu(menuName = "Custom/BuilderSettings")]
|
||||||
|
public class BuilderSettings : ScriptableObject {
|
||||||
|
[Header("Build Prefabs")]
|
||||||
|
public GameObject ObjToSpawn;
|
||||||
|
public GameObject ObjAsPreview;
|
||||||
|
|
||||||
|
[Header("VFX")]
|
||||||
|
public GameObject Vfx;
|
||||||
|
}
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 35818ec7b9b5656418c9f966ac9dea4c
|
||||||
8
Assets/Scripts/Runtime/Environment.meta
Normal file
8
Assets/Scripts/Runtime/Environment.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1cc6243257d2cb84e8758d84afa696ff
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
33
Assets/Scripts/Runtime/Environment/GameObjectPool.cs
Normal file
33
Assets/Scripts/Runtime/Environment/GameObjectPool.cs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.Pool;
|
||||||
|
|
||||||
|
public class GameObjectPool {
|
||||||
|
private ObjectPool<GameObject> Pool;
|
||||||
|
|
||||||
|
public GameObjectPool(GameObject prefab, int defaultSize = 10, int maxSize = 50) {
|
||||||
|
var container = new GameObject(prefab.name + " Pool");
|
||||||
|
Pool = new ObjectPool<GameObject>(
|
||||||
|
createFunc: () => {
|
||||||
|
var obj = Object.Instantiate(prefab, container.transform);
|
||||||
|
obj.SetActive(false);
|
||||||
|
return obj;
|
||||||
|
},
|
||||||
|
actionOnGet: obj => { obj.SetActive(true); },
|
||||||
|
actionOnRelease: obj => { obj.SetActive(false); },
|
||||||
|
actionOnDestroy: obj => Object.Destroy(obj),
|
||||||
|
collectionCheck: true,
|
||||||
|
defaultCapacity: defaultSize,
|
||||||
|
maxSize: maxSize
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GameObject Get(Vector3 position, Quaternion rotation) {
|
||||||
|
var obj = Pool.Get();
|
||||||
|
obj.transform.SetPositionAndRotation(position, rotation);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Release(GameObject obj) => Pool.Release(obj);
|
||||||
|
}
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: a2cabd94ce146e24e889dfdbc6777c09
|
||||||
77
Assets/Scripts/Runtime/Environment/VfxHandler.cs
Normal file
77
Assets/Scripts/Runtime/Environment/VfxHandler.cs
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Unity.VisualScripting;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.Pool;
|
||||||
|
|
||||||
|
|
||||||
|
public class VfxHandlerBase {
|
||||||
|
private List<ObjectPool<ParticleSystem>> ParticleSystemPool = new List<ObjectPool<ParticleSystem>>();
|
||||||
|
private GameObject PoolParentContainer;
|
||||||
|
|
||||||
|
public VfxHandlerBase(GameObject vfx, int defaultCap = 10, int maxCap = 50) {
|
||||||
|
if (!vfx) { Debug.LogError($"You did not pass vfx...."); }
|
||||||
|
PoolParentContainer = new GameObject($"{vfx.name} Pool");
|
||||||
|
var particleSystems = vfx.GetComponentsInChildren<ParticleSystem>();
|
||||||
|
particleSystems.ToListPooled().ForEach(x => GenerateVfxPool(x, defaultCap, maxCap));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void PlayAll(Vector3? position = null, Quaternion? rotation = null) {
|
||||||
|
if (position == null) position = Vector3.zero;
|
||||||
|
if (rotation == null) rotation = Quaternion.Euler(0, 0, 0);
|
||||||
|
|
||||||
|
Get((Vector3)position, (Quaternion)rotation);
|
||||||
|
|
||||||
|
//var particleSystems = obj.GetComponentsInChildren<ParticleSystem>();
|
||||||
|
//particleSystems.ToListPooled().ForEach((x) => x.Play());
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ParticleSystem> Get(Vector3 position, Quaternion rotation) {
|
||||||
|
var particleSystems = ParticleSystemPool.ToList().Select(x => x.Get()).ToList();
|
||||||
|
particleSystems.ForEach(x => {
|
||||||
|
x.transform.SetPositionAndRotation(position, rotation);
|
||||||
|
x.Play();
|
||||||
|
});
|
||||||
|
return particleSystems;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateVfxPool(ParticleSystem vfx, int defaultCap = 10, int maxCap = 50) {
|
||||||
|
if (!vfx) { Debug.LogError($"You did not pass vfx...."); }
|
||||||
|
ObjectPool<ParticleSystem> pool = null;
|
||||||
|
pool = new ObjectPool<ParticleSystem>(
|
||||||
|
createFunc: () => {
|
||||||
|
var inst = Object.Instantiate(vfx, PoolParentContainer.transform);
|
||||||
|
inst.gameObject.SetActive(false);
|
||||||
|
var main = inst.main;
|
||||||
|
main.loop = false;
|
||||||
|
main.stopAction = ParticleSystemStopAction.Callback;
|
||||||
|
inst.gameObject.AddComponent<ReturnToPool>().Initialize(pool);
|
||||||
|
return inst;
|
||||||
|
},
|
||||||
|
actionOnGet: ps => ps.gameObject.SetActive(true),
|
||||||
|
actionOnRelease: ps => {
|
||||||
|
ps.Clear();
|
||||||
|
ps.gameObject.SetActive(false);
|
||||||
|
},
|
||||||
|
//actionOnDestroy: ps => Object.Destroy(ps.gameObject),
|
||||||
|
collectionCheck: true,
|
||||||
|
defaultCapacity: defaultCap,
|
||||||
|
maxSize: maxCap
|
||||||
|
);
|
||||||
|
|
||||||
|
ParticleSystemPool.Add(pool);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class ReturnToPool : MonoBehaviour {
|
||||||
|
private ObjectPool<ParticleSystem> pool;
|
||||||
|
|
||||||
|
public void Initialize(ObjectPool<ParticleSystem> pool) {
|
||||||
|
this.pool = pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnParticleSystemStopped() {
|
||||||
|
pool.Release(GetComponent<ParticleSystem>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,34 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using Unity.VisualScripting;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
[CreateAssetMenu(menuName = "Custom/VfxHandler", fileName = "NewVfxHandler")]
|
|
||||||
public class VfxHandlerBase : ScriptableObject {
|
|
||||||
[Header("VFX Dependencies")]
|
|
||||||
public GameObject VfxExplosion;
|
|
||||||
private GameObject VfxExplosionParentInstance;
|
|
||||||
private List<ParticleSystem> VfxExplosionParticles = new List<ParticleSystem>();
|
|
||||||
|
|
||||||
|
|
||||||
private void OnEnable() {
|
|
||||||
GenerateInstance();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GenerateInstance() {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void PlayAll(Vector3? location = null, Quaternion? rotation = null) {
|
|
||||||
if (!VfxExplosionParentInstance) GenerateInstance();
|
|
||||||
|
|
||||||
if (location != null) VfxExplosionParentInstance.transform.position = (Vector3)location;
|
|
||||||
if (rotation != null) VfxExplosionParentInstance.transform.rotation = (Quaternion)rotation;
|
|
||||||
VfxExplosionParticles.ForEach((x) => x.Play());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -6,7 +6,7 @@ EditorBuildSettings:
|
|||||||
serializedVersion: 2
|
serializedVersion: 2
|
||||||
m_Scenes:
|
m_Scenes:
|
||||||
- enabled: 1
|
- enabled: 1
|
||||||
path: Assets/_GAME_/Scenes/HomeTown.unity
|
path: Assets/Scenes/HomeTown.unity
|
||||||
guid: 2cda990e2423bbf4892e6590ba056729
|
guid: 2cda990e2423bbf4892e6590ba056729
|
||||||
m_configObjects:
|
m_configObjects:
|
||||||
com.unity.input.settings.actions: {fileID: -944628639613478452, guid: 3590b91b4603b465dbb4216d601bff33, type: 3}
|
com.unity.input.settings.actions: {fileID: -944628639613478452, guid: 3590b91b4603b465dbb4216d601bff33, type: 3}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user