IMPLEMENTATION AND COMPARISON IN USING STATE PATTERN ON MAIN CHARACTER MOVEMENT (CASE STUDY: POCONG JUMP VIDEO GAME VERSION 1.0)

Article History: Game development success is often hard to achieve due to various problems such as performance issues, malfunctioning features, and poorly organized program structure. The problems that arise can be prevented by using the design pattern as a game programming architecture from the beginning of development. By implementing a design pattern, the process of developing video games can be made easier and simplified. The development team can focus its efforts on producing better quality video games. In this study, design patterns that would be used are state patterns and finite state machines. The state pattern is implemented by encapsulating the character's behavior in a class called state. The finite state machine will then facilitate the transition of states caused by user/player input or variable value changes. State pattern and the finite state machine are tested with a test case and game performance is tested with software metrics. The result obtained from this study are state patterns and finite state machines have a valid component structure and could improve performance efficiency in video games.


INTRODUCTION
Eternal Dream is a video game development startup company located in Bandar Lampung City, Lampung and has been established since 2017. This development team is one of the video game developers that has experienced development in Indonesia. The revenue earned by video game developers in Indonesia in 2019 reached $1.08 Billion [1].
Based on the information provided by Lucky Putra Dharmawan, the CEO of Eternal Dream and who at that time was the programmer of the Video Game Pocong Jump, Pocong Jump had problems in program structure, functionality, and performance. The structure of the programs contained in video games is not neatly arranged and systematically. In addition, Pocong Jump also has problems with inefficient memory allocation, ineffective coroutine use, and improper use of animations, resulting in video games experiencing poor performance when running on platforms that have 1GB of RAM. In Game Development, we have to use Research and Development to make games. One of the ways to make R&D is to do a comparison between the previous program code and the better program code to produce increased performance [2]. In this research, we are using design pattern for the code.
The use of design patterns as a game programming architecture has been widely applied in the video game development process, with the aim of simplifying and simplifying the video game development process so that it can be easily understood by the development team [2]. We also must to awareness about Context awareness and adaptation, which are crucial aspect in mobile game based learning [3]. The use of design pattern has multiple benefits for game software, because hardware will communicate with input, data transform, and output [4]. The development team can allocate its resources to build video games that have better performance, functionality, and structure, because of which the quality of the video games developed improves. One of the design patterns that can be used to improve video game quality is to apply a state pattern [5], [6] with a finite state machine [5]. FSM not only can be used for games but also another platforms, such as medical area, because FSM is an abstract machine that can allow the storage and also processing of all information with order-sensitive patterns [7].
In this research, We decided to use state pattern which can be implemented in a synchronous approach with FSM [8]. The authors will apply state patterns and finite state machines to Pocong characters in eternal dream's Pocong Jump video game. The state pattern encapsulates the character's behavior on the Pocong character then creates three states, namely silence, jump, and death. The finite state machine oversees handling the transfer between states and then the system will execute the currently active state.

RESEARCH METHODS
This research was used to find whether using the state pattern makes the video game develop better. This was done because A well-developed video game will give players a special gaming experience [9], [10]. Video games have several genres referred to as "The Classics Game Genre". These genres are shooter, action and arcade, platformer, fighting, strategy, role-playing, sport, vehicle, construction and simulations, adventure, and puzzle. Meanwhile, in this study, the Pocong jump video game has a platformer genre where Platformer is one of the genres of video games where characters move by jumping from one platform to another. Platformers give players a challenge that can be fighting or avoiding a trap [11]. Platformers can be divided into two types, namely single-screen platformers and scrolling platformers. In a single-screen platformer, characters can only move within a scope as large as the monitor screen, while in scrolling platformers, the screen moves following the character's movements [12].

Interview
The interview method is carried out by conducting interview directly with the source. The author conducted an interview with Lucky Putra Dharmawan, the CEO of Eternal Dream and who at that time was the programmer of the video game Pocong Jump.

