Skip to content

Commit

Permalink
drag and drop and bench
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanOid221 committed Apr 20, 2023
1 parent 807e005 commit b844a66
Show file tree
Hide file tree
Showing 9 changed files with 1,520 additions and 314 deletions.
1,433 changes: 1,295 additions & 138 deletions Assets/Scenes/SampleScene.unity

Large diffs are not rendered by default.

64 changes: 45 additions & 19 deletions Assets/Scripts/Grid_scripts/BoardTile 1.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ GameObject:
m_Component:
- component: {fileID: 8534926583894089901}
- component: {fileID: 3820107381542920110}
- component: {fileID: 8217161798777680504}
m_Layer: 0
m_Name: Hightlight
m_TagString: Untagged
Expand Down Expand Up @@ -85,19 +84,6 @@ SpriteRenderer:
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
--- !u!65 &8217161798777680504
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 685270021815128244}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 0.9599999, y: 0.9600002, z: 0.20000005}
m_Center: {x: 0, y: 0, z: 0}
--- !u!1 &7069705522007973828
GameObject:
m_ObjectHideFlags: 0
Expand All @@ -109,7 +95,8 @@ GameObject:
- component: {fileID: 7497529776919456243}
- component: {fileID: 4698568988499940676}
- component: {fileID: -6925407272718043065}
- component: {fileID: -6569844940265849170}
- component: {fileID: 2096531744275260735}
- component: {fileID: 2084975170599696583}
m_Layer: 0
m_Name: BoardTile 1
m_TagString: Untagged
Expand Down Expand Up @@ -200,8 +187,47 @@ MonoBehaviour:
highlightSprite: {fileID: 3820107381542920110}
validColor: {r: 0, g: 0.17740631, b: 1, a: 1}
wrongColor: {r: 1, g: 0, b: 0, a: 1}
--- !u!65 &-6569844940265849170
BoxCollider:
elevation: 0
--- !u!1839735485 &2096531744275260735
Tilemap:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7069705522007973828}
m_Enabled: 1
m_Tiles: {}
m_AnimatedTiles: {}
m_TileAssetArray: []
m_TileSpriteArray: []
m_TileMatrixArray: []
m_TileColorArray: []
m_TileObjectToInstantiateArray: []
m_AnimationFrameRate: 1
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_Origin: {x: 0, y: 0, z: 0}
m_Size: {x: 0, y: 0, z: 1}
m_TileAnchor: {x: 0.5, y: 0.5, z: 0}
m_TileOrientation: 0
m_TileOrientationMatrix:
e00: 1
e01: 0
e02: 0
e03: 0
e10: 0
e11: 1
e12: 0
e13: 0
e20: 0
e21: 0
e22: 1
e23: 0
e30: 0
e31: 0
e32: 0
e33: 1
--- !u!135 &2084975170599696583
SphereCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
Expand All @@ -211,5 +237,5 @@ BoxCollider:
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 0.96, y: 0.9600002, z: 0.20000005}
m_Center: {x: 0, y: 0, z: 0}
m_Radius: 0.4800001
m_Center: {x: 0, y: 0, z: 0.28}
52 changes: 27 additions & 25 deletions Assets/Scripts/Grid_scripts/Draggable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ public class Draggable : MonoBehaviour
public Vector3 dragOffset = new Vector3(0, -0.4f, 0);

private Camera cam;

private Renderer Renderer;

private Vector3 oldPosition;
private int oldSortingOrder;
private Tile previousTile = null;
private Tile actualTile = null;

public bool IsDragging = false;

Expand All @@ -26,34 +27,20 @@ private void Start()

public void OnStartDrag()
{


oldPosition = this.transform.position;
oldSortingOrder = Renderer.sortingOrder;

Renderer.sortingOrder = 20;
IsDragging = true;
previousTile = actualTile;
}

public void OnDragging()
{
if (!IsDragging)
return;

//Debug.Log(this.name + " dragging");
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, releaseMask))
{
Vector3 newPosition = hit.point;
newPosition.z = 0;
this.transform.position = newPosition;

}
// Vector3 newPosition = cam.ScreenToWorldPoint(Input.mousePosition);



Tile tileUnder = GetTileUnder();
if (tileUnder != null)
{
Expand All @@ -65,21 +52,27 @@ public void OnDragging()
previousTile.SetHighlight(false, false);
}

previousTile = tileUnder;
//previousTile = tileUnder;
Vector3 newPosition = tileUnder.transform.position;
this.transform.position = newPosition;
actualTile = tileUnder;

}

}

