Hey again people! I realize I have another topic on animation on here too, but I find that working on problems in parallel seems to spare me on. I have a weird way of being productive, but it seems to work for me :) I have limited programming experience, but despite that, I've been working on a prototype for a game for about 3 years, and it's going surprisingly well! I'd also like to add that I'm open to suggestions, but I'd like to keep them within my grasp (CS graduate with 3 years hobbyist XNA/Monogame experience (3 years total)), and also not completely rip my code apart and start from scratch, even if there are "better" ways of doing it. However, I'm very open to making changes to my current design pattern.
So, all players in my game have 3 empty item slots. At the beginning, they can equip any 3 items from a randomly drafted item pool and use them any combination that they see fit. Balancing this will be a whole world of pain, but I'll worry about that later! There are currently 5 types of item: Passive, Activate, Deploy, FixedGun and PivotGun.
-Passive items obviously affect the player if they are simply equipped for the duration of the round.
-Activate items create a simple effect, then have some effect on the player or surroundings - also given a cooldown time.
-Deploy items drop an entity into the GameSpace, such as a turret, defensive barricade, or minion, for example.
-FixedGun items are guns that only fire forwards or in a set direction/pattern (think Megaman/Metroid/Castlevania).
-PivotGun items are guns which will rotate and aim at the cross-hair. The bullets will move at an angle and are aimable with the mouse/right analogue stick (Think Abuse/Capsized).
I've currently designed it so that with any player, any item can simply be loaded in with an arsenal-changing method whenever the player starts the level or enters a shop. This is working fine at the moment, and the under-the-bonnet code makes it very easy to create new items, so that if I do create one, everything required is in a single CS/JSON/XML sheet and no other code in the whole solution has to be changed. Obviously, it's nearly always impossible to predict any future hitches they maybe come up, but I've been constantly tweaking my hierarchy and would like to consult you guys to see if it makes sense:
(I mixed up FixedGun and Gun in my diagram. Whoops! FixedGun inherits from Gun, so just swap the two names around when looking at the above diagram)
I've made a few types of items to test this hierarchy and account for future changes and they seem to be working well. When the guns are fired (FixedGun and PivotGun), they generate the bullets accordingly (Which inherit from BaseProjectile), then these bullets are added to a static BulletList in the GameSpace. I kept this list static and public since there will only ever be one instance of the GameSpace at any given time. These bullets are then checked against every Entity nearby (I implemented spatial hashing). This allows very interesting tactical combinations. For example, if I choose to un-anchor the shop, players can use weapons with knockback to shoot the shop around, and it will collide with tiles and bullets. Force can also be applied to existing bullets, since the BaseProjectile itself inherits from the Entity class. This allows bullets to effectively hurt players or enemies, destroy certain props or move the props, or for projectiles to hit other projectiles (a force gun could blow mines away, for example). I want to keep this consistent, because I like a massively interactive environment, and don't want anything to be "hacked" in and miss out on this web of co-interactivity. This also gives a cool "domino" type effect. So, as another example, I could hypothetically place a jump pad on the floor, then place mines, then shoot the mines at the jump pad, and the mines will constantly bounce on that pad or into the next one, giving a seamlessly transitioning GameSpace.
Also, despite the items working well, I've really struggled with setting up the PivotGun weapons, and haven't finished implementing them yet. I want the bullets to do a number of things:
-Bullets originate in the weapon's nozzle
-Bullets start at the same angle as the weapon's angle when fired
-Bullets move towards the cross-hair
-A muzzle flare effect to originate over the nuzzle when fired and be attached to the end at the correct angle
The diagram below shows how I effectively want it to play out:
As it stands, I've got the bullets moving from the player's shoulder to the cross-hair. The gun also aims at the cross-hair, but in this fashion:
In this current state, the aim of the gun is not taken into consideration, as its positioning is offset from the player's arm. It's worth noting that I draw my guns WITH the arm attached in the PNG file, as I like to my my player's arm position differently depending on the gun. The bullet travels as intended, and the aiming works to an extent, but I'd like the actual end of the gun to aim toward the cross-hair and for the bullets to originate inside the nozzle (like an actual gun). I considered giving the character a completely straight arm with the gun centered every time, but this really limits my creativity :(
Here's a bit of pseudo for how the bullet is created:
//Assume everything else is working fine
//Little code should be passed into the actual bullet
Vector2 gunPosition = position + gunOrigin;
direction = new Vector2(mouseVector.X, mouseVector.Y) - gunPosition;
direction.Normalize();
if (spriteEffect == SpriteEffects.FlipHorizontally)
{
gunRotation = (float)Math.Atan2(-(double)direction.Y, -(double)direction.X);
}
else
{
gunRotation = (float)Math.Atan2((double)direction.Y, (double)direction.X);
}
//Rotation should account for nozzle position
//startingPos must be inside the nozzle
//Nozzle need to be attached to end of gun
startingPos = new Vector2((int)nozzleRect.X, (int)nozzleRect.Y);
spriteBatch.Draw(singleRedPixel, gunOrigin + manualOffset, angle, etc)...
//Draws a red rectangle over what I define as the nozzle
if (gunFired)
{
bullet b = new bullet(startingPos, gunRotation, velocity)
}
This is in its simplest form. I'd like to have it so every time I create an instance of a PivotGun, I can manually feed in an offset for the nozzle, then the gun can aim and bullets can originate based on that. My trigonometry is very rusty though!
So, I guess, based on that, my 2 questions are:
1.) Is my hierarchy / pattern design decent?
2.) How would I add this new behaviour to cross-hair-aimable guns? (Has to take into account varying bullet sizes and gun shapes/sizes)
Help would be greatly appreciated :)