Observation
The observation method is carried out by studying the behavioral function of the Pocong character by playing the Pocong Jump video game and reading the program code contained in the Pocong character. Here is the flowchart of the program code on the Pocong character that we can see in Figure 1 below. Figure 1 shows the simple logic for playing this game is when the main character touches an obstacle or trap that we put somewhere inside the game. The main character will respawn if the main character touch the ground. For this logic, we can put ground below the screen, so the player cannot see it, but that logic will make the main character respawn when fall into the trap that not visible on the screen. Figure 2, Figure 3 and Figure 4 are made to Shown an update function, it will be use to set the main character movement and other feature that we put inside the game.  Figure 4 shown above show the update process for the character for moving and control. As we can see in Figure 2, at the start program will ask abour where player is, whether it is on ground and is it mirrored or not. When the program knows that the character is at the ground, it will check about setPower, and setPower are true then the last check checks the mirrored position of a character. If it turnRight (not mirrored), hen the program will calculate increment forceX and forceY for each frame and set the value cap for forceX to 6.5f and forceY to 13.5f while it is mirrored (turn left). The program will calculate the different values. While the forceY is still the same value, forceX will be set to -6.5f, which means that the X axis will send back to the left of the character. In this progress and calculation, the program will show the increment of the PowerBar charge value and show the user the charge meter. This progress will show no change to the character because we do not input anything to the character when the program starts, but the process will continue to Figure 3. To change the direction of the character, shown in Figure 2 that the program will take the input and check the right input, we can see that if we press KeyDown(A) while the character is faceRight, it will change the direction to faceLeft and if we press KeyDown(A) while the character is faceLeft, it will change the direction to faceRight. While the program knows the direction of the character, this process will continue to keep checking the forceY, forceY, thresoldX, and thresoldY. The last step in Figure 4, shows that the user press the input KeyDown(Space), program will checking the value of power and if the power already released the program will check the character are on the ground or not, the character will jump with the certain value that the program already collect before.

Research Framework
The research stage is needed so that the research carried out is carried out regularly and systematically to achieve the goal. The stages of research carried out in this study can be seen in the form of a diagram in Figure 5.  Figure 5 shows the steps for doing this research. The first step is a literature study, like collecting the data and interviews. After that, we do some research analysis to determine the core problem to search for a solution. After that, we do the system design that will manage the next steps, which are system implementation, the code is made, and after that, the last step is collecting the data with a system test. All of the data that are collected will then be used for the result of this research.

Designing the State Pattern
The state pattern is a class that takes the form of an interface. The class has several components that will be passed on to the child called the state. The components contained in the state pattern will be executed inside the character class loop. In the Unity game engine, the class is the class that implements the MonoBehaviour class. The controller class plays a role in helping the finite state machine function to regulate the course of transitions between states. The state pattern class diagram can be seen in Figure 6.

Designing the Finite Stage Machine
Finite state machines are used to regulate transitions between states that occur within the state pattern [13]. In Figure 7. the design of the finite state machine is depicted using a state machine diagram.  There are three states in the finite state machine system. Those states are IdleState, JumpState, and DieState. At the beginning of the first time the game is run, the character will enter the IdleState. In this state, the character stays in place and the player can change the orientation of the Pocong character to face left or right.
When the player presses and holds the jump button, the Pocong character will fill the jump bar to determine the strength of the jump. When the player releases this button, the state will transition to jumpstate. In this state, Pocong characters will perform jumping actions to move or avoid obstacles. When the character hits the ground, the state will move back to IdleState.
When the character is hit by a trap that causes lives to decrease, the system will force the character to make a transition to DieState. In this state, the character will play a dying animation according to how the Pocong character died. The system then runs a three-second countdown to return the character to the checkpoint position (respawn).

State Pattern Implementation
The implementation of the state pattern begins with creating an interface to facilitate the development and maintenance of video games. Interfaces are also used to homogenize structures within the state class. In the IStatePattern interface, there are four framework methods in the IStatePattern interface, namely Init, Enter, HandleLogic, and HandleState. One example of an implementation of the IStatePattern interface is the creation of a state that will be shown in the following IdleState class.

Finite State Machine
The finite state machine implementation is done inside the HandleState method contained in each state as in the IdleState class. In the Pocong character, there are three actions that can be divided into three states, namely IdleState, JumpState, and DieState. Below is a program implementing the finite state machine by using the Controller class to replace the active state. In Table 1, you can see the actions performed by the Pocong character. Pocong character has an active state that is shown above. We use IdleState for 4 states (idle, orientation, hold, and revive), jumpState when the character performs jumping, DieState when the character die. While the player can do something for the character, like touching space to fill power jump or A to change orientation, IdleState will be shown.

