Skip to content

level system

yenmoc edited this page Sep 3, 2023 · 7 revisions

Information

The current Level System uses Prefab to store information for a level, and uses Addressable to load the level runtime

Note that to avoid a large prefab size, you can consider using a SciptableObject to store setting information for shared data that the script uses in the prefab level to ensure a reduction in the number of refs created inside the file. YALM. In addition, you should also use prefab variant for variations.

You can find the Level System menu under Wizard. If you have not installed Addressable, Level System requires you to install Addressable to be able to use it. Click the Install button to automatically install the Addressable package. If you have installed the Addressable package, the Install button will no longer appear, instead there will be an Open Level Editor button to open the window to support faster leveling by pre-selecting prefabs you can find information about level editor at here

Design

The main components of the level system include LevelSetting LevelComponent and LevelCoordinator

Level Setting

image

Contains settings for a certain group of levels such as level setting for a separate normal level, level setting for a separate boss level.

  • TotalLevel: total number of levels in this level group
  • NumberInSegment: the total number of levels of this type in a segment
  • Schema: Prefab name structure of the level in this group
  • LevelType: General type of this level group
  • LevelLoopReplace : List of levels to be replaced by another level when looping a level (such as replacing levels with tutorials or levels that are too easy)

Level Component

image

image

  • originLevelIndex : is the original index of the level, ex : originLevelIndex = 10 out of 100 levels
  • currentLevelIndex: is the current index, ex: currentLevelIndex = 137 means looped again for the first time
  • winEvent : event will be called when win level (you can logevent tracking win level here)
  • loseEvent : event will be called when a level is lost
  • replayEvent : event will be called when the level replays
  • skipEvent : event will be called when the level is skipped

It's an abstract class so suppose you need to handle your normal level group, create a script NormalLevel then inherit LevelComponent and implemnet the abstract methods then deploy your own code

Level Coordinator

image

  • Id: to differentiate in case you have multiple coordinator levels
  • CurrentLevelIndex: Stores the current position
  • EventLoadLevel: event used to load level according to CurrentLevelIndex
  • EventGetNextLevelLoaded: event used to get the current loaded level
  • EventGetPreviousLevelLoaded: event used to get the previous level after calling EventLoadLevel
  • LoopType: Loop level type, shuffle will shuffle the level after the loop level's turn
  • LevelSetting: Considered as the distribution map of the types of levels that will be distributed, to make it easier to visualize you can look at the image above, LevelSettings we have 6 elements Normal, Normal2, Boss , Normal, Normal2, Special. That is, levels will be allocated in this order of appearance. In addition, each level setting has an attribute NumberInSegment, which indicates the number of this type of level appearing in the level diagram.

As here, NumberInSegment of level Normal is 2
NumberInSegment of level Normal2 is 2
NumberInSegment of Boss level is 1
The NumberInSegment of the Special level is 1

So we have a schema formed according to the LevelSettings above which will be N, N, N2, N2, B, N, N, N2, N2, S Just like that, the level order will repeat according to this scheme

Setup

Prefab level need be mark as addressable and attach script implement LevelComponent

image

Create level setting and fill all info

image

Create gameobject in persistent scene and attach component Level Coordinator

image

Usages

You need to know the index of the current level to play, so we need to declare it

[SerializeField] private IntVariable currentLevelIndex;

To perform load level we need to declare event from ScriptableEventLoadLevel it must match the event attached in LevelCoordiantor.

[SerializeField] private ScriptableEventLoadLevel loadLevelEvent;

Now we can load a level with a known index as follows

var prefab = await loadLevelEvent.Raise(currentLevelIndex.Value);
// you can use prefab to create instance right here 
// or use it somewhere else using event `ScriptableEventGetLevelCached`
Clone this wiki locally