-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDestructableTile.cs
94 lines (84 loc) · 2.58 KB
/
DestructableTile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.UI;
public class DestructableTile : MonoBehaviour
{
private bool collide;
private float startTime;
private Dictionary<TileBase, TileData> dataFromTiles;
public float holdTime;
public float score;
public List<TileData> tileDatas;
public Tilemap ground;
public Vector3Int currentTile;
public Text scoreDisplay;
// Start is called before the first frame update
private void Start()
{
ground = GetComponent<Tilemap>();
startTime = 0f;
score = 0f;
}
private void Awake()
{
dataFromTiles = new Dictionary<TileBase, TileData>();
foreach (var tileData in tileDatas)
{
foreach(var tile in tileData.tiles)
{
dataFromTiles.Add(tile, tileData);
}
}
}
// Update is called once per frame
private void Update()
{
if (Input.GetMouseButton(0))
{
startTime += Time.deltaTime;
if (collide && startTime > holdTime)
{
startTime = 0f;
// This is a hack until raycasts can be implemented, for now, tile may not be findable
try
{
// This should be replacing a the money value on the player, the UI should be doing this.
score += dataFromTiles[ground.GetTile(currentTile)].value;
}
catch (System.ArgumentNullException)
{
score += 1;
}
scoreDisplay.text = score.ToString("0");
ground.SetTile(currentTile, null);
}
}
else if (Input.GetMouseButtonUp(0))
{
startTime = 0f;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
Vector2 hitPosition = Vector2.zero;
Vector3Int cellPosition = ground.WorldToCell(transform.position);
foreach (ContactPoint2D hit in collision.contacts)
{
hitPosition.x = hit.point.x - 0.0001f * hit.normal.x;
hitPosition.y = hit.point.y - 0.0001f * hit.normal.y;
currentTile = ground.WorldToCell(hitPosition);
collide = true;
}
}
}
private void OnCollisionExit2D(Collision2D collision)
{
collide = false;
startTime = 0f;
}
}