public void Update(float delta) { Velocity = Velocity + Vector2.down * delta * 0.001f; var move = Box.Move(delta * Velocity + Box.Bounds.Position, Response.Slide); // Testing if on ground if (move.Hits.Any((c) => (c.Normal.Y < 0))) { Velocity = Velocity * new Vector2(1, -1); } // Testing if on wall if (move.Hits.Any((c) => (Math.Abs(c.Normal.X) > Constants.Threshold))) { Velocity = Velocity * new Vector2(-1, 1); } }
private void UpdatePlayer(Box player, float delta, Keys left, Keys up, Keys right, Keys down) { _velocity.Y += delta * 0.001f; _velocity.X = 0; var k = Keyboard.GetState(); if (k.IsKeyDown(right)) { _velocity.X += 0.1f; } if (k.IsKeyDown(left)) { _velocity.X -= 0.1f; } if (_state.IsKeyUp(up) && k.IsKeyDown(up)) { _velocity.Y -= 0.5f; } if (_onPlatform) { _velocity += _platformVelocity; } if (_timeInRed > 0) { _velocity.Y *= 0.75f; } // Moving player var move = player.Move(player.Bounds.Position + delta * _velocity, (collision) => { if (collision.Other.HasTag(Tags.Group3)) { return(Response.Cross(collision)); } return(Response.Slide(collision)); }); // Testing if on moving platform _onPlatform = move.Hits.Any((c) => c.Box.HasTag(Tags.Group4)); // Testing if on ground if (move.Hits.Any((c) => c.Box.HasTag(Tags.Group4, Tags.Group2, Tags.Group5) && (c.Normal.Y < 0))) { _velocity.Y = 0; } var pushedCrateCollision = move.Hits.FirstOrDefault((c) => c.Box.HasTag(Tags.Group5)); if (pushedCrateCollision.IsHit) { var pushedCrate = pushedCrateCollision.Box.Data as Crate; var n = pushedCrateCollision.Normal; pushedCrate.Velocity = new Vector2(n.X * n.X, n.Y * n.Y) * _velocity; } // Testing if in red water if (move.Hits.Any((c) => c.Box.HasTag(Tags.Group3))) { _timeInRed += delta; if (_timeInRed > 3000) { SpawnPlayer(); } } else { _timeInRed = 0; } player.Data = _velocity; _state = k; }
public Particle(Box box) { Box = box; Velocity = new Vector2((float)Random.NextDouble() * 0.1f, 0); }
public void Update(GameTime time) { var state = Keyboard.GetState(); if (_previous.IsKeyUp(Keys.N) && state.IsKeyDown(Keys.N)) { _moveDestination = !_moveDestination; _selected.Position = (_moveDestination ? _goal.Position : _origin.Position) - _selected.Size / 2; } if (_previous.IsKeyUp(Keys.R) && state.IsKeyDown(Keys.R)) { _response = (_response + 1) % _values.Length; } _previous = state; _isMoving = state.IsKeyDown(Keys.Space); var m = Mouse.GetState().Position; m.Y = _height - m.Y; var pos = new Vector2(m.X, m.Y); var size = _isMoving ? 18 : 6; _cursor = new Rect(m.X - size / 2, m.Y - size / 2, size, size); if (_isMoving) { _selected.Position = pos - _selected.Size / 2; if (_moveDestination) { _goal.Position = pos; } else { _origin.Position = pos; } } // Calculate collision var hit = Hit.Resolve(_origin, _goal, _other); var r = _values[_response]; if (hit.IsHit && r != CollisionResponses.None) { _collision = new Rect(hit.Position, _origin.Size); _normal = new Rect(_collision.Center + hit.Normal * 50, new Vector2(5, 5)); // Destination var collisionPoint = new Collision() { Origin = _origin, Goal = _goal, Hit = hit, }; switch (r) { case CollisionResponses.None: break; case CollisionResponses.Touch: _destination = Response.Touch(collisionPoint).Value; break; case CollisionResponses.Cross: _destination = Response.Cross(collisionPoint).Value; break; case CollisionResponses.Slide: _destination = Response.Slide(collisionPoint).Value; break; case CollisionResponses.Bounce: _destination = Response.Bounce(collisionPoint).Value; break; default: throw new ArgumentOutOfRangeException(); } } else { _collision = new Rect(); _normal = new Rect(); _destination = _goal; } }