Game Development Code Documentation
Lim Shi Zhe (0357534)
Bachelor of Computer Science
Game development- Task 3 Code Documentation
Basic Control(Player controller)
The player moves left and right using A/D or arrow keys.The player flips based on movement direction. This is the basic horizontal movement.
These variables are used for double jump.
-maxJumpCount
sets how many jumps the player can do.-remainingJumps
keeps track during the game.-doubleJumpCost
is how much energy the second jump uses.-isGliding
checks if the player is gliding.
-glideDrag
and glideGravityScale
control falling speed while gliding.-glideEnergyCost
is the energy used per second when gliding.
-normalGravityScale
stores the original gravity to restore after gliding.-dashSpeed
sets how fast the dash is.-dashDuration
controls how long the dash lasts.-dashCooldown
is the wait time before next dash.-dashEnergyCost
is how much energy one dash uses.-isDashing
checks if the player is currently dashing.-dashEndTime
and nextDashTime
help manage timing for dash and cooldown.These variables are used for the energy system.
-maxEnergy
sets the highest amount of energy the player can have.-currentEnergy
keeps track of energy during the game.-energySlider
and energyFill
are for showing energy in the UI.-lowEnergyColor
and fullEnergyColor
change the UI color based on energy level.canDoubleJump
, canDash
, canGlide
: Determine if the player can use double jump, dash, or glide. This allow me to limit the character ability in different level. This function handles player input and ability logic every frame:
-
Movement: Gets horizontal input (
move.x
) if controls are enabled. -
Jumping:
-
Player jumps freely from the ground.
-
Double jump only works if allowed, with enough jumps and energy.
-
-
Gliding:
-
If enabled and the player is falling, starts gliding and slowly reduces energy.
-
Stops gliding when conditions are no longer met.
-
-
Dashing:
-
If dash is enabled and off cooldown, starts a dash and uses energy.
-
-
Disabling movement:
-
If
controlEnabled
is false, player can't move.
-
-
Dash timer:
-
Ends the dash after
dashDuration
.
-
-
Jump state update:
-
Calls
UpdateJumpState()
to manage jump transitions.
-
-
Calls
base.Update()
from the parent class to apply physics.
This function manages the different stages of the player's jump:
-
PrepareToJump:
-
Sets the player into the jumping state.
-
If not on the ground (i.e. a double jump), it reduces remaining jumps and uses energy.
-
Plays jump sound.
-
-
Jumping:
-
Waits until the player leaves the ground, then switches to the InFlight state.
-
-
InFlight:
-
When the player lands, resets jump state and remaining jumps.
-
Plays landing sound.
-
-
Landed:
-
Transitions back to Grounded state.
This function calculates and sets the player's movement speed each frame:
-
If dashing, set a fast horizontal velocity and stop falling (
velocity.y = 0
). -
If jumping, apply upward force.
-
If stopJump is true, reduce upward speed for short hop effect.
-
If gliding, slow down falling speed.
-
Updates sprite direction based on movement.
-
Sets animation values (grounded, speed, gliding, dashing).
-
Finally, sets the
targetVelocity
based on input and speed.
-
These methods handle restoring and displaying energy:
-
RestoreEnergy(float amount)
: Adds energy (without going over the max), updates the UI, and plays a sound. -
UpdateEnergyUI()
: Updates the energy bar's value and color based on current energy
PlayerHealthUI Summary
This script manages the heart-based health display for the player.
What it does:
-
Shows full or empty hearts based on the player's current health.
-
Loops through the heart UI images and:
-
Sets a full heart if the player still has that life.
-
Sets an empty heart if not.
-
Perfect for a classic 3-heart health bar .
PlayerEnergy Summary
This script manages the player's energy system, including its UI display.
What it does:
-
Stores and tracks current vs. max energy.
-
Provides methods to:
-
RestoreEnergy(float amount)
– increase energy. -
ConsumeEnergy(float amount)
– decrease energy.
-
-
Updates the energy bar UI (Slider) in real time.
This script controls a boss's behavior, including two types of attacks, energy management, and death.
Attacks:
-
Attack1 – Shoots a spike projectile (after delay + cooldown).
-
Attack2 – Spawns ground spikes (after delay + cooldown).
-
Both consume energy when used.
Energy System:
-
Boss has limited energy.
-
Energy is reduced by each attack.
-
Boss dies when energy runs out.
Death:
-
Triggers "Die" animation.
-
Boss is destroyed after 2 seconds.
Collision Damage:
-
If the boss touches the player, it deals damage.
BossTrigger Summary
This script activates the boss fight when the player enters the trigger zone.
What it does when triggered:
-
Activates the boss.
-
Closes the door/wall behind the player.
-
Shows the boss platform.
-
Displays the boss health bar UI.
-
Enables an extra item (e.g. health or energy).
-
Disables the trigger itself after activation (only runs once).
Spike Projectile
SpikeProjectile Summary
This script controls the boss's flying spike projectile.
What it does:
-
At
Start()
, the spike flies left with setspeed
. -
When it hits the player, it:
-
Deals damage (
TakeDamage(damage)
). -
Destroys itself.
-
-
When it hits a wall, it also destroys itself.
Feather Pickup
FeatherPickup Summary
This script handles a collectible feather item that restores player energy and appears/disappears on a timed cycle.
What it does:
-
Controls the visibility cycle of the feather using timers.
-
Plays appear/disappear particle effects.
-
Detects player collision and restores energy on pickup.
-
Disables itself after collection.
DisappearingPlatform Summary
This script controls a platform that appears after a delay and disappears permanently after a short time.
What it does:
-
Hides the platform at the start of the game.
-
After a specified delay, makes the platform appear.
-
Keeps the platform visible for a set duration.
-
Then permanently disables it (both visually and physically).
Key methods:
-
SetPlatformState(bool isActive)
– toggles platform's collider and renderer. -
Update()
– handles timing logic for appearance and disappearance.
DialogManager Summary
This script manages NPC dialog sequences and displays them on the UI.
What it does:
-
Controls dialog UI display using TextMeshPro elements.
-
Shows dialog lines sequentially when the player presses E.
-
Starts a dialog with an NPC name and an array of dialog lines.
-
Automatically hides the dialog panel when finished.
Key methods:
-
StartDialog(string[] lines, string npcName)
– initializes and shows the dialog panel. -
DisplayNextLine()
– advances and shows the next dialog line. -
EndDialog()
– closes the dialog panel.
PauseMenu Summary
This script controls the game's pause menu and time scaling.
What it does:
-
Listens for the P key to toggle pause/resume.
-
Freezes or resumes game time using
Time.timeScale
. -
Displays or hides the pause menu UI.
-
Allows restarting the current level or returning to the main menu.
Key methods:
-
Pause()
– activates pause UI and stops time. -
Resume()
– deactivates pause UI and resumes time. -
Restart()
– reloads the current scene. -
BackToMainMenu()
– loads the main menu scene.
Gameover UI
GameOverUI Summary
This script manages the game over screen and player retry flow.
What it does:
-
Handles the Game Over panel display and logic.
-
Freezes game time when shown.
-
Provides options to restart the current level or return to the main menu.
Key methods:
-
Show()
– displays the Game Over UI and pauses the game. -
RestartGame()
– reloads the current scene. -
ReturnToMainMenu()
– loads the main menu scene.
LevelEndTrigger Summary
This script handles the end-of-level sequence when the player reaches the goal.
What it does:
-
Triggers a "You Won" UI and pauses the game.
-
Optionally shows a message and image (e.g. for collecting a key) before ending.
-
Provides buttons for Restart, Return to Main Menu, or Load Next Level.
Key methods:
-
OnTriggerEnter2D()
– detects the player and starts the victory sequence. -
ShowMessageAndEndLevel()
– coroutine that shows message/image, then victory UI. -
RestartGame()
– restarts the current scene. -
ReturnToMainMenu()
– returns to main menu scene. -
LoadNextLevel()
– loads the next scene in build order.
Comments
Post a Comment