Finished making a basic gun and attaching it to the player!

This commit is contained in:
JoshuaNavarro 2019-07-20 18:24:47 -07:00
parent 71868bd0b6
commit 4ac1473d9e
7 changed files with 292 additions and 8 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 226 B

View File

@ -0,0 +1,250 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Revitalize.Framework.Minigame.SeasideScrambleMinigame.SSCProjectiles;
using StardustCore.Animations;
namespace Revitalize.Framework.Minigame.SeasideScrambleMinigame.SSCGuns
{
/// <summary>
/// A simple gun class for shooting projectiles. Doesn't necessarily *have* to be a gun. Could be a slingshot or anything really.
/// </summary>
public class SSCGun
{
/// <summary>
/// Constant that represents the placeholding number for infinite ammo useage.
/// </summary>
public const int infiniteAmmo = -1;
/// <summary>
/// The projectile this gun uses.
/// </summary>
private SSCProjectile _projectile;
/// <summary>
/// The sprite for the gun.
/// </summary>
public AnimatedSprite sprite;
/// <summary>
/// The ammo remaining in the gun.
/// </summary>
public int remainingAmmo;
/// <summary>
/// The max ammo this gun can hold.
/// </summary>
public int maxAmmo;
/// <summary>
/// Whether or not this gun has ammo.
/// </summary>
public bool hasAmmo
{
get
{
return this.remainingAmmo > 0 || this.remainingAmmo == SSCGun.infiniteAmmo;
}
}
/// <summary>
/// How many bullets per shot this gun consumes.
/// </summary>
public int consumesXAmmoPerShot;
/// <summary>
/// The time in milliseconds it takes to reload a single bullet.
/// </summary>
public double reloadSpeed;
/// <summary>
/// The time remaining to reload the gun.
/// </summary>
public double timeRemainingUntilReload;
/// <summary>
/// Checks if the player is reloading the gun.
/// </summary>
public bool isReloading;
/// <summary>
/// Delay between shots in milliseconds.
/// </summary>
public double firingDelay;
/// <summary>
/// Remaining milliseconds until gun can fire again.
/// </summary>
public double remainingFiringDelay;
/// <summary>
/// A reference to the projectile this gun uses.
/// </summary>
public SSCProjectile Projectile
{
get
{
return this._projectile;
}
}
/// <summary>
/// The positon of the gun.
/// </summary>
public Vector2 Position
{
get
{
return this.sprite.position;
}
set
{
this.sprite.position = value;
}
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="Sprite">The sprite for the gun.</param>
/// <param name="Projectile">The projectile the gun uses.</param>
/// <param name="MaxAmmo">The max ammo this gun has.</param>
/// <param name="FiringRate">The delay between this gun can fire.</param>
/// <param name="ReloadSpeed">The rate at which the gun reloads.</param>
/// <param name="ConsumesXAmmo">How many bullets per shot the gun consumes.</param>
public SSCGun(AnimatedSprite Sprite,SSCProjectile Projectile,int MaxAmmo,double FiringRate,double ReloadSpeed,int ConsumesXAmmo=1)
{
this.sprite = Sprite;
this._projectile = Projectile;
this.maxAmmo = MaxAmmo;
this.remainingAmmo = this.maxAmmo;
this.firingDelay = FiringRate;
this.reloadSpeed = ReloadSpeed;
this.consumesXAmmoPerShot = ConsumesXAmmo;
}
/// <summary>
/// Update the gun's logic.
/// </summary>
/// <param name="time"></param>
public virtual void update(GameTime time)
{
this.remainingFiringDelay -= time.ElapsedGameTime.Milliseconds;
if (this.remainingFiringDelay <= 0) this.remainingFiringDelay = 0;
if (this.isReloading)
{
this.timeRemainingUntilReload -= time.ElapsedGameTime.TotalMilliseconds;
ModCore.log("Reloding: " + this.timeRemainingUntilReload);
if (this.timeRemainingUntilReload <= 0)
{
this.reload();
}
}
}
/// <summary>
/// Draw the gun to the screen.
/// </summary>
/// <param name="b"></param>
public virtual void draw(SpriteBatch b)
{
this.draw(b, this.Position, 4f);
}
/// <summary>
/// Draw the gun to the screen.
/// </summary>
/// <param name="b"></param>
/// <param name="Position"></param>
/// <param name="Scale"></param>
public virtual void draw(SpriteBatch b, Vector2 Position,float Scale)
{
this.sprite.draw(b, Position, Scale, 0f);
}
/// <summary>
/// What happens when the gun shoots.
/// </summary>
/// <param name="Position"></param>
/// <param name="Direction"></param>
public virtual void shoot(Vector2 Position, Vector2 Direction)
{
if (this.hasAmmo == false)
{
this.startReload();
return;
}
if (this.canShoot())
{
this.isReloading = false;
this._projectile.spawnClone(Position, Direction);
this.remainingFiringDelay = this.firingDelay;
this.consumeAmmo();
}
}
/// <summary>
/// What happens when the player starts the reload sequence.
/// </summary>
public virtual void startReload()
{
//Maybe play a sound effect?
this.isReloading = true;
}
/// <summary>
/// Checks if the gun can shoot. If out of ammo it starts the reload sequence. If it can shoot it will shoot.
/// </summary>
/// <param name="Position"></param>
/// <param name="Direction"></param>
public virtual void tryToShoot(Vector2 Position, Vector2 Direction)
{
if (this.hasAmmo == false)
{
this.startReload();
return;
}
if (this.canShoot())
{
this.shoot(Position, Direction);
}
}
/// <summary>
/// What happens when the gun consumes ammo.
/// </summary>
public virtual void consumeAmmo()
{
if (this.remainingAmmo == SSCGun.infiniteAmmo)
{
return;
}
else
{
this.remainingAmmo -= this.consumesXAmmoPerShot;
}
}
/// <summary>
/// What happens when the gun reloads. Can do either reloading a single bullet or the whole clip.
/// </summary>
public virtual void reload()
{
this.remainingAmmo = this.maxAmmo;
this.timeRemainingUntilReload = this.reloadSpeed;
if(this.remainingAmmo== this.maxAmmo)
{
this.isReloading = false;
}
//Maybe play a sound here????
}
public virtual bool canShoot()
{
return this.hasAmmo && this.remainingFiringDelay <= 0 && this.isReloading == false; //Could remove the isReloding condition and do guns like revolvers.
}
}
}

View File

@ -36,6 +36,8 @@ namespace Revitalize.Framework.Minigame.SeasideScrambleMinigame
public bool showMouseCursor;
public int maxMouseSleepTime = 300;
public SSCGuns.SSCGun gun;
public SSCPlayer(SSCEnums.PlayerID PlayerID)
@ -126,7 +128,7 @@ namespace Revitalize.Framework.Minigame.SeasideScrambleMinigame
}
this.mouseSensitivity = new Vector2(3f, 3f);
this.gun=new SSCGuns.SSCGun(new StardustCore.Animations.AnimatedSprite("MyFirstGun",this.position,new AnimationManager(SeasideScramble.self.textureUtils.getExtendedTexture("Guns","BasicGun"),new Animation(0,0,16,16)),Color.White), SeasideScramble.self.projectiles.getDefaultProjectile(this, this.position, Vector2.Zero, 1f, new Rectangle(0, 0, 16, 16), Color.White, 4f, 300),10,1000,3000);
}
/// <summary>
@ -165,6 +167,7 @@ namespace Revitalize.Framework.Minigame.SeasideScrambleMinigame
public void draw(SpriteBatch b, Vector2 position)
{
this.characterSpriteController.draw(b, SeasideScramble.GlobalToLocal(SeasideScramble.self.camera.viewport, position), this.playerColor, 4f, this.flipSprite == true ? SpriteEffects.FlipHorizontally : SpriteEffects.None, Math.Max(0f, (this.position.Y) / 10000f));
this.gun.draw(b, SeasideScramble.GlobalToLocal(SeasideScramble.self.camera.viewport, position),2f);
}
public void drawMouse(SpriteBatch b)
{
@ -232,6 +235,8 @@ namespace Revitalize.Framework.Minigame.SeasideScrambleMinigame
this.showMouseCursor = true;
}
}
this.gun.update(Time);
}
/// <summary>
@ -280,7 +285,7 @@ namespace Revitalize.Framework.Minigame.SeasideScrambleMinigame
if (state.ThumbSticks.Right.X != 0 || state.ThumbSticks.Right.Y != 0)
{
this.shoot(state.ThumbSticks.Right);
this.shoot(new Vector2(state.ThumbSticks.Right.X,state.ThumbSticks.Right.Y*-1));
//this.moveMouseCursor(state.ThumbSticks.Right);
}
}
@ -461,7 +466,10 @@ namespace Revitalize.Framework.Minigame.SeasideScrambleMinigame
{
if (SeasideScramble.self.menuManager.isMenuUp) return;
//ModCore.log("Shoot: " + direction);
SeasideScramble.self.projectiles.spawnDefaultProjectile(this, this.position, direction, 1f, new Rectangle(0, 0, 16, 16), Color.White, 4f, 300);
//SeasideScramble.self.projectiles.spawnDefaultProjectile(this, this.position, direction, 1f, new Rectangle(0, 0, 16, 16), Color.White, 4f, 300);
this.gun.tryToShoot(this.position, direction);
}
}