public void OnEndDrag()
{
if (!IsDragging)
return;



if (!TryRelease())
{
//Nothing was found, return to original position.
this.transform.position = oldPosition;
actualTile = null;
}

if (previousTile != null)
Expand All @@ -96,22 +89,33 @@ public void OnEndDrag()
private bool TryRelease()
{
//Released over something!
Tile t = GetTileUnder();
if (t != null)

if (actualTile != null)
{
//It's a tile!
BaseUnit thisEntity = GetComponent<BaseUnit>();
Node candidateNode = GridManager.Instance.GetNodeForTile(t);
Node candidateNode = GridManager.Instance.GetNodeForTile(actualTile);
if (candidateNode != null && thisEntity != null)
{
if (!candidateNode.IsOccupied)
if (!candidateNode.IsOccupied && actualTile.team == previousTile.team)
{
//Let's move this unity to that node
thisEntity.CurrentNode.SetOccupied(false);
thisEntity.SetCurrentNode(candidateNode);
candidateNode.SetOccupied(true);
thisEntity.transform.position = candidateNode.worldPosition;

if (previousTile.isBench)
{
PlayerData.Instance.removeAtTile(candidateNode);
}
if (actualTile.isBench)
{
PlayerData.Instance.benchUnits.Add(thisEntity);

}
previousTile = actualTile;

return true;
}
}
Expand All @@ -129,17 +133,15 @@ public Tile GetTileUnder()
{
// Get the exact point on the board where the ray intersects
Vector3 pointOnBoard = hit.point;
Debug.Log(pointOnBoard);

// Find the tile that contains the point
Tile[] tiles = FindObjectsOfType<Tile>();
foreach (Tile tile in tiles)
{
Collider tileCollider = tile.GetComponent<Collider>();

if (tileCollider.bounds.Contains(pointOnBoard))
{

return tile;
}
}
Expand Down
39 changes: 21 additions & 18 deletions Assets/Scripts/Grid_scripts/GridManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,34 @@ public class GridManager : Manager<GridManager>

public Node GetFreeNode(Team forTeam)
{
int startIndex = startPositionPerTeam[forTeam];
int currentIndex = startIndex;
Tile[] tiles = FindObjectsOfType<Tile>();

tile_type type;
if (forTeam is Team.Team1)
type = tile_type.player_board;
else type = tile_type.ia_board;

while (graph.Nodes[currentIndex].IsOccupied)
foreach (Tile tile in tiles)
{

if (startIndex == 0)
if (!tile.isBench && tile.team == forTeam)
{
currentIndex++;
if (currentIndex == graph.Nodes.Count)
return null;
Node node = GetNodeForTile(tile);
if (!node.IsOccupied)
return node;
}
else
}
return null;
}

public Node GetFreeShopNode(Team forTeam)
{
Tile[] tiles = FindObjectsOfType<Tile>();

foreach(Tile tile in tiles)
{
if (tile.isBench && tile.team == forTeam)
{
currentIndex--;
if (currentIndex == -1)
return null;
Node node = GetNodeForTile(tile);
if (!node.IsOccupied)
return node;
}
}
return graph.Nodes[currentIndex];
return null;
}

public List<Node> GetNodesCloseTo(Node to)
Expand Down
3 changes: 3 additions & 0 deletions Assets/Scripts/Grid_scripts/Tile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class Tile : MonoBehaviour
public Color validColor;
public Color wrongColor;

public Team team;
public bool isBench;

public void SetHighlight(bool active, bool valid)
{
highlightSprite.gameObject.SetActive(active);
Expand Down
3 changes: 2 additions & 1 deletion Assets/Scripts/Units/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public void OnUnitBought(UnitDatabaseSO.UnitData entityData)
newEntity.gameObject.name = entityData.name;
team1Entities.Add(newEntity);

newEntity.Setup(Team.Team1, GridManager.Instance.GetFreeNode(Team.Team1));
newEntity.Setup(Team.Team1, GridManager.Instance.GetFreeShopNode(Team.Team1));
PlayerData.Instance.benchUnits.Add(newEntity);
}

public void UnitDead(BaseUnit unit)
Expand Down
14 changes: 14 additions & 0 deletions Assets/Scripts/Units/PlayerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class PlayerData : Manager<PlayerData>
public int Money { get; private set; }

public System.Action OnUpdate;
public List<BaseUnit> benchUnits = new List<BaseUnit>();

private void Start()
{
Expand All @@ -23,4 +24,17 @@ public void SpendMoney(int amount)
Money -= amount;
OnUpdate?.Invoke();
}

public void removeAtTile(Node node)
{
foreach(BaseUnit unit in benchUnits)
{
if (unit.CurrentNode == node) {
benchUnits.Remove(unit);
Debug.Log(benchUnits.Count);
}

}
}

}
2 changes: 1 addition & 1 deletion Assets/Scripts/Units/UIShop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void GenerateCard()

public void OnCardClick(UICard card, UnitDatabaseSO.UnitData cardData)
{
if (PlayerData.Instance.CanAfford(cardData.cost))
if (PlayerData.Instance.CanAfford(cardData.cost) && PlayerData.Instance.benchUnits.Count < 7)
{
PlayerData.Instance.SpendMoney(cardData.cost);
card.gameObject.SetActive(false);
Expand Down
Loading

0 comments on commit b844a66

Please sign in to comment.