-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameBehavior.cs
191 lines (160 loc) · 5.63 KB
/
GameBehavior.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using PointClick;
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using VNENGINE;
public class GameBehavior : MonoBehaviour
{
public static string firstSceneName = "Forest";
private string currentSceneName;
private SceneLayoutScriptable currentScene;
public bool gamePaused { get; private set; } = false;
private bool showWinScreen = false;
private bool showLossScreen = false;
public Inventory<Item> inventory;
public static GameBehavior Instance { get; private set; }
private Dictionary <string, SceneLayoutScriptable> allScenes;
private Dictionary<string, ItemScriptable> allItems;
private GraphicPanel backPanel, controlPanel, itemsPanel;
public const int maxItems = 3;
public void Awake()
{
if (Instance == null)
Instance = this;
currentSceneName = firstSceneName;
/*currentScene = Resources.Load<SceneLayoutScriptable>("Places\\"+currentSceneName);
if (currentScene == null)
Debug.LogError($"Enable to load scriptable place {currentSceneName}");*/
inventory = new Inventory<Item>();
SceneLayoutScriptable[] tmp = Resources.LoadAll<SceneLayoutScriptable>(FilePaths.places);
allScenes = new Dictionary<string, SceneLayoutScriptable>();
foreach (var scene in tmp)
allScenes[scene.label] = scene;
CheckScenes();
LoadItems();
}
void Start()
{
StartCoroutine(Running());
}
IEnumerator Running()
{
backPanel = GraphicPanelManager.instance.GetPanel("Background");
if (backPanel == null)
Debug.LogError("Background panel could not be found");
controlPanel = GraphicPanelManager.instance.GetPanel("Controls");
if (controlPanel == null)
Debug.LogError("Control panel could not be found");
itemsPanel = GraphicPanelManager.instance.GetPanel("Items");
if (itemsPanel == null)
Debug.LogError("Items panel could not be found");
ResumeGame();
ChangeScene(currentSceneName);
yield return null;
}
private void DrawBackImage()
{
GraphicLayer layer = backPanel.GetLayer(0, true);
layer.SetTexture(currentScene.image);
}
/*
void OnGUI()
{
foreach (var s in currentScene.hotSpots)
{
if(GUI.Button(new Rect(s.xmin, s.ymin, s.xsize, s.ysize), ""))
{
ChangeScene(s.destination);
}
}
}*/
private void Update()
{
if (Input.GetKey(KeyCode.Escape))
Application.Quit();
}
public void ChangeScene(string newSceneName)
{
Debug.Log($"loading scene{newSceneName}");
Globals.instance.mouseMode = Globals.MouseMode.basic;
currentSceneName = newSceneName;
/* currentScene = Resources.Load<SceneLayoutScriptable>("Places\\" + currentSceneName);
if (currentScene == null)
throw new ArgumentException($"Coudl not load scriptable scene {currentSceneName}");*/
if (allScenes.ContainsKey(currentSceneName))
currentScene = allScenes[currentSceneName];
else
throw new ArgumentException($"cannot find scene {currentSceneName}");
controlPanel.GetLayer(0, true).Clear();
DrawBackImage();
controlPanel.GetLayer(0, true).EnableButtons(currentScene);
GraphicLayer itemLayer = itemsPanel.GetLayer(0, true);
foreach (var tmpitem in currentScene.items)
{
allItems[tmpitem.name].DrawItem(itemLayer.panel);
}
}
private int OnHotSpot(SceneLayoutScriptable sc)
{
int spot = -1;
return spot;
}
void PauseGame()
{
gamePaused = true;
Time.timeScale = 0;
}
private void ResumeGame()
{
gamePaused = false;
Time.timeScale = 1;
}
private void CheckScenes()
{
foreach(var scene in allScenes.Keys)
{
var check = CheckScene(allScenes[scene]);
if (check.Count > 0)
{
foreach (var problemScene in check)
Debug.LogError($"Cannot find connection from {scene} to {problemScene}");
}
}
}
private List<string> CheckScene(SceneLayoutScriptable sc)
{
List<string> problemHotSpot = new();
foreach (var spot in sc.hotSpots)
{
if (!allScenes.ContainsKey(spot.destination))
problemHotSpot.Add(spot.destination);
}
return problemHotSpot;
}
private void LoadItems()
{
//ItemScriptable[] tmp = Resources.LoadAll<ItemScriptable>(FilePaths.itemsObjects);
allItems = new();
foreach (var sc in allScenes.Keys)
{
foreach (var tmpitem in allScenes[sc].items)
if (!allItems.ContainsKey(tmpitem.name))
{
allItems[tmpitem.name] = Resources.Load<ItemScriptable>(FilePaths.itemsObjects + tmpitem.name);
if (allItems[tmpitem.name] == null)
Debug.LogError($"cannot find the item {tmpitem.name}");
else
{
Debug.Log($"item {tmpitem.name} loaded successfully");
allItems[tmpitem.name].position.x = tmpitem.x;
allItems[tmpitem.name].position.y = tmpitem.y;
}
}
}
}
}