/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { var shippos = _playership.Getpos(); //camera behaviour when changing rooms foreach (var room in _roomlist) { if (room.Bounds.Contains((int) shippos.X, (int) shippos.Y)) { if (room == _currentroom) { break; } _lastroom01 = _currentroom; _currentroom = room; _camerachanging = true; } } if (_camerachanging == true) { _camera.Update(camerachange(new Vector2(_currentroom.Bounds.Left, _currentroom.Bounds.Top), (int)gameTime.ElapsedGameTime.TotalMilliseconds)); } //camera behaviour when in "big room" if (_currentroom.RoomType =="big") { if (_camerachanging != true) { float xpos; float ypos; if (shippos.X > _currentroom._position.X + Settings.Screensize.X / 2 && shippos.X < _currentroom._position.X +_currentroom.Bounds.Width - Settings.Screensize.X/2) { xpos = -shippos.X + Settings.Screensize.X/2; } else { xpos = _camera._position.X; } if (shippos.Y > _currentroom._position.Y + Settings.Screensize.Y / 2 && shippos.Y < _currentroom._position.Y + _currentroom.Bounds.Height - Settings.Screensize.Y/2) { ypos = -shippos.Y + Settings.Screensize.Y/2; } else { ypos = _camera._position.Y; } _camera.Update(new Vector2(xpos,ypos)); } } // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); var mousestate = Mouse.GetState(); _cursor.changepos(mousestate.X, mousestate.Y); //keyboard input var keyboardState = Keyboard.GetState(); if (keyboardState.IsKeyDown(Keys.A)) { _playership.ChangeVelocity(-1, 0); _playership.thrustoff(2); } else if (keyboardState.IsKeyDown(Keys.D)) { _playership.ChangeVelocity(1, 0); _playership.thrustoff(0); } if (keyboardState.IsKeyDown(Keys.W)) { _playership.ChangeVelocity(0, -1); _playership.thrustoff(1); } else if (keyboardState.IsKeyDown(Keys.S)) { _playership.ChangeVelocity(0, 1); _playership.thrustoff(3); } if (keyboardState.IsKeyDown(Keys.Escape)) this.Exit(); if (keyboardState.IsKeyUp(Keys.A)){_playership.thrustoff(0);} if (keyboardState.IsKeyUp(Keys.D)) { _playership.thrustoff(2); } if (keyboardState.IsKeyUp(Keys.W)) { _playership.thrustoff(3); } if (keyboardState.IsKeyUp(Keys.S)) { _playership.thrustoff(1); } if (keyboardState.IsKeyDown(Keys.E) && _previousKeyboardState.IsKeyDown(Keys.E) == false) Settings.Debugmode = !Settings.Debugmode; _previousKeyboardState = keyboardState; //Update line between turret and reticle _fireline = _cursor.Getpos()-_camera._position - shippos; _fireline.Normalize(); _playership.Turretrotate(_fireline); _timeSinceFired += (int) gameTime.ElapsedGameTime.TotalMilliseconds; //fire projectile if left mouse button is pressed if (_timeSinceFired > 1000/_firerate) if (mousestate.LeftButton == ButtonState.Pressed) { _shootSound.Play(); int _weaponlenght = 12; var _projectile = new Projectile(this, spriteBatch, shippos -new Vector2(5,5)+_fireline*_weaponlenght, _fireline,_playership.Getspeed()); Components.Add(_projectile); _projectileList.Add(_projectile); _playership.Recoil(-_fireline*PhysicsConstants.Recoil); _timeSinceFired = 0; } //remove projectiles that have gone out of bounds /* foreach (var projectile in _projectileList) { if (projectile._position.X > _viewport.X+_viewport.Width || projectile._position.X < _viewport.X || projectile._position.Y > _viewport.Y + _viewport.Height || projectile._position.Y < _viewport.Y) { _projectileRemoveList.Add(projectile); } } */ foreach (Door01 door in Lists.Doorlist) { foreach (Rectangle box in door.Returnbox()) { foreach (var projectile in _projectileList) { if (box.Intersects(projectile._boundingBox)) { _impactSound.Play(); _projectileRemoveList.Add(projectile); door.IsShot(); } } } } foreach (Rectangle block in _currentroom._solids) { foreach (var projectile in _projectileList) { //projectile.Update(gameTime); if (block.Intersects(projectile._boundingBox)) { _impactSound.Play(); _projectileRemoveList.Add(projectile); } //_removeProjectiles(); } } _removeProjectiles(); _projectileRemoveList = new List<Projectile>(); //collision between blocks and player ship _playership.Update(gameTime); //if (_playership.Colliding == false) if (_playership.TimeSinceCol > 100.0d) { List<Rectangle> collidedrects = new List<Rectangle>(); foreach (Rectangle block in _currentroom._solids) { if (block.Intersects(_playership._boundingBox)) { /* if (_playership.Getspeed() < 0.5f) { _playership.Stop(); break; } */ //Rectangle _intersect = Rectangle.Intersect(block, _playership._boundingBox); collidedrects.Add(block); //_playership.Collide(_intersect); //break; } } //If the door closes on the player it auto opens againp foreach (Door01 door in Lists.Doorlist) { foreach (Rectangle box in door.Returnbox()) { if (box.Intersects(_playership._boundingBox)) { collidedrects.Add(box); if (door._status == "closing") { door.open(); } } } } if (collidedrects.Count > 0) { _playership.Collide(collidedrects); } /* else if (collidedrects.Count > 1) { var largestRect = collidedrects.Aggregate((r1, r2) => (r1.Height * r1.Width) > (r2.Height * r2.Width) ? r1 : r2); _playership.Collide(largestRect); } */ } bool notcolliding = true; foreach (Rectangle block in _currentroom._solids) { if (block.Intersects(_playership._boundingBox)) notcolliding = false; } if (notcolliding == true) _playership.NotNear(); base.Update(gameTime); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { this.Components.Add(this.krypton); _camera = new Camera(new Viewport(0, 0, Settings.Screensize.Y, Settings.Screensize.X)); _particleRenderer = new SpriteBatchRenderer { GraphicsDeviceService = graphics }; _particleRenderer.LoadContent(Content); spriteBatch = new SpriteBatch(GraphicsDevice); Services.AddService(typeof(SpriteBatch), spriteBatch); Services.AddService(typeof(SpriteBatchRenderer), _particleRenderer); Services.AddService(typeof(Camera), _camera); Services.AddService(typeof(GameComponentCollection),Components); Services.AddService(typeof(KryptonEngine), krypton); //var _block = new SmallBlock01(this, spriteBatch, new Vector2(0, 0)); _roomlist.Add(new Room01(this, new Point(0, 0), 0)); _roomlist.Add(new Room01(this,new Point(1,0),1)); _roomlist.Add(new Room01(this, new Point(0,1), 2)); _lastroom01 = new Room01(this, new Point(1, 1), 3); _roomlist.Add(_lastroom01); _playership = new PlayerShip(this); Components.Add(_playership); _cursor = new Cursor(this, spriteBatch); Components.Add(_cursor); this.krypton.Initialize(); base.Initialize(); }