public Camera(GameObject obj) : base(obj) { Height = GameObject.Scene.Device.Viewport.Height; Width = GameObject.Scene.Device.Viewport.Width; _viewport = new RectangleF(GameObject.Transform.Position.X, GameObject.Transform.Position.X, Width, Height); // center this around the camera _center = new Vector2(Width / 2, Height / 2); Transform.Position = new Vector2(Width / 2, Height / 2); }
public static RectangleF Overlap(RectangleF one, RectangleF two) { if(Intersects(one, two)) { float x11 = one.Position.X; float x12 = one.Position.X + one.Dimensions.X; float y11 = one.Position.Y; float y12 = one.Position.Y + one.Dimensions.Y; float x21 = two.Position.X; float y21 = two.Position.Y; float x22 = two.Position.X + two.Dimensions.X; float y22 = two.Position.Y + two.Dimensions.Y; float xOverlap = Math.Max(0, Math.Min(x12, x22) - Math.Max(x11, x21)); float yOverlap = Math.Max(0, Math.Min(y12, y22) - Math.Max(y11, y21)); return new RectangleF(Vector2.Zero, new Vector2(xOverlap, yOverlap)); } return Empty; }
public override void Update(GameTime time) { if (Scene.CurrentState == GameState.End) { if(_model.UseAnimations) _model.PlayAnimation(AnimationSequence.Idle, true); return; } // apply gravity here if (UseGravity) { //check to see if we can fall RectangleF rect = new RectangleF((int)Transform.Position.X, (int)Transform.Position.Y + 1, Box.Width, Box.Height); RigidBody collider = null; if (Scene.CheckForCollisions(this, rect, out collider).IsEmpty) { // apply gravity Acceleration += Scene.GravityDirection * (Scene.Gravity * (float)time.ElapsedGameTime.TotalSeconds); } else { // TODO: This is so very wrong, but it introduces a fun double jump that I want to keep if (!Colliding && collider != null) { Colliding = true; GameObject.OnCollision(collider); } // I.E. Falling if(Acceleration.Y > 0 || Velocity.Y > 0) { Acceleration = new Vector2(Acceleration.X, 0); Velocity = new Vector2(Velocity.X, 0); } } } //clamp the max Acceleration = Vector2.Min(Acceleration, MaxVelocity); //check to see if we need to apply drag if (!Velocity.CloseToZero() && HasDrag) { Vector2 otherWay = Vector2.Normalize(Velocity) * -1 * Drag; //see if this even works Velocity += otherWay; if(_model.UseAnimations && _model.PlayingAnimation == AnimationSequence.Idle) _model.PlayAnimation(AnimationSequence.Run, true, 0.05f); } else { // only idle if we are running if (_model.UseAnimations && _model.PlayingAnimation == AnimationSequence.Run) { _model.PlayAnimation(AnimationSequence.Idle, true); Velocity = Vector2.Zero; } } _model.Effects = Velocity.X >= 0 ? SpriteEffects.None : SpriteEffects.FlipHorizontally; Velocity = Vector2.Min(Velocity, MaxVelocity); // do the rigid stuff Velocity += Acceleration * (float)time.ElapsedGameTime.TotalSeconds; Transform.Position += Velocity * (float)time.ElapsedGameTime.TotalSeconds; }
public bool Insersects(RectangleF two) { return Intersects(this, two); }
public static bool Intersects(RectangleF one, RectangleF two) { return one.Position.X < two.Position.X + two.Dimensions.X && one.Position.X + one.Dimensions.X > two.Position.X && one.Position.Y < two.Position.Y + two.Dimensions.Y && one.Position.Y + one.Dimensions.Y > two.Position.Y; }
public void Draw(SpriteBatch b, Texture2D tex, Transform t, RectangleF rect, float layer) { Vector2 actualPos = DrawPosition(t); rect.Position = actualPos; b.Draw(tex, rect.ToRectangle(), null, Color.White, t.Rotation, Vector2.Zero, SpriteEffects.None, layer); }
private void ParseJSON() { if (_configData == null) return; Type t = this.GetType(); foreach (PropertyInfo info in t.GetProperties()) { object[] attr = info.GetCustomAttributes(true); foreach (var custom in attr) { JSONKey jsonKey = custom as JSONKey; if (jsonKey != null) { //lets get the key matching string key = string.IsNullOrEmpty(jsonKey.JSONName) ? info.Name.ToLower() : jsonKey.JSONName; if (_configData[key] != null) { if (info.PropertyType == typeof(string)) { info.SetValue(this, _configData[key].ToString(), null); } else if (info.PropertyType == typeof(int)) { info.SetValue(this, _configData[key].AsInt, null); } else if (info.PropertyType == typeof(bool)) { info.SetValue(this, _configData[key].AsBool, null); } else if (info.PropertyType == typeof(float)) { info.SetValue(this, _configData[key].AsFloat, null); } else if (info.PropertyType == typeof(double)) { info.SetValue(this, _configData[key].AsDouble, null); } else if(info.PropertyType == typeof(RectangleF)) { RectangleF tmp = new RectangleF(); tmp.Position.X = _configData["x"].AsInt; tmp.Position.Y = _configData["y"].AsInt; tmp.Dimensions.X = _configData["width"].AsInt; tmp.Dimensions.Y = _configData["height"].AsInt; info.SetValue(this, tmp, null); } } //we have an attribute that is matching in json if (jsonKey is Required && info.GetValue(this, null) == null) { //uh oh, we have to make sure this has some value! throw new RequiredJSONAttributeMissing(key, string.Format("JSON is missing for the key {0}", key)); } } } } }
public RectangleF CheckForCollisions(RigidBody col, RectangleF rect, out RigidBody x) { foreach (GameObject o in _sceneObjects) { RigidBody collider = o.GetComponent<RigidBody>(); if (collider != null) { if (collider != col) { RectangleF result = RectangleF.Overlap(rect, collider.Box); if(!result.IsEmpty) { x = collider; return result; } } } } x = null; return RectangleF.Empty; }