The making of our Weapon Manager and how important object oriented programming is
|
When I first wrote my weapon manager I did not think about the importance of programming in a object oriented mindset. This lead to a limited script that made it hard to implement new weapons with different stats and behavior. This was something I later needed to rewrite and in this blog post I will talk about how I perceived this encounter. At the start, the weapon manager contained the stats for a general weapon for example fire-rate, spread and recoil. It also used a Transform reference that would have the location and rotation of where the bullets world spawn. So what I did was place the spawn point in front of the default weapon sprite and spawned different projectiles depending of witch weapon you used. This was a big problem because now I could only use sprites that would be the same size and located at the same location as the default sprite. So my solution was to make all weapons into their own objects containing their own information. Now I could create a stats script that would contain all values and behaviors that the weapons need to function that previously existed in the weapon manager script. The only thing left in the weapon manager script now is the function to change between weapons by disabling and enabling the game objects in an array. The result was an easy to use WeaponManager with a clean interface that grants the ability to add new weapons with low effort and to create weapons with their own specific behavior and stats in the new stat script. One extra things that becomes much easier is to modify the weapons in game for example, if you want have a weapon level or buff ability you can simply just change the stats in the weapon object you want to edit. What I have learnt from this experience is the importance to sort and structure code into an object orientated mindset, so that I can reuse and easier access elements in a specific object. EXTRA One interesting behavior that is contained in the stat script is the sprite recoil animation function. What is does is that when you fire a projectile it moves the weapon sprite back in relation to the player and the slowly return to its original spot. Here is a picture of the pattern
Credits go to Erik Säll for the idea |