enlevé besoin d'un placeholder statique par unit
problème : créer un placeholder par unit allait être un sale hassle solution : maintenant, le placeholder est créé dynamiquement note : also, j'ai ajouté un système pour ajouter des tiles
This commit is contained in:
parent
c680713526
commit
8dc77e1772
File diff suppressed because it is too large
Load Diff
@ -8,8 +8,7 @@ public abstract class DraggablePlaceholder : MonoBehaviour
|
||||
protected Color _validColor = Color.green;
|
||||
[SerializeField]
|
||||
protected Color _invalidColor = Color.red;
|
||||
[SerializeField]
|
||||
protected Transform _outline;
|
||||
public Transform Outline { get; set; }
|
||||
|
||||
protected Camera _mainCamCache;
|
||||
protected Rect _lvlBoundsCache;
|
||||
@ -44,7 +43,7 @@ public abstract class DraggablePlaceholder : MonoBehaviour
|
||||
|
||||
protected virtual void UpdatePosition()
|
||||
{
|
||||
var mousePos = Vector3Int.RoundToInt(_mainCamCache.ScreenToWorldPoint(Input.mousePosition) - new Vector3(0.5f, 0.5f));
|
||||
var mousePos = Vector3Int.RoundToInt(_mainCamCache.ScreenToWorldPoint(Input.mousePosition));
|
||||
mousePos.z = 0;
|
||||
if (!_lvlBoundsCache.Contains(mousePos)) return;
|
||||
transform.position = mousePos;
|
||||
@ -57,7 +56,7 @@ public abstract class DraggablePlaceholder : MonoBehaviour
|
||||
/// </summary>
|
||||
public virtual bool CanBePlacedHere()
|
||||
{
|
||||
return !LevelManager.Instance.Has<ILevelObject>(obj => obj.Position.Approximately(transform.position));
|
||||
return !LevelManager.Instance.Has<ILevelObject>(obj => obj.Position.IsInTile(transform.position));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -70,7 +69,7 @@ public abstract class DraggablePlaceholder : MonoBehaviour
|
||||
{
|
||||
Color getColor() => _isOnValidPosition ? _validColor : _invalidColor;
|
||||
|
||||
foreach (var child in _outline.GetComponentsInChildren<SpriteRenderer>(true))
|
||||
foreach (var child in Outline.GetComponentsInChildren<SpriteRenderer>(true))
|
||||
{
|
||||
child.color = getColor();
|
||||
}
|
||||
|
||||
28
Assets/Scripts/GameObjectPlacementButton.cs
Normal file
28
Assets/Scripts/GameObjectPlacementButton.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class GameObjectPlacementButton : UnitPlacementButton
|
||||
{
|
||||
[SerializeField]
|
||||
private GameObject _prefab;
|
||||
protected override void Place()
|
||||
{
|
||||
var instance = Instantiate(_prefab);
|
||||
|
||||
foreach (var comp in instance.transform.GetAllComponents<Rigidbody2D>()) Destroy(comp);
|
||||
foreach (var comp in instance.transform.GetAllComponents<Collider2D>()) Destroy(comp);
|
||||
foreach (var comp in instance.transform.GetAllComponents<MonoBehaviour>()) Destroy(comp);
|
||||
|
||||
var placeholder = instance.AddComponent<ObjectDraggablePlaceholder>();
|
||||
placeholder.Prefab = _prefab;
|
||||
|
||||
var outline = new GameObject("Outline");
|
||||
outline.transform.SetParent(instance.transform);
|
||||
outline.transform.localPosition = Vector2.zero;
|
||||
|
||||
var rend = outline.AddComponent<SpriteRenderer>();
|
||||
rend.sortingOrder = 1;
|
||||
rend.sprite = Sprite.Create(new Texture2D(100, 100), new Rect(0, 0, 100, 100), Vector2.one * 0.5f);
|
||||
|
||||
placeholder.Outline = outline.transform;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/GameObjectPlacementButton.cs.meta
Normal file
11
Assets/Scripts/GameObjectPlacementButton.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c01785e9696dc5f41bd182315a19f6bf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -7,6 +7,13 @@ using GatherAndDefend.LevelEditor;
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
public static Component[] GetAllComponents<T>(this Component component) where T : Component
|
||||
{
|
||||
List<T> comps = new List<T>();
|
||||
comps.AddRange(component.GetComponents<T>());
|
||||
comps.AddRange(component.GetComponentsInChildren<T>());
|
||||
return comps.ToArray();
|
||||
}
|
||||
public static Rect CalculateBounds(this Level level)
|
||||
{
|
||||
Rect bound = new Rect(0, 0, 0, 0);
|
||||
@ -78,8 +85,9 @@ public static class Extensions
|
||||
return null;
|
||||
}
|
||||
public enum LevelObjectType { GameObject, Tile }
|
||||
public static bool Approximately(this Vector3 vect, Vector3 other)
|
||||
|
||||
public static bool IsInTile(this Vector3 vect, Vector3 tile)
|
||||
{
|
||||
return Mathf.Approximately(Vector3.Distance(vect, other), 0);
|
||||
return Vector2.Distance(vect, tile) < 0.5f;
|
||||
}
|
||||
}
|
||||
@ -10,7 +10,7 @@ public abstract class LevelTile : TileBase, ILevelObject
|
||||
{
|
||||
[SerializeField]
|
||||
private Sprite _sprite;
|
||||
|
||||
public Sprite Sprite => _sprite;
|
||||
public bool Instantiated { get; set; }
|
||||
|
||||
public Vector3 Position { get; protected set; }
|
||||
|
||||
@ -2,10 +2,9 @@
|
||||
|
||||
public class ObjectDraggablePlaceholder : DraggablePlaceholder
|
||||
{
|
||||
[SerializeField]
|
||||
private GameObject _prefab;
|
||||
public GameObject Prefab { get; set; }
|
||||
public override void Place()
|
||||
{
|
||||
var instance = Instantiate(_prefab, transform.position, Quaternion.identity);
|
||||
Prefab.Create(transform.position);
|
||||
}
|
||||
}
|
||||
@ -17,9 +17,7 @@ public class Opponent : Entity
|
||||
|
||||
_movementVector.x = -Time.deltaTime * Speed;
|
||||
|
||||
_movementVector.Normalize();
|
||||
|
||||
_rigidbody.velocity = _movementVector * Speed;
|
||||
transform.position += (Vector3)_movementVector;
|
||||
|
||||
if(IsEnemyDetected) {
|
||||
AttackEnemy();
|
||||
|
||||
@ -3,13 +3,11 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class UnitPlacementButton : MonoBehaviour, IPointerDownHandler
|
||||
public abstract class UnitPlacementButton : MonoBehaviour, IPointerDownHandler
|
||||
{
|
||||
[SerializeField, Tooltip("the gameobject we will drag")]
|
||||
private GameObject placeholder;
|
||||
|
||||
public void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
Instantiate(placeholder);
|
||||
Place();
|
||||
}
|
||||
protected abstract void Place();
|
||||
}
|
||||
|
||||
10
Assets/TilePlaceholder.cs
Normal file
10
Assets/TilePlaceholder.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class TilePlaceholder : DraggablePlaceholder
|
||||
{
|
||||
public LevelTile Tile { get; set; }
|
||||
public override void Place()
|
||||
{
|
||||
LevelManager.Instance.Get<LevelTile>().Tilemap.SetTile(Vector3Int.RoundToInt(transform.position), Tile);
|
||||
}
|
||||
}
|
||||
11
Assets/TilePlaceholder.cs.meta
Normal file
11
Assets/TilePlaceholder.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e9baa24d153f3314d8c0858eb70c5392
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
30
Assets/TilePlacementButton.cs
Normal file
30
Assets/TilePlacementButton.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class TilePlacementButton : UnitPlacementButton
|
||||
{
|
||||
public LevelTile tile;
|
||||
protected override void Place()
|
||||
{
|
||||
var placeholder = new GameObject(tile.name);
|
||||
var comp = placeholder.AddComponent<TilePlaceholder>();
|
||||
comp.Tile = tile;
|
||||
var rend = placeholder.AddComponent<SpriteRenderer>();
|
||||
rend.sprite = tile.Sprite;
|
||||
rend.sortingLayerName = "Character";
|
||||
rend.sortingOrder = 2;
|
||||
|
||||
var outline = new GameObject("Outline");
|
||||
rend = outline.AddComponent<SpriteRenderer>();
|
||||
rend.sortingLayerName = "Character";
|
||||
rend.sortingOrder = 2;
|
||||
|
||||
outline.transform.SetParent(placeholder.transform);
|
||||
outline.transform.localPosition = Vector2.zero;
|
||||
outline.transform.localScale = Vector2.one * 1.1f;
|
||||
|
||||
comp.Outline = outline.transform;
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/TilePlacementButton.cs.meta
Normal file
11
Assets/TilePlacementButton.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f3b5cc0d246c3b459ddf4ed75c4fa16
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
17
Assets/Tiles/Farm.asset
Normal file
17
Assets/Tiles/Farm.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: 84b05f2a26f63da46a1028488482d079, type: 3}
|
||||
m_Name: Farm
|
||||
m_EditorClassIdentifier:
|
||||
_sprite: {fileID: 21300000, guid: ccca3e050cb082b45af0a099790463f6, type: 3}
|
||||
_yieldPrefab: {fileID: 0}
|
||||
_yieldSpeed: 1
|
||||
@ -1,7 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ed7ee58626f3e454da1ee905c792503a
|
||||
PrefabImporter:
|
||||
guid: 7568f730b2ba3754297a7612452482e2
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 11400000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
x
Reference in New Issue
Block a user