aboutsummaryrefslogtreecommitdiff
path: root/NvBlast/examples/UnityExample/Assets/Scripts/Demo.cs
blob: eb60d262f579b77624acf10188e7b5613d8041a7 (plain) (blame)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Demo : MonoBehaviour
{
    public GameObject hitSphere;

	void Awake ()
    {
        generateCity();
    }

    private void generateCity()
    {
        const int BUILDING_TYPE_COUNT = 5;
        Vector3 BUILDING_MIN_SIZE = new Vector3(10, 10, 10);
        Vector3 BUILDING_MAX_SIZE = new Vector3(50, 200, 50);

        List<CubeAsset> buildingTypes = new List<CubeAsset>(BUILDING_TYPE_COUNT);
        for (int i = 0; i < BUILDING_TYPE_COUNT; ++i)
        {
            CubeAsset.Settings settings = new CubeAsset.Settings();
            settings.depths.Add(new CubeAsset.DepthInfo(new Vector3(1, 1, 1), NvBlastChunkDesc.Flags.NoFlags));
            settings.depths.Add(new CubeAsset.DepthInfo(new Vector3(1, 2, 1), NvBlastChunkDesc.Flags.NoFlags));
            settings.depths.Add(new CubeAsset.DepthInfo(new Vector3(2, 3, 2), NvBlastChunkDesc.Flags.NoFlags));
            settings.depths.Add(new CubeAsset.DepthInfo(new Vector3(2, 2, 2), NvBlastChunkDesc.Flags.SupportFlag));
            settings.extents = new Vector3(Random.Range(BUILDING_MIN_SIZE.x, BUILDING_MAX_SIZE.x), Random.Range(BUILDING_MIN_SIZE.y, BUILDING_MAX_SIZE.y), Random.Range(BUILDING_MIN_SIZE.z, BUILDING_MAX_SIZE.z));
            settings.staticHeight = 10.0f;

            CubeAsset cubeAsset = CubeAsset.generate(settings);

            buildingTypes.Add(cubeAsset);
        }

        int totalBuildings = 0;

        const float CITY_HALF_SIZE = 200.0f;
        const float SPARSITY = 30.0f;
        const int BUILDING_COUNT_MAX = 20;
        Vector2 pos = new Vector2(-CITY_HALF_SIZE, -CITY_HALF_SIZE);
        float buildingYMax = 0.0f;
        while (pos.y < CITY_HALF_SIZE && totalBuildings < BUILDING_COUNT_MAX)
        {
            // random jump
            pos.x += Random.Range(0.0f, SPARSITY);
            if(pos.x > CITY_HALF_SIZE)
            {
                pos.x = -CITY_HALF_SIZE;
                pos.y += buildingYMax + Random.Range(0.0f, SPARSITY);
                buildingYMax = 0.0f;
                continue;
            }

            // random bulding type spawn
            int type = Random.Range(0, buildingTypes.Count);
            var cubeFamily = (new GameObject("Cube Actor #" + type)).AddComponent<CubeFamily>();
            CubeAsset asset = buildingTypes[type];
            cubeFamily.transform.localPosition = new Vector3(pos.x, asset.extents.y / 2.0f, pos.y);
            pos.x += asset.extents.x;
            buildingYMax = Mathf.Max(buildingYMax, asset.extents.z);
            cubeFamily.Initialize(asset);
            totalBuildings++;
        }
    }

    private IEnumerator applyRadialDamage(Vector3 position, float minRadius, float maxRadius, float compressive, float explosive = 3000.0f)
    {
        var hits = Physics.OverlapSphere(position, maxRadius);
        foreach (var hit in hits)
        {
            var rb = hit.GetComponentInParent<Rigidbody>();
            var family = hit.GetComponentInParent<CubeFamily>();
            if (rb != null && family != null)
            {
                family.ApplyRadialDamage(rb, position, minRadius, maxRadius, compressive);
            }
        }

        yield return new WaitForEndOfFrame();

        hits = Physics.OverlapSphere(position, maxRadius);
        foreach (var hit in hits)
        {
            var rb = hit.GetComponentInParent<Rigidbody>();
            if(rb != null)
            {
                rb.AddExplosionForce(explosive, position, maxRadius, 3.0f);
            }
        }
    }

    private void Update()
    {
        hitSphere.SetActive(false);
        bool isActive = false;
        if (true)
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if(Physics.Raycast(ray, out hit))
            {
                hitSphere.transform.position = hit.point;
                isActive = true;
            }
        }

        _hitSphereSize += Input.GetAxis("Mouse ScrollWheel") * 10.0f;

        if (Input.GetMouseButton(0))
        {
            StartCoroutine(applyRadialDamage(hitSphere.transform.position, 0.0f, _hitSphereSize, 10.0f));
        }

        hitSphere.SetActive(isActive);
        hitSphere.transform.localScale = new Vector3(_hitSphereSize, _hitSphereSize, _hitSphereSize);
    }

    private float _hitSphereSize = 25.0f;
}