public Booster(Vector2 position, float angle, float power) { texture = Game1.boosterTex; HitBox = new Rectangle((int)position.X, (int)position.Y, 32, 32); this.angle = angle; accel = new Vector2((float)(power * Math.Cos(angle)), (float)(power * Math.Sin(angle))); }
private ZipLine zippingLine; // Current zipline being used #endregion Fields #region Constructors public Runner(Vector2 position, bool freeroam) { // Set up animations, like a baller normal = Game1.guyNormal; running = Game1.guyRunning; midair = Game1.guyMidair; sliding = Game1.guySliding; ziplining = Game1.guyZiplining; deadGround = Game1.guyDeadGround; deadMidair = Game1.guyDeadMidair; movedLeft = true; imageAngle = 0.0f; current = normal; // Set up character physics this.position = position; velocity = new Vector2(); isTouchingGround = false; isSliding = false; isZipping = false; staySliding = false; controllable = !freeroam; jumppresscheck = false; wallpresscheck = false; prevPlatSpeed = 0; // Set up hit boxes this.hitBox = new Rectangle((int)position.X + 16, (int)position.Y, 32, 64); this.groundHitBox = new Rectangle((int)position.X + 18, (int)position.Y + 64, 28, 2); this.leftWallBox = new Rectangle((int)position.X + 14, (int)position.Y + 2, 2, 60); this.rightWallBox = new Rectangle((int)position.X + 48, (int)position.Y + 2, 2, 60); this.ziplineBox = new Rectangle((int)position.X + 24, (int)position.Y - 16, 8, 32); // Set up other variables health = 10; healthTracker = 0; }
public void Update() { // Update animation current.Update(); // Update health regen if (health < 10 && health > 0) { healthTracker++; if (healthTracker >= HEALTHINTERVAL) { healthTracker = 0; health++; } } else healthTracker = 0; if (health > 10) health = 10; // If something is slowing you down, stop it from speeding you up in the opposite direction if (Math.Sign(velocity.X + acceleration.X) != Math.Sign(velocity.X) && velocity.X != 0) { acceleration.X = 0; velocity.X = 0; } else { // Cap speed if (Math.Abs(velocity.X + acceleration.X) < 8.0f || velocity.X * acceleration.X < 0) velocity.X += acceleration.X; } // Apply accel to velocity and velocity to position velocity.Y += acceleration.Y; position += velocity; if (platform != null) { // Fix retarded platform switching direction and making you spazz out glitch if (!(Math.Sign(platform.velocity.Y) != Math.Sign(prevPlatSpeed) && prevPlatSpeed > 0)) position += platform.velocity; prevPlatSpeed = platform.velocity.Y; } UpdateHitBox(); // Stay inside screen if (hitBox.Left < 0) { velocity.X = 0; position.X = -16; UpdateHitBox(); } else if (hitBox.Right > Game1.currentRoom.roomWidth) { velocity.X = 0; position.X = Game1.currentRoom.roomWidth - 48; UpdateHitBox(); } // Die if character falls below room bounds if (hitBox.Top > Game1.currentRoom.roomHeight) health = 0; // Reset flags isTouchingGround = false; isTouchingWall = false; bool isOnPlatform = false; // Iterate through walls foreach (Wall w in Game1.currentRoom.Walls) { // Make sure wall effects player if (w is PlatformWall || (!w.Bounds.Intersects(Game1.currentRoom.ViewBox) && !Game1.currentRoom.Freeroam)) continue; // If you're standing on it, apply ground friction and say that you're standing if (isSliding && !(w is DeathWall)) { if (w.Bounds.Intersects(groundHitBox)) { isZipping = false; isTouchingGround = true; if (w is FloatingPlatform) { if (platform == null) { platform = (FloatingPlatform)w; if (Game1.currentRoom.recorder.playing ? !Game1.currentRoom.recorder.keystates[Settings.controls["MoveLeft"]] && !Game1.currentRoom.recorder.keystates[Settings.controls["MoveRight"]] : !Keyboard.GetState().IsKeyDown(Settings.controls["MoveLeft"]) && !Keyboard.GetState().IsKeyDown(Settings.controls["MoveRight"])) velocity.X = 0; } isOnPlatform = true; } } else if (w.Bounds.Intersects(leftWallBox) && !(w is Mirror)) { canWallToRight = true; isTouchingWall = true; } else if (w.Bounds.Intersects(rightWallBox) && !(w is Mirror)) { canWallToLeft = true; isTouchingWall = true; } } // Apply other wall collisions if (this.hitBox.Intersects(w.Bounds)) { List<Vector2> resolutions = new List<Vector2>(); // Resolve Y if (!(this.hitBox.Bottom <= w.Bounds.Top || this.hitBox.Top >= w.Bounds.Bottom)) { if (this.hitBox.Top < w.Bounds.Bottom) resolutions.Add(new Vector2(0, w.Bounds.Bottom - this.hitBox.Top)); if (this.hitBox.Bottom > w.Bounds.Top) resolutions.Add(new Vector2(0, w.Bounds.Top - this.hitBox.Bottom)); } // Resolve X if (!(this.hitBox.Right <= w.Bounds.Left || this.hitBox.Left >= w.Bounds.Right)) { if (this.hitBox.Right > w.Bounds.Left) resolutions.Add(new Vector2(w.Bounds.Left - this.hitBox.Right, 0)); if (this.hitBox.Left < w.Bounds.Right) resolutions.Add(new Vector2(w.Bounds.Right - this.hitBox.Left, 0)); } // Find smallest overlap while (resolutions.Count > 1) { if (resolutions[0].Length() > resolutions[1].Length()) resolutions.RemoveAt(0); else resolutions.RemoveAt(1); } // Set new velocity and position Vector2 resV = resolutions[0]; if (resV.X != 0) { if (resV.X > 0 && velocity.X < 0 || resV.X < 0 && velocity.X > 0) { if (w is DeathWall && health > 0) { Game1.damage.Play(0.5f * Settings.soundVol, 0f, 0f); this.velocity.X = Math.Sign(velocity.X) * -6; health -= 9; } else this.velocity.X = 0; } } else { if (resV.Y > 0 && velocity.Y < 0 || resV.Y < 0 && velocity.Y > 0) { if (w is DeathWall && health > 0) { Game1.damage.Play(0.5f * Settings.soundVol, 0f, 0f); this.velocity.Y = Math.Sign(velocity.Y) * -6; health -= 9; } else { if (Math.Abs(this.velocity.Y) >= 3) Game1.collide.Play(0.3f * Settings.soundVol, 0f, 0f); this.velocity.Y = 0; } } } this.position += resV; UpdateHitBox(); } // If you're standing on it, apply ground friction and say that you're standing if (!isSliding) { if (w.Bounds.Intersects(groundHitBox)) { isZipping = false; isTouchingGround = true; if (w is FloatingPlatform) { if (platform == null) { platform = (FloatingPlatform)w; if (Game1.currentRoom.recorder.playing ? !Game1.currentRoom.recorder.keystates[Settings.controls["MoveLeft"]] && !Game1.currentRoom.recorder.keystates[Settings.controls["MoveRight"]] : !Keyboard.GetState().IsKeyDown(Settings.controls["MoveLeft"]) && !Keyboard.GetState().IsKeyDown(Settings.controls["MoveRight"])) velocity.X = 0; } isOnPlatform = true; } groundFriction.X = Math.Sign(velocity.X) * -1 * 0.5f; } else if (w.Bounds.Intersects(leftWallBox) && !(w is Mirror)) { canWallToRight = true; isTouchingWall = true; } else if (w.Bounds.Intersects(rightWallBox) && !(w is Mirror)) { canWallToLeft = true; isTouchingWall = true; } } } // Land on boxes if (velocity.Y > 0 && !isTouchingGround) { foreach (Box b in Game1.currentRoom.Boxes) { if (groundHitBox.Intersects(b.hitBox) && groundHitBox.Y < b.hitBox.Y + 12) { position.Y = b.position.Y - 64; velocity.Y = 0; if (b.platform != null) { if (platform == null) { platform = b.platform; if (Game1.currentRoom.recorder.playing ? !Game1.currentRoom.recorder.keystates[Settings.controls["MoveLeft"]] && !Game1.currentRoom.recorder.keystates[Settings.controls["MoveRight"]] : !Keyboard.GetState().IsKeyDown(Settings.controls["MoveLeft"]) && !Keyboard.GetState().IsKeyDown(Settings.controls["MoveRight"])) velocity.X = 0; } isOnPlatform = true; } if (!isSliding) groundFriction.X = Math.Sign(velocity.X) * -1 * 0.5f; else groundFriction.X = 0; isTouchingGround = true; } } } // Check for crushing bool iscrushed = false; foreach (Wall w in Game1.currentRoom.Walls) { if (!(w is PlatformWall) && this.hitBox.Intersects(w.Bounds)) { if (!(isSliding || staySliding)) { staySliding = true; break; } crushCount++; if (crushCount >= 3) health = 0; iscrushed = true; break; } } if (!iscrushed) crushCount = 0; if (health <= 0) controllable = false; // Apply platform velocity when leaving platform if (!isOnPlatform && platform != null) { velocity += platform.velocity; platform = null; } // Apply booster acceleration foreach (Booster b in Game1.currentRoom.Boosters) if (b.HitBox.Intersects(this.hitBox)) { Game1.boost.Play(0.5f * Settings.soundVol, 0f, 0f); velocity += b.Acceleration; } // Check for rocket collision foreach (RocketLauncher r in Game1.currentRoom.Launchers) if (r.rocket.hitBox.Intersects(this.hitBox)) { this.health -= 6; this.velocity += r.rocket.velocity * 1.5f; } // Check for fire collision foreach (Flamethrower f in Game1.currentRoom.Flamethrowers) { bool brk = false; foreach (Flamethrower.Flame fl in f.flames) { if (!fl.drawBox.Intersects(Game1.currentRoom.ViewBox)) continue; if (fl.drawBox.Intersects(this.hitBox)) { health -= 0.6f; velocity += fl.velocity * 0.06f; brk = true; break; } } if (brk) break; } // Check if level finish reached if (Game1.currentRoom.Finish != null && !Game1.currentRoom.Finished) if (this.hitBox.Intersects(Game1.currentRoom.Finish.HitBox)) { Game1.finish.Play(Settings.soundVol, 0f, 0f); Game1.currentRoom.Finished = true; } // Remove ground friction if midair if (!isTouchingGround) { Game1.run.Stop(); if (health <= 0) current = deadMidair; else current = midair; groundFriction = Vector2.Zero; } else { // Change to normal animation if standing still if (health <= 0) current = deadGround; else if (velocity.X == 0) current = normal; } // Reset space key if (Game1.currentRoom.recorder.playing ? !Game1.currentRoom.recorder.keystates[Settings.controls["Jump"]] : !Keyboard.GetState().IsKeyDown(Settings.controls["Jump"])) { if (heldBox != null) jumppresscheck = false; if (isTouchingGround) jumppresscheck = false; else if (canWallToLeft || canWallToRight) wallpresscheck = false; } if ((Game1.currentRoom.recorder.playing ? Game1.currentRoom.recorder.keystates[Settings.controls["Jump"]] : Keyboard.GetState().IsKeyDown(Settings.controls["Jump"])) && controllable) { // Jumping if (isTouchingGround && !staySliding && !jumppresscheck) { Game1.jump.Play(Settings.soundVol, 0f, 0f); velocity.Y = -10.0f; current = midair; isTouchingGround = false; jumppresscheck = true; wallpresscheck = true; } // Wall jumping else if (!wallpresscheck && !isTouchingGround && isTouchingWall) { Game1.jump.Play(Settings.soundVol, 0f, 0f); velocity.X = canWallToRight ? 8 : -8; movedLeft = !movedLeft; if (velocity.Y > -7.5f) velocity.Y = -7.5f; wallpresscheck = true; jumppresscheck = true; canWallToRight = false; canWallToLeft = false; } // Box jumping else if (heldBox != null && !isTouchingGround && !jumppresscheck) { Game1.jump.Play(Settings.soundVol, 0f, 0f); velocity.Y = -10.0f; current = midair; jumppresscheck = true; wallpresscheck = true; heldBox.velocity.Y = 4; heldBox.velocity.X = this.velocity.X; heldBox.grabbed = false; heldBox = null; } } // If ziplining, update according to zipline's values if (zippingLine != null) { current = ziplining; position.Y = zippingLine.GetY(ziplineBox.Center.X); acceleration = zippingLine.Acceleration * (Math.Abs(zippingLine.Slope) > 1.5f ? 1 : 3); velocity = zippingLine.GetNewVelocity(velocity); movedLeft = velocity.X < 0; } // Check for whether or not ziplining if control is being held if ((Game1.currentRoom.recorder.playing ? Game1.currentRoom.recorder.keystates[Settings.controls["Slide"]] : Keyboard.GetState().IsKeyDown(Settings.controls["Slide"])) && controllable && !isTouchingGround) { bool zipping = false; foreach (ZipLine z in Game1.currentRoom.ZipLines) { if ((z.pos1.X < z.pos2.X ? (ziplineBox.Left >= z.pos1.X && ziplineBox.X <= z.pos2.X) : (ziplineBox.Left <= z.pos1.X && ziplineBox.X >= z.pos2.X)) && ziplineBox.Contains(ziplineBox.Center.X, (int)z.GetY(ziplineBox.Center.X))) { zipping = true; isZipping = true; if (zippingLine == null) { zippingLine = z; current = ziplining; position.Y = z.GetY(ziplineBox.Center.X); acceleration = z.Acceleration * 3; velocity = z.GetNewVelocity(velocity); movedLeft = velocity.X < 0; } break; } } // If not hitting a zipline, turn off zipling variables if (!zipping) { zippingLine = null; isZipping = false; acceleration.Y = 0.4f; } } else { zippingLine = null; isZipping = false; acceleration.Y = 0.4f; } // Check for picking up boxes if (Game1.currentRoom.recorder.playing ? Game1.currentRoom.recorder.keystates[Settings.controls["Box"]] : Keyboard.GetState().IsKeyDown(Settings.controls["Box"])) { if (isTouchingGround && heldBox == null) foreach (Box b in Game1.currentRoom.Boxes) { if (b.hitBox.Intersects(rightWallBox) && !movedLeft || b.hitBox.Intersects(leftWallBox) && movedLeft) { b.grabbed = true; heldBox = b; break; } } } else { if (heldBox != null) { heldBox.grabbed = false; heldBox.velocity = this.velocity; heldBox = null; } } // Slide when holding control isSliding = ((Game1.currentRoom.recorder.playing ? Game1.currentRoom.recorder.keystates[Settings.controls["Slide"]] : Keyboard.GetState().IsKeyDown(Settings.controls["Slide"])) || staySliding) && controllable && isTouchingGround; // Don't get up if sliding under low ceiling bool flag = true; if (isSliding || staySliding) { foreach (Wall w in Game1.currentRoom.Walls) { if (w.Bounds.Left < hitBox.Right && w.Bounds.Right > hitBox.Left && w.Bounds.Bottom + 32 > hitBox.Top && w.Bounds.Top < hitBox.Top) { staySliding = true; flag = false; break; } } } if (flag) staySliding = false; // Move right if ((Game1.currentRoom.recorder.playing ? Game1.currentRoom.recorder.keystates[Settings.controls["MoveRight"]] : Keyboard.GetState().IsKeyDown(Settings.controls["MoveRight"])) && !isSliding && !isZipping && controllable) { canWallToRight = false; acceleration.X = isTouchingGround ? 1.0f : 0.5f; movedLeft = false; if (isTouchingGround) current = running; if (Game1.currentRoom.recorder.playing ? Game1.currentRoom.recorder.keystates[Settings.controls["MoveLeft"]] : Keyboard.GetState().IsKeyDown(Settings.controls["MoveLeft"])) { acceleration.X = 0.0f; movedLeft = true; current = normal; } } // Move left else if ((Game1.currentRoom.recorder.playing ? Game1.currentRoom.recorder.keystates[Settings.controls["MoveLeft"]] : Keyboard.GetState().IsKeyDown(Settings.controls["MoveLeft"])) && !isSliding && !isZipping && controllable) { canWallToLeft = false; acceleration.X = isTouchingGround ? -1.0f : -0.5f; movedLeft = true; if (isTouchingGround) current = running; } // Slow down if no keys are being held else acceleration.X = 0.0f; if (Math.Abs(velocity.X) > 8.0f && !isSliding && isTouchingGround) { if (acceleration.X < 0 && velocity.X < 0) velocity.X++; else if (acceleration.X > 0 && velocity.X > 0) velocity.X--; } // Set sliding animation if sliding if (isSliding && isTouchingGround) { canWallToLeft = false; canWallToRight = false; acceleration.X = 0.0f; groundFriction.X = 0.0f; current = sliding; } // Apply ground friction to horizontal acceleration acceleration.X += groundFriction.X; // Update midair animation based on vertical speed int midairFrame = (int)velocity.Y * 2 + 3; if (midairFrame < 0) midairFrame = 0; if (midairFrame > 8) midairFrame = 8; midair.Frame = midairFrame; // Play sounds if (current == running) { Game1.slide.Stop(); if (Game1.run.State != SoundState.Playing) Game1.run.Play(); } else if (current == ziplining || current == sliding) { Game1.run.Stop(); if (Game1.slide.State != SoundState.Playing && velocity.X != 0) Game1.slide.Play(); } else { Game1.run.Stop(); Game1.slide.Stop(); } }
protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); finishTex = Content.Load<Texture2D>("finish"); poleTex = Content.Load<Texture2D>("pole"); lineTex = Content.Load<Texture2D>("pixel"); Texture2D[] images = new Texture2D[3]; for (int i = 1; i <= images.Length; i++) images[i - 1] = Content.Load<Texture2D>("booster000" + i.ToString()); boosterTex = new AnimatedTexture(images, 600, true, false); platformTex = Content.Load<Texture2D>("floating platform"); messageTex = Content.Load<Texture2D>("message"); boxTex = Content.Load<Texture2D>("box"); explosionTex = Content.Load<Texture2D>("explosion particle"); rocketTex = Content.Load<Texture2D>("rocket"); launcherTex = Content.Load<Texture2D>("launcher"); flamethrowerTex = Content.Load<Texture2D>("flamethrower"); tileSet = new Texture2D[5]; deathWallSet = new Texture2D[5]; backgrounds = new Texture2D[5]; tileSet[0] = Content.Load<Texture2D>("tiles/tilegrass"); tileSet[1] = Content.Load<Texture2D>("tiles/tilelava"); tileSet[2] = Content.Load<Texture2D>("tiles/tilenight"); tileSet[3] = Content.Load<Texture2D>("tiles/tilecave"); tileSet[4] = Content.Load<Texture2D>("tiles/tilefactory"); deathWallSet[0] = Content.Load<Texture2D>("tiles/deathgrass"); deathWallSet[1] = Content.Load<Texture2D>("tiles/deathlava"); deathWallSet[2] = Content.Load<Texture2D>("tiles/deathnight"); deathWallSet[3] = Content.Load<Texture2D>("tiles/deathcave"); deathWallSet[4] = Content.Load<Texture2D>("tiles/deathfactory"); mirrorTex = Content.Load<Texture2D>("tiles/mirror"); backgrounds[0] = Content.Load<Texture2D>("backgrounds/bggrass"); backgrounds[1] = Content.Load<Texture2D>("backgrounds/bglava"); backgrounds[2] = Content.Load<Texture2D>("backgrounds/bgnight"); backgrounds[3] = Content.Load<Texture2D>("backgrounds/bgcave"); backgrounds[4] = Content.Load<Texture2D>("backgrounds/bgfactory"); medalTex = Content.Load<Texture2D>("medal"); wallTex = Content.Load<Texture2D>("pixel"); skinPreviews = new Texture2D[6]; skinPreviews[0] = Content.Load<Texture2D>("skins/speed runner/speed runner normal0001"); skinPreviews[1] = Content.Load<Texture2D>("skins/blob/blob normal0001"); skinPreviews[2] = Content.Load<Texture2D>("skins/stick figure/stick figure normal0001"); skinPreviews[3] = Content.Load<Texture2D>("skins/mr guy/mr guy normal0001"); skinPreviews[4] = Content.Load<Texture2D>("skins/mario/mario normal"); skinPreviews[5] = Content.Load<Texture2D>("skins/ninja/ninja normal0001"); prevLocked = Content.Load<Texture2D>("skins/locked"); run = Content.Load<SoundEffect>("sounds/run").CreateInstance(); slide = Content.Load<SoundEffect>("sounds/slide").CreateInstance(); jump = Content.Load<SoundEffect>("sounds/jump"); boost = Content.Load<SoundEffect>("sounds/boost"); collide = Content.Load<SoundEffect>("sounds/collision"); finish = Content.Load<SoundEffect>("sounds/finish"); damage = Content.Load<SoundEffect>("sounds/damage"); rocketLaunch = Content.Load<SoundEffect>("sounds/rocket launch"); explosion = Content.Load<SoundEffect>("sounds/explosion"); playingGrass = true; playingLava = false; playingNight = false; playingCave = false; playingFactory = false; grassMusic = Content.Load<Song>("music/grass"); lavaMusic = Content.Load<Song>("music/lava"); nightMusic = Content.Load<Song>("music/night"); caveMusic = Content.Load<Song>("music/cave"); factoryMusic = Content.Load<Song>("music/factory"); titlefont = Content.Load<SpriteFont>("titlefont"); msgfont = Content.Load<SpriteFont>("msgfont"); mnufont = Content.Load<SpriteFont>("mnufont"); currentRoom = new MainMenu(false); Settings.GetSettings(); LoadNewSkin(this, Settings.skin); MediaPlayer.IsRepeating = true; MediaPlayer.Play(grassMusic); }
public static void LoadNewSkin(Game game, string skinName) { selectedSkin = skinName; Settings.skin = skinName; skinManager.Dispose(); skinManager = new ContentManager(game.Services, "Content/skins"); Texture2D[] images; if (skinName == "mario") guyNormal = new AnimatedTexture(skinManager.Load<Texture2D>("mario/mario normal")); else { images = new Texture2D[19]; for (int i = 1; i <= images.Length; i++) images[i - 1] = skinManager.Load<Texture2D>(skinName + "/" + skinName + " normal00" + (i > 9 ? "" : "0") + i.ToString()); guyNormal = new AnimatedTexture(images, 3, true, false); } if (skinName == "mario") { images = new Texture2D[3]; for (int i = 1; i <= images.Length; i++) images[i - 1] = skinManager.Load<Texture2D>(skinName + "/" + skinName + " running000" + i.ToString()); guyRunning = new AnimatedTexture(images, 6, true, false); } else { images = new Texture2D[19]; for (int i = 1; i <= images.Length; i++) images[i - 1] = skinManager.Load<Texture2D>(skinName + "/" + skinName + " running00" + (i > 9 ? "" : "0") + i.ToString()); guyRunning = new AnimatedTexture(images, 1, true, false); } images = new Texture2D[9]; for (int i = 1; i <= images.Length; i++) images[i - 1] = skinManager.Load<Texture2D>(skinName + "/" + skinName + " midair000" + i.ToString()); guyMidair = new AnimatedTexture(images, 3, false, true); guyZiplining = new AnimatedTexture(skinManager.Load<Texture2D>(skinName + "/" + skinName + " ziplining")); guySliding = new AnimatedTexture(skinManager.Load<Texture2D>(skinName + "/" + skinName + " sliding")); guyDeadGround = new AnimatedTexture(skinManager.Load<Texture2D>(skinName + "/" + skinName + " dead")); if (skinName == "mario") guyDeadMidair = new AnimatedTexture(skinManager.Load<Texture2D>("mario/mario dead midair")); else { images = new Texture2D[25]; for (int i = 1; i <= images.Length; i++) images[i - 1] = skinManager.Load<Texture2D>(skinName + "/" + skinName + " dead midair00" + (i > 9 ? "" : "0") + i.ToString()); guyDeadMidair = new AnimatedTexture(images, 1, true, false); } }