Class DiagramTower:Description: The "Tower" is an abstract class that describes the behaviorand properties of a defensive tower object in a tower defense game.. Each tower instance can h
Trang 1TOWER DEFENSE GAME
Software Specification
Project Code: TD Document Code: TD_ Software Specification_v02
Ha Noi, 10/06/2023
Trang 2LEADER : Nguyễn Minh Thái REVIEWERS: TD Development Team 10/06/2023
APPROVAL: Bùi Thị Thùy 21/06/2023
TEAM MEMBER :
Trang 3Table Of Contents
Understand the Problem 3
Design a Solution 3
Class Diagram 5
Tower 5
Tower Attack 7
Unit 10
Attacker 12
Defender 13
Game Click Manager 14
Wave Manager 16
Game state manager 18
Map 20
Skill 20
Environment 22
Overview 23
1.Game Story 23
2.Game Flow 24
3.Gameplay 24
4 Map 25
4.1 GrassMap 27
4.2 FrozenMap 27
4.3 DessertMap 28
4.4 Environment 28
4.5 Health bar 30
4.6 Select Unit 30
5 Sprites 31
5.1 Menu 31
5.2 Towers 32
5.3 Bullets Tower 34
5.4 Bullet Impact Tower 34
5.5 Enemy 35
5.6 Defenders 36
5.7 Map 37
5.8 Environment 37
6 Tower 40
Trang 46.1 AOE Tower 42
6.2 Mage Tower 44
6.3 Archer Tower 46
6.4 Headquarter 49
7.Units 49
7.1 Common Behaviour 49
7.2 Warrior 50
7.3 Xena 51
7.4 Archery 52
8.Monsters 53
8.1 Common Behaviour 53
8.2 Harpy 54
8.3 Ogre 57
8.4 Minotaur 58
8.5 Banshee 60
8.6 Ghost 61
9.Spawning 63
9.1 Monster 63
10.Skill 63
10.1 Using skill 63
10.2 Magic 65
10.3 Heal 65
10.4 Meteorite 65
11.Control 65
11.1 Moving: 65
11.2 Place and control: 66
11.3 Upgrading and Summoning: 66
12.Balancing 66
12.1 Total Strength formula: 66
12.2 Pseudocode for calculating strength of monsters and player over wave 66
12.3 Graph of strength over time 67
12.4 Example 67
13 Screen 67
13.1 Menu 68
13.2 Playing 68
13.3 Popup Shopping 69
Trang 513.4 Popup MenuSkill 70
13.5 Popup when click option button in game 70
13.6 Screen when lose game 71
13.7 Mini Map 71
14 Mindmap 72
15 Interaction 73
15.1 Interaction between Warrior and Harpy 73
15.2 Interaction between Warrior and Ogre 73
15.4 Interaction between Warrior and Banshee 76
15.5 Interaction between Archery and Harpy 76
15.6 Interaction between Archery and Ogre 77
15.7 Interaction between Archery and Minotaur 78
15.8 Interaction between Archery and Banshee 79
15.9 Interaction between Monster and Tower 79
16.Summon 80
17.Script 80
17.1 Attackers 80
17.2 Defenders: 83
17.3 Tower: 84
17.4 Game Control 86
17.5 Design Pattern 88
17.6 File IO 89
17.7 Exception Handling 90
17.8 Inheritance & Polymorphism 91
18.References 92
19 Write Test Cases 92
Trang 6Understand the Problem
Implement a game called Tower Defense In the game, the player can buy defenders to resist the monster’s attack Besides that, the user can move the defenders to the target position to attack the monsters Playerswill receive support from the towers And players can upgrade the power
of towers and defenders
Upgrading turrets will cause turrets to take more damage and fire faster From there you can kill monsters easier In addition, the player can upgrade the defender's strength stats There are 3 types of turrets, each with 3 different power levels There are two types of defenders: ranged and melee
Monsters will get stronger over time They will be spawned in turns When each turn ends, the player will have 5 seconds to prepare for the next turn After turns, monsters will become stronger (attack, speed, defense)
The player's points will be accumulated through the plays Each monsterturn is a turn When the main house is destroyed, the player's score is the number of turns the monster has been destroyed During the game, the player can go to the menu to save the game and keep playing or play a new game
Design a Solution
We’ll approach our design, implementation, and testing in five iterations Specifically, we'll have iterations for basic gameplay, full gameplay, menus, sounds, and XML configuration data It should be obvious why we're thinking of the full gameplay The difficulty menu, and the pause menu as menus Perhaps less intuitively, we're also thinking of the high score menu as a menu because it will behave just like any other menu, with a clickable button to take some action (in this case, close the menu and return to the main menu Just as we did in Chapter 17, we'll make our main menu and difficulty menu separate scenes in our game Our pause menu will be a "popup menu" that appears above the gameplay scene, so we'll make that a prefab we can create and destroy as
necessary This is a different approach from the one we used in the
Trang 7previous chapter for our high score table, but recall that in our previous approach we included a canvas in the scene, then modified whether or not it was displayed as appropriate Although our game here only has one gameplay scene, you should be able to easily imagine a game with many gameplay scenes (e.g., levels) We don't want to have to add the pause menu canvas to each of those scenes in the editor, so a prefab is the better way to go Finally, our high score menu will also be a prefab We'll display it both from the main menu and at the end of a game, so having a prefab we can use in both places will be a good approach.
Trang 8Class Diagram
Tower:
Description: The "Tower" is an abstract class that describes the behaviorand properties of a defensive tower object in a tower defense game It inherits from MonoBehaviour, indicating that it's meant to be attached to
a game object in the Unity environment The Tower class outlines the core attack functionality of a defensive tower, including attack modes
Trang 9and targeting Each tower instance can hold specific properties such as damage, cooldown, and range, and manage its own bullet instantiation.Attributes:
- targetRotation: A Quaternion that represents the rotation needed to aim
at the current target
- bullet: A GameObject which is the bullet that the tower fires
- animator: An Animator that is responsible for handling animation
- cooldownTimerBullet: A Timer that handles the cooldown between bullet shots
- finishedRotate: A Boolean to check if the tower has finished rotating towards the target
- colliders: A static list of GameObjects which holds all the colliders (presumably of potential targets) the tower can interact with
- isAttack: A Boolean indicating if the tower is currently attacking
- damage, cooldown, range: Double values representing the tower's attack properties, accessible via respective public properties (Damage, Cooldown, Range)
- AttackSelectedMonster(): Handles the procedure of attacking a specificmonster when selected by the user
- AttackMonsterWithLowestHitPoint(): A targeting behavior where the tower attacks the monster with the lowest hitpoints
- AttackClosestMonster(): A targeting behavior where the tower attacks the monster closest to it
- OnTriggerExit2D(): Method to handle when a collider exits the tower's range The attack flag is turned off
Trang 10- Create(): An abstract method to be implemented by subclasses, used
to create and place a tower at a specific position
Associations:
- This class is associated with various other classes including
GameObject, Timer, MonsterTargetClickManager,
TowerModeAttackManager, TowerAttack, Unit, Attacker, AudioManager, and AudioClipName
Tower Attack
Description:
Trang 11This class is responsible for managing the attack behavior of towers in the game It contains properties and methods to deal with the attack's range, damage, the target of the attack, and even the visualization of reaching the target with an animation.
Properties:
Range: A float indicating how far the attack can reach
sourceGameObject: The game object initiating the attack.targetDirection: The Vector2 direction towards which the attack
Damage: The amount of damage the attack can cause
reachingAnimation: A GameObject that serves to display an animation when the attack reaches its target
Methods:
Trang 12Start(): A Unity engine method that sets up the count variable when the game starts.
Update(): A Unity engine method that updates every frame, checking if the attack has reached its target, applying damage, and managing relevant animations and behaviors
DoesEffectBehaviour(): A virtual method that is meant to be overridden in derived classes to define specific
behaviors/effects of the attack upon reaching the target.OnBecameInvisible(): A Unity engine method that destroys the game object when it becomes invisible to the camera, helping
to manage resources and performance
Trang 13Description:
The Unit class represents a generic, controllable unit in the game This class is abstract, indicating that it is not meant to be instantiated directly but should be used as a base for other, more specific unit types Units
Trang 14can be thought of as the main entities participating in the game's battles
HealthBar is a reference to the unit's health bar
IsAttack and StateAttack indicate whether the unit is currently attacking and the state of the attack
BaseDamage, BaseHitPoints, and BaseSpeed denote the fundamental attributes that are utilized to compute the actual attributes with respect tothe unit's level
Methods:
Initialize(float damageMultiplier, float hitpointMultiplier, float
speedMultiplier): Initialize the unit with a multiplier for damage, hit points,and speed
Attack(Unit target): Executes the unit's attack on a target unit
DisplayAttackShape(Vector2 direction, Unit target): Displays the shape
of the unit's attack, with specific logic for different types of attacks (MeleAttack, AOEAttack, RangedAttack)
TakeDamage(float amount): Reduces the unit's hit points by a certain amount and updates the health bar accordingly, triggering the Die method if hit points fall to or below zero
Die(): Defines what should happen when the unit dies (currently, the unit GameObject is destroyed)
LevelUp(): Increments the unit's level
Trang 15StrengtCaculation(): Calculates and returns the unit's strength based on its damage, hit points, and speed.
Attacker
Description:
This class is responsible for managing the behavior of attacking units in
a game It contains properties and methods for dealing with the selection
of target, range of attack, turning towards the target and controlling animations It also has behaviors for attacking enemy units or towers, continuing the movement after killing a target, and flipping direction based on the position of the target
agent: An instance of AgentMoventMentMonster class for controlling the movement of the attacker
threshold: A float for determining when to flip the direction of the attacker
FlipDirection: A Vector3 to calculate the direction between attacker and its target
dotProduct: A float to calculate the dot product of FlipDirection and transform.right
Trang 16OnTriggerExit2D(Collider2D other): A method that's called when the Collider2D other has stopped touching the trigger.
Attack(Unit target): A method that starts the attack on a target Unit.Die(): A method that handles the death of the attacker
Strength2(): A method that calculates and returns the strength of the attacker
Defender
Description:
This class is responsible for managing the behavior of defending units in
a game It contains properties and methods for dealing with the selection
of target, stopping and continuing movement, interacting with attackers, and handling the death of a defender
Properties:
Trang 17currentTarget: An instance of Attacker indicating the current target of the defender.
selection: A GameObject representing the selected unit
isInsideTrigger: A boolean to check if the defender is inside a trigger.stateAttack: An integer for tracking the attack state of the defender.UpGradedGold: An integer representing the gold gained by upgrading the defender
StrengthGold(): A method that calculates and returns the strength of the defender
Die(): A method that handles the death of the defender It calculates the value using StrengthGold method and destroys the game object.Game Click Manager
Trang 18This class, 'SkillManager', handles the operations related to the skill management of the player character in the game It contains properties and methods to choose and activate different skills, such as Hypnosis, Healing, and Meteo
Properties:
chooseSkill: An integer to keep track of the selected skill
skillCanvas: A GameObject representing the UI canvas for skills.btnHypnosis, btnHeal: Buttons for activating the Hypnosis and Heal skillsrespectively
Hypnosis, Health, Meteo: GameObjects representing the skills or effects associated with Hypnosis, Healing, and Meteo
Trang 19mainCamera: An instance of the game's main camera.
isMoving: A boolean flag indicating if a Meteo object is currently moving.Methods:
Start(): A Unity engine method that is called before the first frame update It initializes the chooseSkill, skillCanvas, and mainCamera.Update(): A Unity engine method that is called once per frame It checks for mouse input to activate the chosen skill
CreateMovingObject(Vector3 vector3): A method that instantiates the Meteo GameObject at a specified position
SkillHypnosis(), SkillHeal(), SkillMeteo(): These methods set the chooseSkill to the respective skills and hide the skillCanvas
Exit(): This method sets chooseSkill to 1 and hides the skillCanvas
Wave Manager
Description:
Trang 20This class, `UnitSpawner`, manages the spawning of enemy units in the game, following certain conditions and waves.
Properties:
- Constants such as `WIDTH_MAP`, `HEIGHT_MAP`, and
`DELTA_TIME_PER_WAVE` which define gameplay parameters
- `attackersQueue`: A queue to hold and manage the attackers in the game
- Prefabs of game entities like `prefabBanshee`, `prefabHarpy`,
`prefabMinotaur`, `prefabOrge`, `prefabGhost`, and `prefabBoss`
- Public getters and setters for `StrengthPerWave`, `CountWave`,
`AccumStrength`, and `TypeUnit`
- Parameters to define the maximum and minimum values for the x and ycoordinates of spawning positions
- A `System.Random` instance `rand` for generating random numbers
- Other private variables such as `isEndWave`, `level`, and `timer`.Methods:
- `TriggerCondition()`: A function to check the condition for spawning the boss Currently, it always returns false
- `Start()`: A Unity engine method that initializes the wave count, wave strength, and spawns a new wave of attackers
- `Update()`: A Unity engine method that checks the completion of a wave and initiates a new wave if completed
- `IsFinishWave()`: Checks whether a wave is finished by checking if there are any remaining attackers
- `SpawnNewWave()`: Generates a new wave of attackers based on the total strength for the wave
- `IncreaseStrength()`: Calls the `LevelUp()` method on all attacker types
to increase their strength
- `GenerateUnit(GameObject prefab)`: Creates a new unit of the type provided by the GameObject argument at a random position within defined bounds
- `StartNewWave(float delay)`: A coroutine that waits for a given delay then starts a new wave If the previous wave has finished, it increments the wave count and strength per wave
Trang 21Usage:
This class would typically be attached to a manager GameObject in the scene Its purpose is to manage the spawning of enemy waves in the game, controlling their strength and frequency, and checking conditions for wave completion and boss spawning
Game state manager
The MenuMainManagement class serves as the control center for the main menu of your game The class is equipped with the functions that correspond to the buttons present in the main menu
This class also has functionality for saving and loading the state of the game by converting game object data into a JSON string and storing it using Unity's PlayerPrefs These functions can be invoked when the player decides to continue the game after exiting or wants to start a new game
The functions in this class can be explained as follows:
Start(): This function is invoked when the scene starts It adds an invoker
to the EventManager for the ResetGold event
ToResetGold(int value): This function invokes the ResetGold event with
a value of 0
Trang 22NewGameButtonOnClickEvent(): This function loads the scene "map" when the New Game button is clicked.
ContinueButtonOnClickEvent(): This function loads the saved game and then loads the "map" scene when the Continue button is clicked.QuitButtonOnClickEvent(): This function quits the application when the Quit button is clicked
SettingButton(): This function enables the canvas of the child
GameObject tagged with "optionInGame" when the Settings button is clicked
getGameChildObject(GameObject target): This helper function returns the GameObject named "BackGround" which is a child of the input GameObject
ExitSettingButton(): This function disables the canvas of the child GameObject tagged with "optionInGame" when the Exit button in the settings is clicked
SurrenderButton(): This function resets the game information and loads the "GameOver" scene when the Surrender button is clicked
ExitSettingButton2(): This function loads the "Menu" scene when the second Exit button in the settings is clicked
RestGame(): This function saves the state of the game by storing information about all game objects in PlayerPrefs The information is serialized into a JSON string before saving
Overall, this class controls the flow of your game from the main menu, allowing the player to start a new game, continue a saved game, access settings, and quit the game
Map