View File

@ -16,6 +16,7 @@ namespace Revitalize.Framework.Minigame.SeasideScrambleMinigame.SSCProjectiles
public float speed;
public float scale;
public Rectangle hitBox;
public int damage;
public Vector2 position
{
get
@ -59,7 +60,7 @@ namespace Revitalize.Framework.Minigame.SeasideScrambleMinigame.SSCProjectiles
{
}
public SSCProjectile(object Owner,AnimatedSprite Sprite,Rectangle HitBox ,Vector2 Position,Vector2 Direction, float Speed, int LifeSpan ,float Scale)
public SSCProjectile(object Owner,AnimatedSprite Sprite,Rectangle HitBox ,Vector2 Position,Vector2 Direction, float Speed, int LifeSpan ,float Scale,int damage)
{
this.sprite = Sprite;
this.hitBox = HitBox;
@ -70,6 +71,7 @@ namespace Revitalize.Framework.Minigame.SeasideScrambleMinigame.SSCProjectiles
this.maxLifeSpan = LifeSpan;
this.currentLifeSpan = LifeSpan;
this.owner = Owner;
this.damage = damage;
}
/// <summary>
@ -88,6 +90,8 @@ namespace Revitalize.Framework.Minigame.SeasideScrambleMinigame.SSCProjectiles
public virtual void updateMovement()
{
this.position += this.Velocity;
this.hitBox.X += (int)this.Velocity.X;
this.hitBox.Y += (int)this.Velocity.Y;
}
/// <summary>
@ -170,5 +174,16 @@ namespace Revitalize.Framework.Minigame.SeasideScrambleMinigame.SSCProjectiles
return this.owner != null;
}
/// <summary>
/// Spawns a clone at the given position with the given direction.
/// </summary>
/// <param name="position"></param>
/// <param name="direction"></param>
public virtual void spawnClone(Vector2 position,Vector2 direction)
{
//AnimatedSprite newSprite = new AnimatedSprite(this.sprite.name, position, new AnimationManager(this.sprite.animation.objectTexture.Copy(), this.sprite.animation.defaultDrawFrame), this.color);
SSCProjectile basic = new SSCProjectile(this.owner, new AnimatedSprite("DefaultProjectile", position, new AnimationManager(SeasideScramble.self.textureUtils.getExtendedTexture("Projectiles", "Basic"), new Animation(0, 0, 4, 4)), this.color), new Rectangle(this.hitBox.X,this.hitBox.Y,this.hitBox.Width,this.hitBox.Height), position, direction, this.speed, this.maxLifeSpan, this.scale,this.damage);
SeasideScramble.self.projectiles.addProjectile(basic);
}
}
}

