From 4ac1473d9ec9372602c1f006151b40b5e5d1c463 Mon Sep 17 00:00:00 2001 From: JoshuaNavarro Date: Sat, 20 Jul 2019 18:24:47 -0700 Subject: [PATCH] Finished making a basic gun and attaching it to the player! --- .../Graphics/Guns/BasicGun.png | Bin 0 -> 226 bytes .../SeasideScrambleMinigame/SSCGuns/SSCGun.cs | 250 ++++++++++++++++++ .../SeasideScrambleMinigame/SSCPlayer.cs | 14 +- .../SSCProjectiles/SSCProjectile.cs | 17 +- .../SSCProjectiles/SSCProjectileManager.cs | 8 +- .../SeasideScramble.cs | 3 + GeneralMods/Revitalize/Revitalize.csproj | 8 +- 7 files changed, 292 insertions(+), 8 deletions(-) create mode 100644 GeneralMods/Revitalize/Content/Minigames/SeasideScramble/Graphics/Guns/BasicGun.png create mode 100644 GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SSCGuns/SSCGun.cs diff --git a/GeneralMods/Revitalize/Content/Minigames/SeasideScramble/Graphics/Guns/BasicGun.png b/GeneralMods/Revitalize/Content/Minigames/SeasideScramble/Graphics/Guns/BasicGun.png new file mode 100644 index 0000000000000000000000000000000000000000..afb0a0745eb0c025712833afaa03a3944f1379fb GIT binary patch literal 226 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`D?MEtLo9le6C_xB4BT4QeEToj zzuw60MAm=x+YD?#@b!a0+nY5yO;XGKW*;jkek#?%$O8nTR!9Hew5^Z;flZq?J?QEv zbL^U7b2!mGxNW}mJ_DN{JDAsH9DTgHC!*BYz`#H;J1rq0VaLuQUgzUej%+XjW=XM3tVrmU5S11e0g2>sVAQoBNL#^+axk+a%485k + /// A simple gun class for shooting projectiles. Doesn't necessarily *have* to be a gun. Could be a slingshot or anything really. + /// + public class SSCGun + { + /// + /// Constant that represents the placeholding number for infinite ammo useage. + /// + public const int infiniteAmmo = -1; + + /// + /// The projectile this gun uses. + /// + private SSCProjectile _projectile; + /// + /// The sprite for the gun. + /// + public AnimatedSprite sprite; + + /// + /// The ammo remaining in the gun. + /// + public int remainingAmmo; + /// + /// The max ammo this gun can hold. + /// + public int maxAmmo; + /// + /// Whether or not this gun has ammo. + /// + public bool hasAmmo + { + get + { + return this.remainingAmmo > 0 || this.remainingAmmo == SSCGun.infiniteAmmo; + } + } + /// + /// How many bullets per shot this gun consumes. + /// + public int consumesXAmmoPerShot; + + /// + /// The time in milliseconds it takes to reload a single bullet. + /// + public double reloadSpeed; + /// + /// The time remaining to reload the gun. + /// + public double timeRemainingUntilReload; + + /// + /// Checks if the player is reloading the gun. + /// + public bool isReloading; + + + /// + /// Delay between shots in milliseconds. + /// + public double firingDelay; + /// + /// Remaining milliseconds until gun can fire again. + /// + public double remainingFiringDelay; + + /// + /// A reference to the projectile this gun uses. + /// + public SSCProjectile Projectile + { + get + { + return this._projectile; + } + } + /// + /// The positon of the gun. + /// + public Vector2 Position + { + get + { + return this.sprite.position; + } + set + { + this.sprite.position = value; + } + } + + /// + /// Constructor. + /// + /// The sprite for the gun. + /// The projectile the gun uses. + /// The max ammo this gun has. + /// The delay between this gun can fire. + /// The rate at which the gun reloads. + /// How many bullets per shot the gun consumes. + 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; + } + + /// + /// Update the gun's logic. + /// + /// + 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(); + } + } + } + + /// + /// Draw the gun to the screen. + /// + /// + public virtual void draw(SpriteBatch b) + { + this.draw(b, this.Position, 4f); + } + + /// + /// Draw the gun to the screen. + /// + /// + /// + /// + public virtual void draw(SpriteBatch b, Vector2 Position,float Scale) + { + this.sprite.draw(b, Position, Scale, 0f); + } + + /// + /// What happens when the gun shoots. + /// + /// + /// + 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(); + } + } + + /// + /// What happens when the player starts the reload sequence. + /// + public virtual void startReload() + { + //Maybe play a sound effect? + this.isReloading = true; + } + + /// + /// Checks if the gun can shoot. If out of ammo it starts the reload sequence. If it can shoot it will shoot. + /// + /// + /// + public virtual void tryToShoot(Vector2 Position, Vector2 Direction) + { + if (this.hasAmmo == false) + { + this.startReload(); + return; + } + if (this.canShoot()) + { + this.shoot(Position, Direction); + } + } + + + /// + /// What happens when the gun consumes ammo. + /// + public virtual void consumeAmmo() + { + if (this.remainingAmmo == SSCGun.infiniteAmmo) + { + return; + } + else + { + this.remainingAmmo -= this.consumesXAmmoPerShot; + } + } + + + /// + /// What happens when the gun reloads. Can do either reloading a single bullet or the whole clip. + /// + 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. + } + } +} diff --git a/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SSCPlayer.cs b/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SSCPlayer.cs index 8698ced2..a93c3ad2 100644 --- a/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SSCPlayer.cs +++ b/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SSCPlayer.cs @@ -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); } /// @@ -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); } /// @@ -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); + } } diff --git a/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SSCProjectiles/SSCProjectile.cs b/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SSCProjectiles/SSCProjectile.cs index ef1850dd..23c4dc30 100644 --- a/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SSCProjectiles/SSCProjectile.cs +++ b/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SSCProjectiles/SSCProjectile.cs @@ -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; } /// @@ -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; } /// @@ -170,5 +174,16 @@ namespace Revitalize.Framework.Minigame.SeasideScrambleMinigame.SSCProjectiles return this.owner != null; } + /// + /// Spawns a clone at the given position with the given direction. + /// + /// + /// + 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); + } } } diff --git a/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SSCProjectiles/SSCProjectileManager.cs b/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SSCProjectiles/SSCProjectileManager.cs index bc017948..f86de217 100644 --- a/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SSCProjectiles/SSCProjectileManager.cs +++ b/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SSCProjectiles/SSCProjectileManager.cs @@ -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 } diff --git a/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SeasideScramble.cs b/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SeasideScramble.cs index dadab9d9..fb7343ca 100644 --- a/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SeasideScramble.cs +++ b/GeneralMods/Revitalize/Framework/Minigame/SeasideScrambleMinigame/SeasideScramble.cs @@ -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() diff --git a/GeneralMods/Revitalize/Revitalize.csproj b/GeneralMods/Revitalize/Revitalize.csproj index 33b76cde..7ca71b3e 100644 --- a/GeneralMods/Revitalize/Revitalize.csproj +++ b/GeneralMods/Revitalize/Revitalize.csproj @@ -69,6 +69,7 @@ + @@ -133,6 +134,9 @@ PreserveNewest + + Always + Always @@ -179,8 +183,6 @@ Always - - - + \ No newline at end of file