-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathAutomap.cs
2794 lines (2419 loc) · 140 KB
/
Automap.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2023 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: Michael Rauter (a.k.a. Nystul)
// Contributors: Lypyl, Interkarma, Numidium
//
// Notes:
//
//#define DEBUG_RAYCASTS
//#define DEBUG_SHOW_RAYCAST_TIMES
using UnityEngine;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using DaggerfallConnect;
using DaggerfallConnect.Arena2;
using DaggerfallConnect.Utility;
using DaggerfallWorkshop;
using DaggerfallWorkshop.Utility;
using DaggerfallWorkshop.Game.UserInterface;
using DaggerfallWorkshop.Game.UserInterfaceWindows;
using DaggerfallWorkshop.Game.Player;
using DaggerfallWorkshop.Game.Entity;
using DaggerfallWorkshop.Game.Serialization;
using DaggerfallWorkshop.Game.Utility;
using Wenzil.Console;
namespace DaggerfallWorkshop.Game
{
using AutomapGeometryModelState = Automap.AutomapGeometryBlockState.AutomapGeometryBlockElementState.AutomapGeometryModelState;
using AutomapGeometryBlockElementState = Automap.AutomapGeometryBlockState.AutomapGeometryBlockElementState;
/// <summary>
/// this class provides the automap core functionality like geometry creation and discovery mechanism
/// </summary>
public class Automap : MonoBehaviour
{
#region Singleton
private static Automap _instance;
public static Automap instance
{
get
{
if (_instance == null)
_instance = GameObject.FindObjectOfType<Automap>();
return _instance;
}
private set { _instance = value; }
}
#endregion
#region classes
/// <summary>
/// class to store state of discovery of a dungeon block or interior, this state can be used to restore state of GameObject gameobjectGeometry
/// this class basically maps the two fields/states (MeshRenderer active state and Material shader keyword "RENDER_IN_GRAYSCALE") that are
/// used for discovery state of the models inside GameObject gameobjectGeometry when an interior is loaded into this GameObject
/// it is also used as type inside the blocks list in AutomapDungeonState
/// </summary>
public class AutomapGeometryBlockState
{
public class AutomapGeometryBlockElementState
{
public class AutomapGeometryModelState
{
public bool discovered; /// discovered before (render model)
public bool visitedInThisRun; /// visited in this dungeon run (if true render in color, otherwise grayscale)
}
public List<AutomapGeometryModelState> models;
}
public List<AutomapGeometryBlockElementState> blockElements;
public String blockName;
}
/// <summary>
/// class to store state of discovery of a dungeon, this state can be used to restore state of GameObject gameobjectGeometry
/// this class basically maps the two fields/states (MeshRenderer active state and Material shader keyword "RENDER_IN_GRAYSCALE") that are
/// used for discovery state of the models inside GameObject gameobjectGeometry when a dungeon is loaded into this GameObject
/// it also stores list of user note markers
/// </summary>
public class AutomapDungeonState
{
public String locationName; /// name of the dungeon location
public ulong timeInSecondsLastVisited; /// time in seconds (from DaggerfallDateTime) when player last visited the dungeon (used to store only the n last dungeons in save games)
public bool entranceDiscovered; /// indicates if the dungeon entrance has already been discovered
public List<AutomapGeometryBlockState> blocks; // automap geometry dungeon state
public SortedList<int, NoteMarker> listUserNoteMarkers; // list of user note markers
public Dictionary<string, TeleporterConnection> dictTeleporterConnections; // dictionary of discovered teleporter connections
}
public class NoteMarker
{
public string note;
public Vector3 position;
public NoteMarker(Vector3 position, string note)
{
this.note = note;
this.position = position;
}
}
/// <summary>
/// we need to use this instead of Transform since Transform can not be serialized
/// </summary>
public class TeleporterTransform
{
public Vector3 position;
public Quaternion rotation;
public TeleporterTransform(Transform transform)
{
position = transform.position;
rotation = transform.rotation;
}
public TeleporterTransform(Transform transform, Vector3 positionOffset)
{
position = transform.position + positionOffset;
rotation = transform.rotation;
}
}
public class TeleporterConnection
{
public TeleporterTransform teleporterEntrance;
public TeleporterTransform teleporterExit;
public override string ToString()
{
return "position: " + teleporterEntrance.position.ToString() + ", rotation: " + teleporterExit.rotation.ToString();
}
}
#endregion
#region Fields
const string ResourceNameRotateArrow = "RotateArrow";
const string NameGamobjectBeacons = "Beacons";
const string NameGameobjectPlayerMarkerArrow = "PlayerMarkerArrow";
const string NameGameobjectBeaconPlayerPosition = "BeaconPlayerPosition";
const string NameGameobjectBeaconRotationPivotAxis = "BeaconRotationPivotAxis";
const string NameGameobjectRotateArrow = "CurvedArrow";
const string NameGameobjectBeaconEntrance = "BeaconEntrancePosition"; // gameobject will hold both entrance position marker beacon and cube entrance position marker
const string NameGameobjectBeaconEntrancePositionMarker = "BeaconEntrancePositionMarker";
const string NameGameobjectCubeEntrancePositionMarker = "CubeEntrancePositionMarker";
const string NameGameobjectTeleporterPortalMarker = "PortalMarker";
const string NameGameobjectTeleporterSubStringStart = "Teleporter [";
const string NameGameobjectTeleporterEntranceSubStringEnd = "] - Portal Entrance";
const string NameGameobjectTeleporterExitSubStringEnd = "] - Portal Exit";
const string NameGameobjectTeleporterConnection = "Teleporter Connection";
const string NameGameobjectUserMarkerNotes = "UserMarkerNotes";
const string NameGameobjectUserNoteMarkerSubStringStart = "UserNoteMarker_";
const string NameGameobjectDiamond = "Diamond";
const float raycastDistanceDown = 3.0f; // 3 meters should be enough (note: flying too high will result in geometry not being revealed by this raycast
const float raycastDistanceViewDirection = 30.0f; // don't want to make it too easy to discover big halls - although it shouldn't be to small as well
const float raycastDistanceEntranceMarkerReveal = 100.0f;
const float scanRateGeometryDiscoveryInHertz = 5.0f; // n times per second the discovery of new geometry/meshes is checked
GameObject gameobjectAutomap = null; // used to hold reference to instance of GameObject "Automap" (which has script Game/Automap.cs attached)
GameObject gameobjectGeometry = null; // used to hold reference to instance of GameObject with level geometry used for automap
int layerAutomap; // layer used for level geometry of automap
int layerPlayer; // player layer
GameObject gameObjectCameraAutomap = null; // used to hold reference to GameObject to which camera class for automap camera is attached to
Camera cameraAutomap = null; // camera for automap camera
GameObject gameobjectAutomapKeyLight = null; // instead this script will use its own key light to lighten the level geometry used for automap
GameObject gameobjectAutomapFillLight = null; // and fill light
GameObject gameobjectAutomapBackLight = null; // and back light
GameObject gameObjectPlayerAdvanced = null; // used to hold reference to instance of GameObject "PlayerAdvanced"
float slicingBiasY; // y-bias from player y-position of geometry slice plane (set via Property SlicingBiasY used by DaggerfallAutomapWindow script to set value)
Vector3 rotationPivotAxisPosition; // position of the rotation pivot axis (set via Property RotationPivotAxisPosition used by DaggerfallAutomapWindow script to set value)
bool isOpenAutomap = false; // flag that indicates if automap window is open (set via Property IsOpenAutomap triggered by DaggerfallAutomapWindow script)
// automap render mode is a setting that influences geometry rendered above slicing plane
public enum AutomapRenderMode
{
Cutout = 0,
Wireframe = 1,
Transparent = 2
};
AutomapRenderMode currentAutomapRenderMode = AutomapRenderMode.Cutout; // currently selected automap render mode (default value: cutout)
// flag that indicates if external script should reset automap settings (set via Property ResetAutomapSettingsSignalForExternalScript checked and erased by DaggerfallAutomapWindow script)
// this might look weirds - why not just notify the DaggerfallAutomapWindow class you may ask... - I wanted to make Automap inaware and independent of the actual GUI implementation
// so communication will always be only from DaggerfallAutomapWindow to Automap class - so into other direction it works in that way that Automap will pull
// from DaggerfallAutomapWindow via flags - this is why this flag and its Property ResetAutomapSettingsSignalForExternalScript exist
bool resetAutomapSettingsFromExternalScript = false;
GameObject gameobjectBeacons = null; // collector GameObject to hold beacons
GameObject gameobjectPlayerMarkerArrow = null; // GameObject which will hold player marker arrow
GameObject gameobjectBeaconPlayerPosition = null; // GameObject which will hold player marker ray (red ray)
GameObject gameobjectBeaconEntrancePosition = null; // GameObject which will hold (dungeon) entrance marker ray (green ray)
GameObject gameobjectBeaconRotationPivotAxis = null; // GameObject which will hold rotation pivot axis ray (blue ray)
GameObject gameobjectRotationArrow1 = null; // GameObject which will hold rotation arrow1 (blue arrow)
GameObject gameobjectRotationArrow2 = null; // GameObject which will hold rotation arrow2 (blue arrow)
GameObject gameObjectEntrancePositionCubeMarker = null; // used for entrance marker discovery
Collider playerCollider = null;
// specifies which object should have focus ()
public enum AutomapFocusObject
{
Player = 0,
Entrance = 1,
RotationAxis = 2
};
AutomapFocusObject focusObject;
//readonly Vector3 rayPlayerPosOffset = new Vector3(-0.1f, 0.0f, +0.1f); // small offset to prevent ray for player position to be exactly in the same position as the rotation pivot axis
//readonly Vector3 rayEntrancePosOffset = new Vector3(0.1f, 0.0f, +0.1f); // small offset to prevent ray for dungeon entrance to be exactly in the same position as the rotation pivot axis
readonly Vector3 rayPlayerPosOffset = new Vector3(0.0f, 0.0f, 0.0f); // small offset to prevent ray for player position to be exactly in the same position as the rotation pivot axis
readonly Vector3 rayEntrancePosOffset = new Vector3(0.0f, 0.0f, 0.0f); // small offset to prevent ray for dungeon entrance to be exactly in the same position as the rotation pivot axis
bool debugTeleportMode = false;
Texture2D textureMicroMap = null;
SortedList<int, NoteMarker> listUserNoteMarkers = new SortedList<int, NoteMarker>(); // the list containing the user note markers, key is the id used when creating the user note marker
int idOfUserMarkerNoteToBeChanged; // used to identify id of last double-clicked user note marker when changing note text
DaggerfallInputMessageBox messageboxUserNote;
GameObject gameObjectUserNoteMarkers = null; // container object for custom user notes markers
GameObject customGameObjectUserNoteMarker = null;
/// <summary>
/// this dictionary is used to store teleporter connections after being discovered by pc
/// </summary>
Dictionary<string, TeleporterConnection> dictTeleporterConnections = new Dictionary<string, TeleporterConnection>();
GameObject gameobjectTeleporterMarkers = null; // container object for teleporter markers
GameObject gameobjectTeleporterConnection = null; // on mouse hover over connection between portal entrance and exit is shown
int numberOfDungeonMemorized = 1; /// 0... vanilla daggerfall behavior, 1... remember last visited dungeon, n... remember n visited dungeons
// dungeon state is of type AutomapDungeonState which has its models in block field in a 4 level hierarchy
AutomapDungeonState automapDungeonState = null;
// interior state is of type AutomapGeometryBlockState which has its models in a 3 level hierarchy
AutomapGeometryBlockState automapGeometryInteriorState = null;
/// <summary>
/// this dictionary is used to store the discovery state of dungeons in the game world
/// the AutomapDungeonState is stored for each dungeon in this dictionary identified by its identifier string
/// </summary>
Dictionary<string, AutomapDungeonState> dictAutomapDungeonsDiscoveryState = new Dictionary<string, AutomapDungeonState>();
bool iTweenCameraAnimationIsRunning = false; // indicates if iTween camera animation is running (i.e. teleporter portal jump animation plays)
static bool isCreatingDungeonAutomapBaseGameObjects = false; // Indicates that the Automap is creating the models for a Castle's or Dungeon's Automap
#endregion
#region Properties
/// <summary>
/// DaggerfallAutomapWindow script will use this to get automap layer
/// </summary>
public int LayerAutomap
{
get { return (layerAutomap); }
}
/// <summary>
/// DaggerfallAutomapWindow script will use this to get automap camera
/// </summary>
public Camera CameraAutomap
{
get { return (cameraAutomap); }
}
/// <summary>
/// DaggerfallAutomapWindow script will use this to check if it should reset automap settings (and if it does it will erase flag)
/// </summary>
public bool ResetAutomapSettingsSignalForExternalScript
{
get { return (resetAutomapSettingsFromExternalScript); }
set { resetAutomapSettingsFromExternalScript = value; }
}
/// <summary>
/// DaggerfallAutomapWindow script will use this to propagate its slicingBiasY (y-offset from the player y position)
/// </summary>
public float SlicingBiasY
{
get { return (slicingBiasY); }
set { slicingBiasY = value; }
}
/// <summary>
/// DaggerfallAutomapWindow script will use this to propagate its rotation pivot axis position (dependent on selected view)
/// </summary>
public Vector3 RotationPivotAxisPosition
{
get { return (rotationPivotAxisPosition); }
set { rotationPivotAxisPosition = value; }
}
/// <summary>
/// DaggerfallAutomapWindow script will use this to propagate its rotation pivot axis rotation
/// (rotating the pixot axis will rotate the indicator arrows as they are child objects of the pivot axis)
/// </summary>
public Quaternion RotationPivotAxisRotation
{
get { return (gameobjectBeaconRotationPivotAxis.transform.rotation); }
set { gameobjectBeaconRotationPivotAxis.transform.rotation = value; }
}
/// <summary>
/// DaggerfallAutomapWindow script will use this to propagate if the automap window is open or not
/// </summary>
public bool IsOpenAutomap
{
set { isOpenAutomap = value; }
}
/// <summary>
/// used to set or get the debug teleport mode
/// </summary>
public bool DebugTeleportMode
{
get { return (debugTeleportMode); }
set { debugTeleportMode = value; }
}
/// <summary>
/// returns the Texture2D containing the texture with the micro map (small texture with 2x2 pixels representing a dungeon block - note: rendering will render this texture at double size)
/// </summary>
public Texture2D TextureMicroMap
{
get { return textureMicroMap; }
}
/// <summary>
/// returns true if a iTween camera animation is running
/// (i.e. teleporter portal has been clicked and animation runs to jump to /
/// focus portal at other end of teleporter connection)
/// </summary>
public bool ITweenCameraAnimationIsRunning
{
get { return iTweenCameraAnimationIsRunning; }
}
public static bool IsCreatingDungeonAutomapBaseGameObjects
{
get { return isCreatingDungeonAutomapBaseGameObjects; }
}
/// <summary>
/// Used to overwrite the gameObjectUserNoteMarker to a custom prefab.
/// Only overwrites if not null.
/// Default: null.
/// Note: The Prefab must contain at least 1 collider for it to be edited/removed.
/// </summary>
public GameObject CustomGameObjectUserNoteMarker
{
set { customGameObjectUserNoteMarker = value; }
get { return customGameObjectUserNoteMarker; }
}
#endregion
#region Public Methods
/// <summary>
/// GetState() method for save system integration
/// </summary>
public Dictionary<string, AutomapDungeonState> GetState()
{
SaveStateAutomapDungeon(false);
return dictAutomapDungeonsDiscoveryState;
}
/// <summary>
/// SetState() method for save system integration
/// </summary>
public void SetState(Dictionary<string, AutomapDungeonState> savedDictAutomapDungeonsDiscoveryState)
{
dictAutomapDungeonsDiscoveryState = savedDictAutomapDungeonsDiscoveryState;
}
/// <summary>
/// sets the number of dungeons that are memorized
/// </summary>
public void SetNumberOfDungeonMemorized(int n)
{
numberOfDungeonMemorized = n;
}
/// <summary>
/// DaggerfallAutomapWindow script will use this to signal this script to update when automap window was pushed - TODO: check if this can done with an event (if events work with gui windows)
/// </summary>
public void UpdateAutomapStateOnWindowPush()
{
// create teleport markers (that are not already present on map)
// since new teleporters could have been discovered by pc since last time map was open this must be checked here
CreateTeleporterMarkers();
SetActivationStateOfMapObjects(true);
gameobjectPlayerMarkerArrow.transform.position = gameObjectPlayerAdvanced.transform.position;
gameobjectPlayerMarkerArrow.transform.rotation = gameObjectPlayerAdvanced.transform.rotation;
gameobjectBeaconPlayerPosition.transform.position = gameObjectPlayerAdvanced.transform.position + rayPlayerPosOffset;
// create camera (if not present) that will render automap level geometry
CreateAutomapCamera();
// create lights that will light automap level geometry
CreateLightsForAutomapGeometry();
UpdateMicroMapTexture();
UpdateSlicingPositionY();
}
/// <summary>
/// DaggerfallAutomapWindow script will use this to signal this script to update when automap window was popped - TODO: check if this can done with an event (if events work with gui windows)
/// </summary>
public void UpdateAutomapStateOnWindowPop()
{
// about SetActivationStateOfMapObjects(false):
// this will not be enough if we will eventually allow gui windows to be opened while exploring the world
// then it will be necessary to either only disable the colliders on the automap level geometry or
// make player collision ignore colliders of objects in automap layer - I would clearly prefer this option
SetActivationStateOfMapObjects(false);
if ((GameManager.Instance.PlayerEnterExit.IsPlayerInside) && ((GameManager.Instance.PlayerEnterExit.IsPlayerInsideBuilding) || (GameManager.Instance.PlayerEnterExit.IsPlayerInsideDungeon) || (GameManager.Instance.PlayerEnterExit.IsPlayerInsideDungeonCastle)))
{
// and get rid of lights used to light the automap level geometry
UnityEngine.Object.Destroy(gameobjectAutomapKeyLight);
UnityEngine.Object.Destroy(gameobjectAutomapFillLight);
UnityEngine.Object.Destroy(gameobjectAutomapBackLight);
}
// destroy the camera so it does not use system resources
if (gameObjectCameraAutomap != null)
{
UnityEngine.Object.Destroy(gameObjectCameraAutomap);
}
}
/// <summary>
/// DaggerfallAutomapWindow script will use this to signal this script to update when anything changed that requires Automap to update - TODO: check if this can done with an event (if events work with gui windows)
/// </summary>
public void ForceUpdate()
{
Update();
}
/// <summary>
/// DaggerfallAutomapWindow script will use this to signal this script to switch to the next available automap rendering mode
/// </summary>
public void SwitchToNextAutomapRenderMode()
{
int numberOfAutomapRenderModes = Enum.GetNames(typeof(AutomapRenderMode)).Length;
currentAutomapRenderMode++;
if ((int)currentAutomapRenderMode > numberOfAutomapRenderModes - 1) // first mode is mode 0 -> so use numberOfAutomapRenderModes-1 for comparison
currentAutomapRenderMode = 0;
switch (currentAutomapRenderMode)
{
default:
case AutomapRenderMode.Transparent:
Shader.DisableKeyword("AUTOMAP_RENDER_MODE_WIREFRAME");
Shader.EnableKeyword("AUTOMAP_RENDER_MODE_TRANSPARENT");
break;
case AutomapRenderMode.Wireframe:
Shader.EnableKeyword("AUTOMAP_RENDER_MODE_WIREFRAME");
Shader.DisableKeyword("AUTOMAP_RENDER_MODE_TRANSPARENT");
break;
case AutomapRenderMode.Cutout:
Shader.DisableKeyword("AUTOMAP_RENDER_MODE_WIREFRAME");
Shader.DisableKeyword("AUTOMAP_RENDER_MODE_TRANSPARENT");
break;
}
}
/// <summary>
/// DaggerfallAutomapWindow script will use this to signal this script to switch to automap rendering mode "transparent"
/// </summary>
public void SwitchToAutomapRenderModeTransparent()
{
currentAutomapRenderMode = AutomapRenderMode.Transparent;
Shader.DisableKeyword("AUTOMAP_RENDER_MODE_WIREFRAME");
Shader.EnableKeyword("AUTOMAP_RENDER_MODE_TRANSPARENT");
}
/// <summary>
/// DaggerfallAutomapWindow script will use this to signal this script to switch to automap rendering mode "wireframe"
/// </summary>
public void SwitchToAutomapRenderModeWireframe()
{
currentAutomapRenderMode = AutomapRenderMode.Wireframe;
Shader.EnableKeyword("AUTOMAP_RENDER_MODE_WIREFRAME");
Shader.DisableKeyword("AUTOMAP_RENDER_MODE_TRANSPARENT");
}
/// <summary>
/// DaggerfallAutomapWindow script will use this to signal this script to switch to automap rendering mode "cutout"
/// </summary>
public void SwitchToAutomapRenderModeCutout()
{
currentAutomapRenderMode = AutomapRenderMode.Cutout;
Shader.DisableKeyword("AUTOMAP_RENDER_MODE_WIREFRAME");
Shader.DisableKeyword("AUTOMAP_RENDER_MODE_TRANSPARENT");
}
/// <summary>
/// DaggerfallAutomapWindow script will use this to signal this script to switch focus to next object of interest and return the GameObject which has focus
/// </summary>
/// <returns> the GameObject which has the focus </returns>
public GameObject SwitchFocusToNextObject()
{
int numberOfAutomapFocusObjects = Enum.GetNames(typeof(AutomapFocusObject)).Length;
focusObject++;
// if entrance is not discovered and focusObject is entrance
if ((gameobjectBeaconEntrancePosition) && (!gameobjectBeaconEntrancePosition.activeSelf) && focusObject == AutomapFocusObject.Entrance)
{
focusObject++; // skip entrance and focus next object
}
if ((int)focusObject > numberOfAutomapFocusObjects - 1) // first mode is mode 0 -> so use numberOfAutomapFocusObjects-1 for comparison
focusObject = 0;
GameObject gameobjectInFocus;
switch (focusObject)
{
default:
case AutomapFocusObject.Player:
gameobjectInFocus = gameobjectBeaconPlayerPosition;
break;
case AutomapFocusObject.Entrance:
gameobjectInFocus = gameobjectBeaconEntrancePosition;
break;
case AutomapFocusObject.RotationAxis:
gameobjectInFocus = gameobjectBeaconRotationPivotAxis;
break;
}
return (gameobjectInFocus);
}
/// <summary>
/// gets the mouse hover over text that will be displayed in the status bar/info bar at the bottom of the automap window
/// </summary>
/// <param name="screenPosition">the mouse position to be used for raycast</param>
/// <returns>the string containing the hover over text</returns>
public string GetMouseHoverOverText(Vector2 screenPosition)
{
RaycastHit? nearestHit = null;
GetRayCastNearestHitOnAutomapLayer(screenPosition, out nearestHit);
if (nearestHit.HasValue)
{
// if hit geometry is user note marker
if (nearestHit.Value.transform.name.StartsWith(NameGameobjectUserNoteMarkerSubStringStart))
{
int id = System.Convert.ToInt32(nearestHit.Value.transform.name.Replace(NameGameobjectUserNoteMarkerSubStringStart, ""));
if (listUserNoteMarkers.ContainsKey(id))
return listUserNoteMarkers[id].note; // get user note by id
}
// if hit geometry is player position beacon
else if (nearestHit.Value.transform.name == NameGameobjectBeaconPlayerPosition)
{
return TextManager.Instance.GetLocalizedText("automapPlayerPositionBeacon");
}
// if hit geometry is player rotation pivot axis or rotation indicator arrows
else if (nearestHit.Value.transform.name == NameGameobjectBeaconRotationPivotAxis || nearestHit.Value.transform.name == NameGameobjectRotateArrow)
{
return TextManager.Instance.GetLocalizedText("automapRotationPivotAxis");
}
// if hit geometry is dungeon entrance/exit position beacon
else if (nearestHit.Value.transform.name == NameGameobjectBeaconEntrancePositionMarker)
{
return TextManager.Instance.GetLocalizedText("automapEntranceExitPositionBeacon");
}
// if hit geometry is dungeon entrance/exit position marker
else if (nearestHit.Value.transform.name == NameGameobjectCubeEntrancePositionMarker)
{
return TextManager.Instance.GetLocalizedText("automapEntranceExit");
}
// if hit geometry is player position marker arrow
else if (nearestHit.Value.transform.name == NameGameobjectPlayerMarkerArrow)
{
return TextManager.Instance.GetLocalizedText("automapPlayerMarker");
}
// if hit geometry is teleporter portal marker and its parent gameobject is an teleporter entrance
else if (
nearestHit.Value.transform.name == NameGameobjectTeleporterPortalMarker &&
nearestHit.Value.transform.parent.transform.name.StartsWith(NameGameobjectTeleporterSubStringStart) &&
nearestHit.Value.transform.parent.transform.name.EndsWith(NameGameobjectTeleporterEntranceSubStringEnd)
)
{
return TextManager.Instance.GetLocalizedText("automapTeleporterEntrance");
}
// if hit geometry is teleporter portal marker and its parent gameobject is an teleporter exit
else if (
nearestHit.Value.transform.name == NameGameobjectTeleporterPortalMarker &&
nearestHit.Value.transform.parent.transform.name.StartsWith(NameGameobjectTeleporterSubStringStart) &&
nearestHit.Value.transform.parent.transform.name.EndsWith(NameGameobjectTeleporterExitSubStringEnd)
)
{
return TextManager.Instance.GetLocalizedText("automapTeleporterExit");
}
}
return ""; // otherwise return empty string (= no mouse hover over text will be displayed)
}
/// <summary>
/// will make the mouse hover over gameobjects appear in the automap window
/// </summary>
/// <param name="screenPosition">the mouse position to be used for raycast</param>
/// <returns>a flag that will indicate that the automap render panel will need to be updated</returns>
public bool UpdateMouseHoverOverGameObjects(Vector2 screenPosition)
{
RaycastHit? nearestHit = null;
GetRayCastNearestHitOnAutomapLayer(screenPosition, out nearestHit);
if (nearestHit.HasValue)
{
// if hit geometry is teleporter portal marker and its parent gameobject is an teleporter entrance
if (nearestHit.Value.transform.name == NameGameobjectTeleporterPortalMarker && gameobjectTeleporterConnection == null)
{
gameobjectTeleporterConnection = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
UnityEngine.Object.Destroy(gameobjectTeleporterConnection.GetComponent<Collider>());
gameobjectTeleporterConnection.name = NameGameobjectTeleporterConnection;
gameobjectTeleporterConnection.transform.SetParent(gameobjectAutomap.transform);
gameobjectTeleporterConnection.layer = layerAutomap;
Material material = new Material(Shader.Find("Standard"));
material.color = new Color(0.43f, 0.34f, 0.85f, 1.0f);
material.SetFloat("_Metallic", 0.0f);
material.SetFloat("_Glossiness", 0.0f);
gameobjectTeleporterConnection.GetComponent<MeshRenderer>().material = material;
TeleporterConnection connection = null;
// hit portal marker is an entrance portal
if (nearestHit.Value.transform.parent.transform.name.EndsWith(NameGameobjectTeleporterEntranceSubStringEnd))
{
string key = nearestHit.Value.transform.parent.transform.name.Replace(NameGameobjectTeleporterSubStringStart, "");
key = key.Replace(NameGameobjectTeleporterEntranceSubStringEnd, "");
if (dictTeleporterConnections.ContainsKey(key))
{
connection = dictTeleporterConnections[key];
}
}
// hit portal marker is an exit portal
else if (nearestHit.Value.transform.parent.transform.name.EndsWith(NameGameobjectTeleporterExitSubStringEnd))
{
string key = nearestHit.Value.transform.parent.transform.name.Replace(NameGameobjectTeleporterSubStringStart, "");
key = key.Replace(NameGameobjectTeleporterExitSubStringEnd, "");
if (dictTeleporterConnections.ContainsKey(key))
{
connection = dictTeleporterConnections[key];
}
}
gameobjectTeleporterConnection.transform.position = (connection.teleporterEntrance.position + connection.teleporterExit.position) * 0.5f;
gameobjectTeleporterConnection.transform.localScale = new Vector3(0.2f, (connection.teleporterEntrance.position - connection.teleporterExit.position).magnitude * 0.5f, 0.2f);
gameobjectTeleporterConnection.transform.rotation = Quaternion.FromToRotation(Vector3.up, (connection.teleporterEntrance.position - connection.teleporterExit.position));
return true; // signalize to update automap render panel
}
if (nearestHit.Value.transform.name != NameGameobjectTeleporterPortalMarker)// if no teleporter portal was hit
{
// destroy teleporter connection gameobject
if (gameobjectTeleporterConnection != null)
{
UnityEngine.GameObject.Destroy(gameobjectTeleporterConnection);
}
return true; // signalize to update automap render panel
}
}
else
{
if (gameobjectTeleporterConnection != null)
{
UnityEngine.GameObject.Destroy(gameobjectTeleporterConnection);
}
return true; // signalize to update automap render panel
}
return false; // no update required in the automap render panel
}
public bool TryForTeleporterPortalsAtScreenPosition(Vector2 screenPosition)
{
RaycastHit? nearestHit = null;
GetRayCastNearestHitOnAutomapLayer(screenPosition, out nearestHit);
if (nearestHit.HasValue)
{
// if hit gamobject is a teleporter portal marker
if (nearestHit.Value.transform.name == NameGameobjectTeleporterPortalMarker)
{
// hit portal marker is an entrance portal
if (nearestHit.Value.transform.parent.transform.name.EndsWith(NameGameobjectTeleporterEntranceSubStringEnd))
{
string key = nearestHit.Value.transform.parent.transform.name.Replace(NameGameobjectTeleporterSubStringStart, "");
key = key.Replace(NameGameobjectTeleporterEntranceSubStringEnd, "");
if (dictTeleporterConnections.ContainsKey(key))
{
TeleporterConnection connection = dictTeleporterConnections[key];
iTweenCameraAnimationIsRunning = true;
Hashtable moveParams = __ExternalAssets.iTween.Hash(
"position", cameraAutomap.transform.position - (connection.teleporterEntrance.position - connection.teleporterExit.position),
//"position", connection.teleporterExit.position - cameraAutomap.transform.forward * computedCameraBackwardDistance,
"time", 1.0f,
"ignoretimescale", true, // important since timescale == 0 in menus
"easetype", __ExternalAssets.iTween.EaseType.easeInOutSine,
"oncomplete", "ITweenAnimationComplete",
"oncompletetarget", this.gameObject // important to specify target so that oncomplete function is found (since it is not part of camera gameobject)
);
__ExternalAssets.iTween.MoveTo(cameraAutomap.gameObject, moveParams); // MoveTo call on camera gameObject
}
}
// hit portal marker is an exit portal
else if (nearestHit.Value.transform.parent.transform.name.EndsWith(NameGameobjectTeleporterExitSubStringEnd))
{
string key = nearestHit.Value.transform.parent.transform.name.Replace(NameGameobjectTeleporterSubStringStart, "");
key = key.Replace(NameGameobjectTeleporterExitSubStringEnd, "");
if (dictTeleporterConnections.ContainsKey(key))
{
TeleporterConnection connection = dictTeleporterConnections[key];
iTweenCameraAnimationIsRunning = true;
Hashtable moveParams = __ExternalAssets.iTween.Hash(
"position", cameraAutomap.transform.position - (connection.teleporterExit.position - connection.teleporterEntrance.position),
//"position", connection.teleporterEntrance.position - cameraAutomap.transform.forward * computedCameraBackwardDistance,
"time", 1.0f,
"ignoretimescale", true, // important since timescale == 0 in menus
"easetype", __ExternalAssets.iTween.EaseType.easeInOutSine,
"oncomplete", "ITweenAnimationComplete",
"oncompletetarget", this.gameObject // important to specify target so that oncomplete function is found (since it is not part of camera gameobject)
);
__ExternalAssets.iTween.MoveTo(cameraAutomap.gameObject, moveParams); // MoveTo call on camera gameObject
}
}
return true;
}
}
return false;
}
private void ITweenAnimationComplete()
{
iTweenCameraAnimationIsRunning = false;
}
/// <summary>
/// method which tries to add or edit an existing user marker on a given click position
/// raycast test: if automap geometry is hit -> add new marker, if marker is hit -> edit marker, otherwise: do nothing
/// </summary>
/// <param name="screenPosition">the mouse position to be used for raycast</param>
public void TryToAddOrEditUserNoteMarkerOnDungeonSegmentAtScreenPosition(Vector2 screenPosition, bool editUserNoteOnCreation)
{
RaycastHit? nearestHit = null;
GetRayCastNearestHitOnAutomapLayer(screenPosition, out nearestHit);
if (nearestHit.HasValue)
{
// if hit gamobject is not a user note marker (so for now it is possible to create markers around beacons - this is intended)
if (!nearestHit.Value.transform.name.StartsWith(NameGameobjectUserNoteMarkerSubStringStart))
{
// add a new user note marker
Vector3 spawningPosition = (nearestHit.Value.point) + nearestHit.Value.normal * 0.7f;
// test if there is already a user note marker near to the requested spawning position
var enumerator = listUserNoteMarkers.GetEnumerator();
while (enumerator.MoveNext())
{
if (Vector3.Distance(enumerator.Current.Value.position, spawningPosition) < 1.0f)
return; // if yes, do not add a new marker
}
int id = listUserNoteMarkers.AddNext(new NoteMarker(spawningPosition, ""));
/*GameObject gameObjectNewUserNoteMarker =*/ CreateUserMarker(id, spawningPosition);
if (editUserNoteOnCreation)
{
EditUserNote(id);
}
}
else
{
// edit user note marker
int id = System.Convert.ToInt32(nearestHit.Value.transform.name.Replace(NameGameobjectUserNoteMarkerSubStringStart, ""));
EditUserNote(id);
}
}
}
/// <summary>
/// method which tries to delete an existing user marker on a given click position
/// </summary>
/// <param name="screenPosition">the mouse position to be used for raycast</param>
/// <returns>true if a marker was hit and thus deleted</returns>
public bool TryToRemoveUserNoteMarkerOnDungeonSegmentAtScreenPosition(Vector2 screenPosition)
{
RaycastHit? nearestHit = null;
GetRayCastNearestHitOnAutomapLayer(screenPosition, out nearestHit);
if (nearestHit.HasValue)
{
if (nearestHit.Value.transform.name.StartsWith(NameGameobjectUserNoteMarkerSubStringStart)) // if user note marker was hit
{
int id = System.Convert.ToInt32(nearestHit.Value.transform.name.Replace(NameGameobjectUserNoteMarkerSubStringStart, ""));
if (listUserNoteMarkers.ContainsKey(id))
listUserNoteMarkers.Remove(id); // remove it from list
if (customGameObjectUserNoteMarker == null){
GameObject.Destroy(nearestHit.Value.transform.gameObject); // and destroy gameobject
} else {
GameObject.Destroy(GetHighestAncestorBelowAncestor(nearestHit.Value.transform.gameObject, NameGameobjectUserMarkerNotes));
}
return true;
}
}
return false;
}
/// <summary>
/// method which tries if a raycast will hit automap geometry and if so will center camera on the click position
/// </summary>
/// <param name="screenPosition">the mouse position to be used for raycast</param>
public void TryCenterAutomapCameraOnDungeonSegmentAtScreenPosition(Vector2 screenPosition)
{
RaycastHit? nearestHit = null;
GetRayCastNearestHitOnAutomapLayer(screenPosition, out nearestHit);
if (nearestHit.HasValue)
{
float distance = (cameraAutomap.transform.position - gameObjectPlayerAdvanced.transform.position).magnitude;
cameraAutomap.transform.position = (nearestHit.Value.point);
cameraAutomap.transform.position -= cameraAutomap.transform.forward * distance;
}
}
/// <summary>
/// method which tries if a raycast will hit automap geometry and if so will center rotation pivot axis on the click position
/// </summary>
/// <param name="screenPosition">the mouse position to be used for raycast</param>
public void TrySetRotationPivotAxisToDungeonSegmentAtScreenPosition(Vector2 screenPosition)
{
RaycastHit? nearestHit = null;
GetRayCastNearestHitOnAutomapLayer(screenPosition, out nearestHit);
if (nearestHit.HasValue)
{
float yOffset = +1.0f;
rotationPivotAxisPosition = new Vector3(nearestHit.Value.point.x, nearestHit.Value.point.y + yOffset, nearestHit.Value.point.z);
}
}
/// <summary>
/// DaggerfallAutomapWindow script will use this to signal this script to try to teleport player to dungeon segment shown at a provided screen position
/// </summary>
/// <param name="screenPosition"> the screen position of interest - if a dungeon segment is shown at this position player will be teleported there </param>
public void TryTeleportPlayerToDungeonSegmentAtScreenPosition(Vector2 screenPosition)
{
RaycastHit? nearestHit = null;
GetRayCastNearestHitOnAutomapLayer(screenPosition, out nearestHit);
if (nearestHit.HasValue)
{
gameObjectPlayerAdvanced.transform.position = nearestHit.Value.point + Vector3.up * 0.1f;
gameobjectBeaconPlayerPosition.transform.position = nearestHit.Value.point + rayPlayerPosOffset;
gameobjectPlayerMarkerArrow.transform.position = nearestHit.Value.point + rayPlayerPosOffset;
}
// don't forget to update micro map texture, so new player position is visualized correctly on micro map
UpdateMicroMapTexture();
}
#endregion
#region Unity
void Awake()
{
gameObjectPlayerAdvanced = GameObject.Find("PlayerAdvanced");
if (!gameObjectPlayerAdvanced)
{
DaggerfallUnity.LogMessage("GameObject \"PlayerAdvanced\" not found! in script Automap (in function Awake())", true);
if (Application.isEditor)
Debug.Break();
else
Application.Quit();
}
layerAutomap = LayerMask.NameToLayer("Automap");
if (layerAutomap == -1)
{
DaggerfallUnity.LogMessage("Did not find Layer with name \"Automap\"! Defaulting to Layer 10\nIt is prefered that Layer \"Automap\" is set in Unity Editor under \"Edit/Project Settings/Tags and Layers!\"", true);
layerAutomap = 10;
}
layerPlayer = LayerMask.NameToLayer("Player");
if (layerPlayer == -1)
{
DaggerfallUnity.LogMessage("Did not find Layer with name \"Player\"! entrance/exit marker discovery will not work", true);
}
Camera.main.cullingMask = Camera.main.cullingMask & ~((1 << layerAutomap)); // don't render automap layer with main camera
}
void OnDestroy()
{
}
void OnEnable()
{
PlayerEnterExit.OnTransitionInterior += OnTransitionToInterior;
PlayerEnterExit.OnTransitionDungeonInterior += OnTransitionToDungeonInterior;
PlayerEnterExit.OnTransitionExterior += OnTransitionToExterior;
PlayerEnterExit.OnTransitionDungeonExterior += OnTransitionToDungeonExterior;
StartGameBehaviour.OnNewGame += OnNewGame;
SaveLoadManager.OnLoad += OnLoadEvent;
DaggerfallAction.OnTeleportAction += OnTeleportAction;
}
void OnDisable()
{
PlayerEnterExit.OnTransitionInterior -= OnTransitionToInterior;
PlayerEnterExit.OnTransitionDungeonInterior -= OnTransitionToDungeonInterior;
PlayerEnterExit.OnTransitionExterior -= OnTransitionToExterior;
PlayerEnterExit.OnTransitionDungeonExterior -= OnTransitionToDungeonExterior;
StartGameBehaviour.OnNewGame -= OnNewGame;
SaveLoadManager.OnLoad -= OnLoadEvent;
DaggerfallAction.OnTeleportAction -= OnTeleportAction;
}
void Start()
{
// Set number of dungeons memorized
SetNumberOfDungeonMemorized(DaggerfallUnity.Settings.AutomapNumberOfDungeons);
gameobjectAutomap = GameObject.Find("Automap/InteriorAutomap");
if (gameobjectAutomap == null)
{
DaggerfallUnity.LogMessage("GameObject \"Automap/InteriorAutomap\" missing! Create a GameObject called \"Automap\" in root of hierarchy and add a GameObject \"InternalAutomap\" to it, to this add script Game/Automap!\"", true);
if (Application.isEditor)
Debug.Break();
else
Application.Quit();
}
playerCollider = GameManager.Instance.PlayerGPS.gameObject.GetComponent<CharacterController>().GetComponent<Collider>();
if (playerCollider == null)
{
DaggerfallUnity.LogMessage("Collider on PlayerGPS not found!\"", true);
if (Application.isEditor)
Debug.Break();
else
Application.Quit();
}
// set default automap render mode
SwitchToAutomapRenderModeCutout();
// register console commands
try
{
AutoMapConsoleCommands.RegisterCommands();
}
catch (Exception ex)
{
Debug.LogError(string.Format("Error Registering Automap Console commands: {0}", ex.Message));
}
// coroutine for periodically update discovery state of automap level geometry
StartCoroutine(CoroutineCheckForNewlyDiscoveredMeshes());
}
void Update()
{