public override List <Object> update(GameTime gameTime) { vel = p.getCenter() - getCenter(); vel.Normalize(); if (fire_cooldown < 0) { translate(vel * 125 * (float)gameTime.ElapsedGameTime.TotalSeconds); } rotation = (float)(Math.Atan2(vel.Y, vel.X) + Math.PI / 2); List <Object> bullets = new List <Object>(); if ((p.getCenter() - getCenter()).Length() < 66 && fire_cooldown < 0) { float b_rot = rotation - (float)Math.PI / 2; Vector2 bullet_dir = (new Vector2((float)Math.Cos(b_rot), (float)Math.Sin(b_rot))); Bullet b = new Bullet(bullet_dir * 100, getCenter() + bullet_dir * tex.Width, false); b.rotation = b_rot; bullets.Add(b); fire_cooldown = 5; playPunch = true; } fire_cooldown -= gameTime.ElapsedGameTime.TotalSeconds; return(bullets); }
public Vector2 getOffset() { Vector2 center = p.getCenter(); Vector2 origin = center - new Vector2(1280 / 2 * zoomout, 720 / 2 * zoomout); origin.X = MathHelper.Clamp(origin.X, 0, bounds.X - 1280 * zoomout); origin.Y = MathHelper.Clamp(origin.Y, 0, bounds.Y - 720 * zoomout); return(origin); }
protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } if (gamestate == 0) { if ((GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed || Mouse.GetState().LeftButton == ButtonState.Pressed) && !prevA) { gamestate = 1; } prevA = GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed || Mouse.GetState().LeftButton == ButtonState.Pressed; return; } if (gamestate == 1) { if ((GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed || Mouse.GetState().LeftButton == ButtonState.Pressed) && !prevA) { gamestate = 2; loadLevel(); startTime = (float)gameTime.TotalGameTime.TotalSeconds; } prevA = GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed || Mouse.GetState().LeftButton == ButtonState.Pressed; return; } String alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if (gamestate == 3) { if ((GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y > .75 || Keyboard.GetState().IsKeyDown(Keys.Down)) && cooldown > 0.1) { //dec 1 int k = (alph.IndexOf(currchar) - 1) % 26; if (k == -1) { k = 25; } currchar = alph.ToCharArray()[k]; cooldown = 0; } else if ((GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y < -.75 || Keyboard.GetState().IsKeyDown(Keys.Up)) && cooldown > 0.1) { //inc 1 currchar = alph.ToCharArray()[(alph.IndexOf(currchar) + 1) % 26]; cooldown = 0; } if ((GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed || Mouse.GetState().LeftButton == ButtonState.Pressed) && !prevA && name.Length < 8) { name = name + currchar; currchar = 'A'; } if ((GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.X)) && !prevB && name.Length > 0) { currchar = name.ToCharArray().Last(); name = name.Substring(0, name.Length - 1); } if ((GamePad.GetState(PlayerIndex.One).Buttons.Start == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Space)) && prevStart == false) { objs.Clear(); StreamReader SR; SR = File.OpenText("score.txt"); for (int i = 0; i < 5; i++) { names.Add(SR.ReadLine()); } for (int i = 0; i < 5; i++) { scores.Add(Int32.Parse(SR.ReadLine())); } SR.Close(); int a = 0; while (Score.score < scores[a]) { a++; if (a == scores.Count) { break; } } scores.Insert(a, Score.score); names.Insert(a, name); StreamWriter SW = new StreamWriter("score.txt"); for (int i = 0; i < 5; i++) { SW.WriteLine(names[i]); } for (int i = 0; i < 5; i++) { SW.WriteLine(scores[i]); } SW.Close(); gamestate = 4; prevStart = GamePad.GetState(PlayerIndex.One).Buttons.Start == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Space); name = ""; Score.reset(); return; } //fixed chars //if ((int)currchar == 41) currchar = (char) 42; //else if ((int)currchar == 68) currchar = (char) 67; cooldown += (float)gameTime.ElapsedGameTime.TotalSeconds; prevA = GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed || Mouse.GetState().LeftButton == ButtonState.Pressed; prevB = GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.X); prevStart = GamePad.GetState(PlayerIndex.One).Buttons.Start == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Space); return; } else if (gamestate == 4) { if ((GamePad.GetState(PlayerIndex.One).Buttons.Start == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Space)) && !prevStart) { objs.Clear(); gamestate = 0; prevA = GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed || Mouse.GetState().LeftButton == ButtonState.Pressed; } prevStart = GamePad.GetState(PlayerIndex.One).Buttons.Start == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Space); return; } prevStart = GamePad.GetState(PlayerIndex.One).Buttons.Start == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Space); if ((GamePad.GetState(PlayerIndex.One).Buttons.Start == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Space))) { gamestate = 0; prevA = GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed || Mouse.GetState().LeftButton == ButtonState.Pressed; return; } if ((GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed || Mouse.GetState().RightButton == ButtonState.Pressed) && !prevA && player.isAlive()) { loadLevel(); startTime = (float)gameTime.TotalGameTime.TotalSeconds; } if ((GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed || Mouse.GetState().LeftButton == ButtonState.Pressed) && !prevA && !player.isAlive()) { gamestate = 3; } prevA = GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed || Mouse.GetState().LeftButton == ButtonState.Pressed; if (GamePad.GetState(PlayerIndex.One).Buttons.Y == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.C)) { Camera.zoomout = Math.Min(Camera.zoomout * 1.01F, 1.2F); } if (GamePad.GetState(PlayerIndex.One).Buttons.X == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.V)) { Camera.zoomout = Math.Max(0.5F, Camera.zoomout / 1.01F); } if (Camera.zoomout > 1) { Camera.zoomout *= 0.999F; } player.vel = gameTime.ElapsedGameTime.Ticks * GamePad.GetState(PlayerIndex.One).ThumbSticks.Left; if (true) //!GamePad.GetState(PlayerIndex.One).IsConnected) { int y_val = (Keyboard.GetState().IsKeyDown(Keys.Up) ? 1 : 0) - (Keyboard.GetState().IsKeyDown(Keys.Down) ? 1 : 0); int x_val = (Keyboard.GetState().IsKeyDown(Keys.Right) ? 1 : 0) - (Keyboard.GetState().IsKeyDown(Keys.Left) ? 1 : 0); player.vel = gameTime.ElapsedGameTime.Ticks * new Vector2(x_val, y_val); } Vector2 stick = GamePad.GetState(PlayerIndex.One).ThumbSticks.Right; if (stick != new Vector2(0, 0)) { player.rotation = -(float)(Math.Atan2(GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y, GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.X) - Math.PI / 2); } else { Vector2 p_center = (player.getCenter() - c.getOffset()) / c.getzoomout(); float y_val = Mouse.GetState().Y - p_center.Y; float x_val = Mouse.GetState().X - p_center.X; player.rotation = (float)(Math.Atan2(y_val, x_val) + Math.PI / 2); } if (GamePad.GetState(PlayerIndex.One).Triggers.Right > 0.1 || Mouse.GetState().LeftButton == ButtonState.Pressed) { player.fire(); } else { player.release(); } // Move the sprite around. UpdateSprites(gameTime); base.Update(gameTime); }
void UpdateSprites(GameTime gameTime) { List <Object> newObjs = new List <Object>(); if (r.Next(100) == 1 && (objs.Count - objectCount) < 60) { newObjs.Add(createEnemy(r.Next(2900), r.Next(2900), r.Next(10))); } // Move the sprite by speed, scaled by elapsed time. for (int i = 0; i < objs.Count; i++) { newObjs.AddRange(objs[i].update(gameTime)); if (objs[i] is Player) { d = 0.8F; } else { d = 1 / (float)Math.Pow((player.getCenter() - objs[i].getCenter()).Length(), 0.33); } if (objs[i].playFire) { if (objs[i] is Player) { lasersound.Play(0.8F, 0, 0); } else { shotsound.Play(d, 0, 0); } objs[i].playFire = false; } if (objs[i].playHurt) { hurtsound.Play(d, 0, 0); objs[i].playHurt = false; } if (objs[i].playPunch) { if (objs[i] is Player) { d = 0.8F; } else { d = 1 / (float)Math.Pow((player.getCenter() - objs[i].getCenter()).Length(), 0.33); } punchsound.Play(0.6F, 0, 0); objs[i].playPunch = false; } } for (int i = 0; i < objs.Count; i++) { if (!objs[i].canMove()) { continue; } for (int j = 0; j < objs.Count; j++) { if (i == j) { continue; } objs[i].collide(objs[j]); } } objs.RemoveAll(o => !o.isAlive()); objs.AddRange(newObjs); }