Weapon Structure - UQcsse3200/2024-studio-1 GitHub Wiki
Combat Mechanic
The player can have up to 2 weapons equipped at all times. The combat system supports 3 main weapon types: melee, ranged, and none (bare hands).
Key Components
WeaponConfig
1. - Purpose: Manages and loads weapon data from either hardcoded values or external files (e.g., JSON). It stores weapon configurations in a map and provides access to weapon details.
- Responsibilities:
- Fetching weapon data by name.
- Providing weapon attributes like damage, range, and fire rate.
WeaponData
2. - Purpose: Represents the detailed configuration for each weapon, including attributes such as damage, range, and reload time.
- Attributes:
name
: The name of the weapon.damage
: The damage value of the weapon.range
: The effective range of the weapon.fireRate
: The speed at which the weapon can be fired or used.ammo
: Current ammo count (for ranged weapons).maxAmmo
: Maximum ammo capacity (for ranged weapons).reloadTime
: Time required to reload the weapon.iconPath
: Path to the weapon's image file.
RangedWeapon
3. - Purpose: A subclass of
WeaponData
that handles ranged weapons, which have ammo and a defined reload time. - Special Attributes:
ammo
: Tracks current ammunition.maxAmmo
: The maximum amount of ammunition the weapon can hold.
MeleeWeapon
4. - Purpose: A subclass of
WeaponData
for melee weapons. These weapons have no ammunition and are designed for close combat. - Special Attributes:
- No ammunition-related fields.
FiringController
5. - Purpose: Manages the firing mechanics of a ranged weapon, including handling shooting direction, ammo consumption, and reload mechanics.
- Responsibilities:
- Handling the shooting logic.
- Managing weapon cooldowns, reload times, and ammo updates.
Pickup and Drop Mechanic
Both RangedWeapon
and MeleeWeapon
implement the Collectible
interface and are responsible for managing weapon pickup and drop in the inventory system.
Key Methods:
pickup(Inventory inventory)
: Adds this weapon to the inventory and updates the entity with the newly equipped weapon.drop(Inventory inventory)
: Removes this weapon from the specified inventory and switches the entity to a default weapon (bare hands).
Spawn Mechanic
WeaponFactory
creates weapons as collectibles or entities on the map.
Key Methods:
Collectible create(Collectible.Type type, String specification)
: Creates a weapon of typetype
using thespecification
.
Example Code
Creating a Weapon Entity
Here’s how to create a melee weapon entity:
// Creating a melee weapon collectible
MeleeWeapon sword = new MeleeWeapon("Sword", "path/to/sword_icon.png", 10, 1.5f, 0);
// Creating the weapon entity
Entity swordEntity = weaponFactory.createWeaponEntity(sword);
// Use the swordEntity in your game
And for a ranged weapon entity:
// Creating a ranged weapon collectible
RangedWeapon shotgun = new RangedWeapon("Shotgun", "path/to/shotgun_icon.png", 20, 10f, 0.5f, 5, 10, 2.0f);
// Creating the weapon entity
Entity shotgunEntity = weaponFactory.createWeaponEntity(shotgun);
Creating a Weapon Collectible Item
To create a weapon collectible item:
// Creating a melee weapon collectible
Collectible meleeCollectible = weaponFactory.create(Collectible.Type.MELEE_WEAPON, "Sword");
// Creating a ranged weapon collectible
Collectible rangedCollectible = weaponFactory.create(Collectible.Type.RANGED_WEAPON, "Shotgun");
// Adding to inventory
Inventory playerInventory = new Inventory();
meleeCollectible.pickup(playerInventory);
rangedCollectible.pickup(playerInventory);
UML Diagram
This includes the WeaponFactory, which creates melee and ranged weapons based on the name specified in the config file and the pre-set weapon data.
Each weapon entity includes 3 main components:
- WeaponAnimationController for controlling animation.
- PositionTracker for tracking the position of the player (entity) and moving along with it.
- FiringController for controlling different attack types and dealing damage, associated with weapon entities to perform actions.
Note: Range weapon also use projectileFactory
Melee Attack Mechanic:
Melee Weapon Attack Sequence:
- The player activates the melee weapon, triggering the activate(null) method.
- The FiringController calls attackMelee() for checking valid conditions and getting all entities from GameArea by calling getListOfEntities().
- Then call applyFilteredDamage() to apply damage to each nearby opponents if they standing in front of the player.
Range Attack Mechanic:
Ranged Weapon Shoot Sequence:
- The player activates the ranged weapon by specifying a direction.
- The FiringController processes this with the shoot(direction) method.
- A projectile is created using the ProjectileFactory and span the projectile Entity to the GameArea with flying direction.