View File

@ -69,9 +69,15 @@ namespace Revitalize.Framework.Minigame.SeasideScrambleMinigame.SSCProjectiles
public void spawnDefaultProjectile(object Owner,Vector2 Position,Vector2 Direction,float Speed,Rectangle HitBox,Color Color,float Scale,int LifeSpan=300)
{
SSCProjectile basic = new SSCProjectile(Owner, new StardustCore.Animations.AnimatedSprite("DefaultProjectile", Position, new StardustCore.Animations.AnimationManager(SeasideScramble.self.textureUtils.getExtendedTexture("Projectiles", "Basic"), new StardustCore.Animations.Animation(0, 0, 4, 4)), Color), HitBox, Position, Direction, Speed, LifeSpan, Scale);
SSCProjectile basic = new SSCProjectile(Owner, new StardustCore.Animations.AnimatedSprite("DefaultProjectile", Position, new StardustCore.Animations.AnimationManager(SeasideScramble.self.textureUtils.getExtendedTexture("Projectiles", "Basic"), new StardustCore.Animations.Animation(0, 0, 4, 4)), Color), HitBox, Position, Direction, Speed, LifeSpan, Scale,1);
this.addProjectile(basic);
}
public SSCProjectile getDefaultProjectile(object Owner, Vector2 Position, Vector2 Direction, float Speed, Rectangle HitBox, Color Color, float Scale, int LifeSpan = 300)
{
SSCProjectile basic = new SSCProjectile(Owner, new StardustCore.Animations.AnimatedSprite("DefaultProjectile", Position, new StardustCore.Animations.AnimationManager(SeasideScramble.self.textureUtils.getExtendedTexture("Projectiles", "Basic"), new StardustCore.Animations.Animation(0, 0, 4, 4)), Color), HitBox, Position, Direction, Speed, LifeSpan, Scale, 1);
return basic;
}
#endregion
}