Without Design Pattern With Design Pattern
In Table 2, shows the gameplay comparison of Pocong characters between those who are not and those who use the design pattern. To make it different in design when comparing this research, we made a few changes in the bottom right of the design pattern. While there is no difference in design, there are so many results that will be shown in this research later below.

State Pattern Implementation
State pattern testing is carried out with a test case to find out the correctness of the system to be executed. The results of the tests can be seen in Table 3.

Finite State Machine Implementation
Finite state machine testing is carried out with a test case to determine the validity of the state displacement. The results of the tests can be seen in Table 4.  Table 4 shows Finite State matching Testing, and test cases are the transition between states. In the test case IdleState, we can see steps or conditions we made for testing this state when no input state does not transition, but when the spacebar is released after pressing or holding it, IdleState changes into JumpState. It also changes into DieState when the character hits traps. For the JumpState, the transition we want to see is from JumpState to IdleState, and DieState is already shown in Tabel 4 above. DieState also gives appropriate test results when we give no input, and automatically after 3.5 seconds, DieState will change into IdleState to Respawn the character.

Performance Testing
For testing, we use a test case, which is a software test based on several predefined input scenarios. The test case is carried out by comparing the expected results with the actual results that occurred at the time of testing. If there is a discrepancy between the two, the program code must be corrected [14]. Performance testing was carried out by comparing Eternal Dream's Pocong character behavior program with the program that has been designed in this study. Testing is carried out using a profiler by turning on the deep profile to determine the program execution time of each frame. To facilitate the testing process, this study used 100 pieces of prefab characters. The results of algorithm performance testing can be seen in Table 5 and Table  6.  Table 5 shows performance testing without a design pattern. We can also see the result in Figure 8. With this testing, we take five times data retrieval for each State. For IdleState the average time for executed the code is 1.3ms, the average Orientation is 1.3ms, the average of JumpCharge is 1.5ms, and the average shown below.  In Table 7 and Table 8, you can see the test results of algorithms that have used design patterns,  Table 7 shows performance testing with design pattern that already implemented. With this testing we take five times data retrieval for each State. For IdleState the average time for executed the code is 0.4ms, the average of Orientation is 0.5ms, the average of JumpCharge is 0.6ms, the average of JumpButtonRelease is 0.9ms, the average of MidAir is 0.3ms, and the average when Die is 0.2ms.  We use Software metrics as indicators for software development to measure the quality of software. Software metric contains information that is quantitative in nature to evaluate the efficiency of certain programs or features contained in the software. One of the indicators that are often used in metric software is execution time. Execution time is the time it takes the CPU (Central Processing Unit) to execute a program from start to finish [15]. In Figure 11, information is obtained that both algorithms have a relatively stable execution time, with programs that use the design pattern having a faster execution time. When the jump button is released, there is a very significant spike in execution time occurring in Eternal Dream's algorithm. In Figure 12, information obtained from algorithms with a design pattern has a higher FPS and is more stable than algorithms that do not use design patterns. The weight of computing during JumpButtonRelease makes video game performance decrease for a moment, so players feel a lagging when characters move.

CONCLUSIONS
Based on the results of research conducted on the behavior of Pocong characters, it was concluded that Pocong characters in the Pocong Jump video game could be developed by applying design patterns in the form of state patterns and finite state machines. The program code that implements the design pattern has a more organized structure and a faster program execution time. The test results of the state pattern and finite state machine components on the Pocong character show valid values. The value describes that the component has been appropriate and successfully tested.
Some suggestions that researchers can give to conduct further research, namely: The test results of the state pattern and finite state machine components on the Pocong character show valid values. The value describes that the component has been appropriate and successfully tested.
Some suggestions that researchers can give to conduct further research, namely: 1. The number of states carried out in this study is relatively small, it is hoped that in the future a study will be carried out with a larger number of states with different transitions.
2. It is hoped that in the future research related to the use of state pattern and finite state machine methods will be carried out in different cases, such as the behavior of non-player character movements or the interaction of the non-player character with players. 3. Develop this research by testing the design pattern and architecture of other video games, such as observers, object pooling, spatial partitions, and others.

ACKNOWLEDGMENT
With the creation of this study, the author expresses his deepest gratitude and gratitude to all parties who contributed to the study. Thank you to Lucky Putra Dharmawan for being willing to provide interviews, materials, and research objects. Thank you to a team from Teknokrat Indonesia University who are willing to help with this collaboration research. As well as thanks to team from Sam Ratulangi University..