File size: 2,055 Bytes
c80142e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using UnityEngine;
using System.Collections.Generic;

[System.Serializable]
public class ActorData
{
    public GameObject prefab;
    public Vector3 originalPosition;
    public Quaternion originalRotation;
}


public class ActorManager : MonoBehaviour
{
    public static ActorManager Instance;

    private Dictionary<Actor, ActorData> actorOriginals = new();
    private List<Actor> activeActors = new();

    void Awake()
    {
        if (Instance != null && Instance != this)
            Destroy(gameObject);
        else
            Instance = this;
    }

    public void RegisterActor(Actor actor)
    {
        if (actorOriginals.ContainsKey(actor)) return;

        GameObject clone = Instantiate(actor.gameObject);
        clone.SetActive(false);
        clone.transform.SetParent(transform);

        actorOriginals[actor] = new ActorData
        {
            prefab = actor.prefabReference,
            originalPosition = actor.transform.position,
            originalRotation = actor.transform.rotation
        };

        activeActors.Add(actor);
    }

    public void ResetActor(Actor actor)
    {
        if (!actorOriginals.TryGetValue(actor, out var data)) return;

        Destroy(actor.gameObject);

        GameObject newActorGO = Instantiate(data.prefab, data.originalPosition, data.originalRotation);
        newActorGO.SetActive(true);

        Actor newActor = newActorGO.GetComponent<Actor>();
        actorOriginals.Remove(actor);
        actorOriginals[newActor] = data;

        activeActors.Remove(actor);
        activeActors.Add(newActor);
    }

    public void ResetAllForObjective(Objective obj)
    {
        List<Actor> toReset = new();

        foreach (var actor in activeActors)
        {
            if (actor != null && actor.objectiveList.Contains(obj))
            {
                toReset.Add(actor);
            }
        }

        foreach (var actor in toReset)
        {
            ResetActor(actor);
        }
    }
}