View File

@ -99,11 +99,14 @@ namespace Revitalize.Framework.Minigame.SeasideScrambleMinigame
UIManager.searchForTextures(ModCore.ModHelper, ModCore.Manifest, Path.Combine("Content", "Minigames", "SeasideScramble", "Graphics", "UI"));
TextureManager projectileManager = new TextureManager("Projectiles");
projectileManager.searchForTextures(ModCore.ModHelper, ModCore.Manifest, Path.Combine("Content", "Minigames", "SeasideScramble", "Graphics", "Projectiles"));
TextureManager gunManager = new TextureManager("Guns");
gunManager.searchForTextures(ModCore.ModHelper, ModCore.Manifest, Path.Combine("Content", "Minigames", "SeasideScramble", "Graphics", "Guns"));
this.textureUtils.addTextureManager(playerManager);
this.textureUtils.addTextureManager(mapTextureManager);
this.textureUtils.addTextureManager(UIManager);
this.textureUtils.addTextureManager(projectileManager);
this.textureUtils.addTextureManager(gunManager);
}
private void LoadMaps()

View File

@ -69,6 +69,7 @@
<Compile Include="Framework\Minigame\SeasideScrambleMinigame\SeasideScrambleMap.cs" />
<Compile Include="Framework\Minigame\SeasideScrambleMinigame\SSCCamera.cs" />
<Compile Include="Framework\Minigame\SeasideScrambleMinigame\SSCEnums.cs" />
<Compile Include="Framework\Minigame\SeasideScrambleMinigame\SSCGuns\SSCGun.cs" />
<Compile Include="Framework\Minigame\SeasideScrambleMinigame\SSCMenus\CharacterSelectScreen.cs" />
<Compile Include="Framework\Minigame\SeasideScrambleMinigame\SSCMenus\SSCMenuManager.cs" />
<Compile Include="Framework\Minigame\SeasideScrambleMinigame\SSCMenus\TitleScreen.cs" />
@ -133,6 +134,9 @@
<Content Include="Content\Graphics\Furniture\Tables\Oak Table.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Content\Minigames\SeasideScramble\Graphics\Guns\BasicGun.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Content\Minigames\SeasideScramble\Graphics\Player\Junimo.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
@ -179,8 +183,6 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Folder Include="Framework\Minigame\SeasideScrambleMinigame\SSCGuns\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>