public FlxRect copyTo(FlxRect Rect) { Rect.x = x; Rect.y = y; Rect.width = width; Rect.height = height; return(this); }
public FlxRect copyFrom(FlxRect Rect) { x = Rect.x; y = Rect.y; width = Rect.width; height = Rect.height; return(this); }
/** * Instantiate a new particle. Like <code>FlxSprite</code>, all meaningful creation * happens during <code>loadGraphic()</code> or <code>makeGraphic()</code> or whatever. */ public FlxParticle() : base() { lifespan = 0; friction = 500; rotateBy = 0; minSpeed = new FlxPoint(); maxSpeed = new FlxPoint(); particleRect = null; }
/// <summary> /// Set the boundaries of the camera. Useful for preventing the camera from scrolling past the edge of a map. /// </summary> /// <param name="X"></param> /// <param name="Y"></param> /// <param name="Width"></param> /// <param name="Height"></param> /// <param name="UpdateWorld"></param> public void setBounds(float X = 0, float Y = 0, float Width = 0, float Height = 0, Boolean UpdateWorld = false) { if (bounds == null) { bounds = new FlxRect(); } bounds.make(X, Y, Width, Height); if (UpdateWorld) { FlxG.worldBounds.copyFrom(bounds); } update(); }
public FlxSprite makeGraphic(float Width, float Height, Color color) { texture = new Texture2D(FlxG.graphicsDevice, (int)Width, (int)Height); Color[] colors = new Color[(int)Width * (int)Height]; for (int i = 0; i < colors.Length; i++) { colors[i] = new Color(color.ToVector3()); } texture.SetData(colors); color = color * alpha; sourceRect = new FlxRect(0, 0, Width, Height); resetHelpers(); frameWidth = width = Width; frameHeight = height = Height; return(this); }
/// <summary> /// Override to null out variables manually /// </summary> public override void destroy() { velocity = null; acceleration = null; drag = null; maxVelocity = null; scrollFactor = null; _point = null; _rect = null; last = null; //cameras = null; if (path != null) { path.destroy(); } path = null; }
public override void draw() { if (dirty) { calcFrame(); } if (cameras == null) { cameras = FlxG.cameras; } FlxCamera camera = FlxG.camera; int i = 0; int l = cameras.Count; while (i < l) { camera = cameras[i++]; //camera = FlxG.camera; if (!onScreen(camera)) { continue; } _point.x = x - (int)(camera.scroll.x * scrollFactor.x) - offset.x; _point.y = y - (int)(camera.scroll.y * scrollFactor.y) - offset.y; _point.x += (_point.x > 0) ? 0.0000001f : -0.0000001f; _point.y += (_point.y > 0) ? 0.0000001f : -0.0000001f; if (visible) { base.draw(); if (texture != null) { //if the sprite is animated then the sourceRect needs to be changed to be the correct frame if (_animated) { sourceRect = new FlxRect(frameWidth * _curIndex, 0, frameWidth, frameHeight); } Rectangle rect = new Rectangle((int)sourceRect.x, (int)sourceRect.y, (int)sourceRect.width, (int)sourceRect.height); FlxG.spriteBatch.Draw(texture, getVec2(), rect, _color * alpha, angle, new Vector2(), scale.getVec2(), SpriteEffects.None, 0f); } } } }
/// <summary> /// Instantiates a <code>FlxObject</code> /// </summary> /// <param name="X">X-coordinate of the object in space</param> /// <param name="Y">y-coordinate of the object in space</param> /// <param name="Width">Desired width of the rectangle</param> /// <param name="Height">Desired height of the rectangle</param> public FlxObject(float X = 0, float Y = 0, float Width = 0, float Height = 0) : base() { x = X; y = Y; last = new FlxPoint(x, y); width = Width; height = Height; mass = 1.0f; elasticity = 0.0f; health = 1; immovable = false; moves = true; touching = NONE; wasTouching = NONE; allowCollisions = ANY; velocity = new FlxPoint(); acceleration = new FlxPoint(); drag = new FlxPoint(); maxVelocity = new FlxPoint(10000, 10000); angle = 0; angularVelocity = 0; angularAcceleration = 0; angularDrag = 0; maxAngular = 10000; scrollFactor = new FlxPoint(1, 1); _flicker = false; _flickerTimer = 0; _point = new FlxPoint(); _rect = new FlxRect(); path = null; pathSpeed = 0; pathAngle = 0; }
public FlxCamera(float X, float Y, float Width, float Height, float Zoom = 1.0f) : base() { x = X; y = Y; width = Width; height = Height; defaultZoom = 1.0f; rotating = 0.0f; scroll = new FlxPoint(); _point = new FlxPoint(); target = null; deadzone = null; bounds = null; zooming = Zoom; _fxShakeIntensity = 0; _fxShakeDuration = 0; _fxShakeComplete = null; _fxShakeOffset = new FlxPoint(); _fxShakeDirection = 0; }
/** * Checks to see if some <code>FlxRect</code> object overlaps this <code>FlxRect</code> object. * * @param Rect The rectangle being tested. * * @return Whether or not the two rectangles overlap. */ public Boolean overlaps(FlxRect Rect) { return (Rect.x + Rect.width > x) && (Rect.x < x + width) && (Rect.y + Rect.height > y) && (Rect.y < y + height); }
public FlxRect copyTo(FlxRect Rect) { Rect.x = x; Rect.y = y; Rect.width = width; Rect.height = height; return this; }
public FlxRect copyFrom(FlxRect Rect) { x = Rect.x; y = Rect.y; width = Rect.width; height = Rect.height; return this; }
/// <summary> /// Initiate all the things needed by the engine /// </summary> internal static void init() { FlxG.defaultWholeScreenViewport = FlxG.viewport; FlxG.cameras = new List<FlxCamera>(); FlxG.viewports = new List<Viewport>(); //FlxG.width = FlxG.graphics.PreferredBackBufferWidth; //FlxG.height = FlxG.graphics.PreferredBackBufferHeight; FlxG.bgColor = Color.Black; FlxG.mute = false; FlxG.sounds = new FlxGroup(); FlxG.console = new FlxConsole(); FlxG.worldBounds = new FlxRect(); FlxG.defaultFont = FlxG.content.Load<SpriteFont>("ConsoleFont"); //FlxG.zoom = 1f; FlxG.rotation = 0f; FlxG.pad1 = new FlxGamepad(PlayerIndex.One); FlxG.pad2 = new FlxGamepad(PlayerIndex.Two); FlxG.pad3 = new FlxGamepad(PlayerIndex.Three); FlxG.pad4 = new FlxGamepad(PlayerIndex.Four); FlxG.keys = new FlxKeyboard(); FlxG.mouse = new FlxMouse(); FlxG.safeZone = new FlxRect(FlxG.graphicsDevice.Viewport.TitleSafeArea.X, FlxG.graphicsDevice.Viewport.TitleSafeArea.Y, FlxG.graphicsDevice.Viewport.TitleSafeArea.Width, FlxG.graphicsDevice.Viewport.TitleSafeArea.Height); FlxCamera defaultCam = new FlxCamera(0, 0, FlxG.width, FlxG.height, FlxG.zoom); FlxG.addCamera(defaultCam); //Thread tCreate = new Thread(FlxG.state.create); //tCreate.Start(); //create state last FlxG.state.create(); }
public override void draw() { if (dirty) calcFrame(); if (cameras == null) cameras = FlxG.cameras; FlxCamera camera = FlxG.camera; int i = 0; int l = cameras.Count; while (i < l) { camera = cameras[i++]; //camera = FlxG.camera; if (!onScreen(camera)) { continue; } _point.x = x - (int)(camera.scroll.x * scrollFactor.x) - offset.x; _point.y = y - (int)(camera.scroll.y * scrollFactor.y) - offset.y; _point.x += (_point.x > 0) ? 0.0000001f : -0.0000001f; _point.y += (_point.y > 0) ? 0.0000001f : -0.0000001f; if (visible) { base.draw(); if (texture != null) { //if the sprite is animated then the sourceRect needs to be changed to be the correct frame if (_animated) sourceRect = new FlxRect(frameWidth * _curIndex, 0, frameWidth, frameHeight); Rectangle rect = new Rectangle((int)sourceRect.x, (int)sourceRect.y, (int)sourceRect.width, (int)sourceRect.height); FlxG.spriteBatch.Draw(texture, getVec2(), rect, _color * alpha, angle, new Vector2(), scale.getVec2(), SpriteEffects.None, 0f); } } } }
/// <summary> /// Internal function used by FlxEmitter to create the particles /// </summary> /// <param name="Graphic"></param> /// <param name="Rotation"></param> /// <param name="Width"></param> /// <param name="Height"></param> /// <returns></returns> public FlxSprite loadParticleGraphic(Texture2D Graphic, float Rotation, float Width = 0, float Height = 0) { rotateBy = Rotation * 0.0174532925f; texture = Graphic; int numFrames = (int)FlxU.floor(texture.Width / texture.Height); if (Width == 0 || Height == 0) { Width = texture.Height; Height = Width; } frameWidth = width = Width; frameHeight = height = Height; List<FlxRect> _draws = new List<FlxRect>(); for (int i = 0; i < numFrames; i++) { FlxRect textureArea = new FlxRect(i * height, 0, width, height); _draws.Add(textureArea); } int di;// = (int)Math.Round(FlxU.randomBetween(0, numFrames)); Random r = new Random(); di = r.Next(numFrames); particleRect = _draws[di]; //FlxRect t = _draws[di]; sourceRect = new FlxRect(particleRect.x, particleRect.y, particleRect.width, particleRect.height); return this; }
/// <summary> /// Set the boundaries of the camera. Useful for preventing the camera from scrolling past the edge of a map. /// </summary> /// <param name="X"></param> /// <param name="Y"></param> /// <param name="Width"></param> /// <param name="Height"></param> /// <param name="UpdateWorld"></param> public void setBounds(float X = 0, float Y = 0, float Width = 0, float Height = 0, Boolean UpdateWorld = false) { if (bounds == null) bounds = new FlxRect(); bounds.make(X, Y, Width, Height); if (UpdateWorld) FlxG.worldBounds.copyFrom(bounds); update(); }
public FlxSprite makeGraphic(float Width, float Height, Color color) { texture = new Texture2D(FlxG.graphicsDevice, (int)Width, (int)Height); Color[] colors = new Color[(int)Width * (int)Height]; for (int i = 0; i < colors.Length; i++) { colors[i] = new Color(color.ToVector3()); } texture.SetData(colors); color = color * alpha; sourceRect = new FlxRect(0, 0, Width, Height); resetHelpers(); frameWidth = width = Width; frameHeight = height = Height; return this; }
/// <summary> /// Make the camera follow an object with a specified follow-style /// </summary> /// <param name="Target"></param> /// <param name="Style"></param> public void follow(FlxObject Target, uint Style=STYLE_LOCKON) { target = Target; float helper, w, h; switch (Style) { case STYLE_PLATFORMER: w = width / 8; h = height / 3; deadzone = new FlxRect((width - w) / 2, (height - h) / 2 - h * 0.25f, w, h); break; case STYLE_TOPDOWN: helper = FlxU.max(width, height) / 3; deadzone = new FlxRect((width - helper) / 3, (height - helper) / 3, helper, helper); break; case STYLE_TOPDOWN_TIGHT: helper = FlxU.max(width, height) / 12; deadzone = new FlxRect((width - helper) / 2, (height - helper) / 2, helper, helper); break; case STYLE_LOCKON: break; case STYLE_LOOSE: deadzone = new FlxRect(0, 0, width, height); break; default: deadzone = null; break; } }
/// <summary> /// Y-axis component of the object separation process /// </summary> /// <param name="Object1"></param> /// <param name="Object2"></param> /// <returns></returns> public static bool separateY(FlxObject Object1, FlxObject Object2) { //can't separate two immovable objects bool obj1immovable = Object1.immovable; bool obj2immovable = Object2.immovable; if(obj1immovable && obj2immovable) return false; ////If one of the objects is a tilemap, just pass it off. if (Object1 is FlxTilemap) return (Object1 as FlxTilemap).overlapsWithCallback(Object2, separateY); if (Object2 is FlxTilemap) return (Object2 as FlxTilemap).overlapsWithCallback(Object1, separateY, true); //First, get the two object deltas float overlap = 0; float obj1delta = Object1.y - Object1.last.y; float obj2delta = Object2.y - Object2.last.y; if(obj1delta != obj2delta) { //Check if the Y hulls actually overlap float obj1deltaAbs = (obj1delta > 0) ? obj1delta : -obj1delta; float obj2deltaAbs = (obj2delta > 0) ? obj2delta : -obj2delta; FlxRect obj1rect = new FlxRect(Object1.x,Object1.y-((obj1delta > 0)?obj1delta:0),Object1.width,Object1.height+obj1deltaAbs); FlxRect obj2rect = new FlxRect(Object2.x,Object2.y-((obj2delta > 0)?obj2delta:0),Object2.width,Object2.height+obj2deltaAbs); if((obj1rect.x + obj1rect.width > obj2rect.x) && (obj1rect.x < obj2rect.x + obj2rect.width) && (obj1rect.y + obj1rect.height > obj2rect.y) && (obj1rect.y < obj2rect.y + obj2rect.height)) { float maxOverlap = obj1deltaAbs + obj2deltaAbs + OVERLAP_BIAS; //If they did overlap (and can), figure out by how much and flip the corresponding flags if(obj1delta > obj2delta) { overlap = Object1.y + Object1.height - Object2.y; if((overlap > maxOverlap) || !Convert.ToBoolean(Object1.allowCollisions & DOWN) || !Convert.ToBoolean(Object2.allowCollisions & UP)) overlap = 0; else { Object1.touching |= DOWN; Object2.touching |= UP; } } else if(obj1delta < obj2delta) { overlap = Object1.y - Object2.height - Object2.y; if((-overlap > maxOverlap) || !Convert.ToBoolean(Object1.allowCollisions & UP) || !Convert.ToBoolean(Object2.allowCollisions & DOWN)) overlap = 0; else { Object1.touching |= UP; Object2.touching |= DOWN; } } } } //Then adjust their positions and velocities accordingly (if there was any overlap) if(overlap != 0) { float obj1v = Object1.velocity.y; float obj2v = Object2.velocity.y; if(!obj1immovable && !obj2immovable) { overlap *= 0.5f; Object1.y = Object1.y - overlap; Object2.y += overlap; float obj1velocity = (float)Math.Sqrt((obj2v * obj2v * Object2.mass) / Object1.mass) * ((obj2v > 0) ? 1f : -1f); float obj2velocity = (float)Math.Sqrt((obj1v * obj1v * Object1.mass) / Object2.mass) * ((obj1v > 0) ? 1f : -1f); float average = (obj1velocity + obj2velocity) * 0.5f; obj1velocity -= average; obj2velocity -= average; Object1.velocity.y = average + obj1velocity * Object1.elasticity; Object2.velocity.y = average + obj2velocity * Object2.elasticity; } else if(!obj1immovable) { Object1.y = Object1.y - overlap; Object1.velocity.y = obj2v - obj1v*Object1.elasticity; //This is special case code that handles cases like horizontal moving platforms you can ride if(Object2.active && Object2.moves && (obj1delta > obj2delta)) Object1.x += Object2.x - Object2.last.x; } else if(!obj2immovable) { Object2.y += overlap; Object2.velocity.y = obj1v - obj2v*Object2.elasticity; //This is special case code that handles cases like horizontal moving platforms you can ride if(Object1.active && Object1.moves && (obj1delta < obj2delta)) Object2.x += Object1.x - Object1.last.x; } return true; } else return false; }
/// <summary> /// Y-axis component of the object separation process /// </summary> /// <param name="Object1"></param> /// <param name="Object2"></param> /// <returns></returns> static public bool separateY(FlxObject Object1, FlxObject Object2) { //can't separate two immovable objects bool obj1immovable = Object1.immovable; bool obj2immovable = Object2.immovable; if (obj1immovable && obj2immovable) { return(false); } ////If one of the objects is a tilemap, just pass it off. if (Object1 is FlxTilemap) { return((Object1 as FlxTilemap).overlapsWithCallback(Object2, separateY)); } if (Object2 is FlxTilemap) { return((Object2 as FlxTilemap).overlapsWithCallback(Object1, separateY, true)); } //First, get the two object deltas float overlap = 0; float obj1delta = Object1.y - Object1.last.y; float obj2delta = Object2.y - Object2.last.y; if (obj1delta != obj2delta) { //Check if the Y hulls actually overlap float obj1deltaAbs = (obj1delta > 0) ? obj1delta : -obj1delta; float obj2deltaAbs = (obj2delta > 0) ? obj2delta : -obj2delta; FlxRect obj1rect = new FlxRect(Object1.x, Object1.y - ((obj1delta > 0)?obj1delta:0), Object1.width, Object1.height + obj1deltaAbs); FlxRect obj2rect = new FlxRect(Object2.x, Object2.y - ((obj2delta > 0)?obj2delta:0), Object2.width, Object2.height + obj2deltaAbs); if ((obj1rect.x + obj1rect.width > obj2rect.x) && (obj1rect.x < obj2rect.x + obj2rect.width) && (obj1rect.y + obj1rect.height > obj2rect.y) && (obj1rect.y < obj2rect.y + obj2rect.height)) { float maxOverlap = obj1deltaAbs + obj2deltaAbs + OVERLAP_BIAS; //If they did overlap (and can), figure out by how much and flip the corresponding flags if (obj1delta > obj2delta) { overlap = Object1.y + Object1.height - Object2.y; if ((overlap > maxOverlap) || !Convert.ToBoolean(Object1.allowCollisions & DOWN) || !Convert.ToBoolean(Object2.allowCollisions & UP)) { overlap = 0; } else { Object1.touching |= DOWN; Object2.touching |= UP; } } else if (obj1delta < obj2delta) { overlap = Object1.y - Object2.height - Object2.y; if ((-overlap > maxOverlap) || !Convert.ToBoolean(Object1.allowCollisions & UP) || !Convert.ToBoolean(Object2.allowCollisions & DOWN)) { overlap = 0; } else { Object1.touching |= UP; Object2.touching |= DOWN; } } } } //Then adjust their positions and velocities accordingly (if there was any overlap) if (overlap != 0) { float obj1v = Object1.velocity.y; float obj2v = Object2.velocity.y; if (!obj1immovable && !obj2immovable) { overlap *= 0.5f; Object1.y = Object1.y - overlap; Object2.y += overlap; float obj1velocity = (float)Math.Sqrt((obj2v * obj2v * Object2.mass) / Object1.mass) * ((obj2v > 0) ? 1f : -1f); float obj2velocity = (float)Math.Sqrt((obj1v * obj1v * Object1.mass) / Object2.mass) * ((obj1v > 0) ? 1f : -1f); float average = (obj1velocity + obj2velocity) * 0.5f; obj1velocity -= average; obj2velocity -= average; Object1.velocity.y = average + obj1velocity * Object1.elasticity; Object2.velocity.y = average + obj2velocity * Object2.elasticity; } else if (!obj1immovable) { Object1.y = Object1.y - overlap; Object1.velocity.y = obj2v - obj1v * Object1.elasticity; //This is special case code that handles cases like horizontal moving platforms you can ride if (Object2.active && Object2.moves && (obj1delta > obj2delta)) { Object1.x += Object2.x - Object2.last.x; } } else if (!obj2immovable) { Object2.y += overlap; Object2.velocity.y = obj1v - obj2v * Object2.elasticity; //This is special case code that handles cases like horizontal moving platforms you can ride if (Object1.active && Object1.moves && (obj1delta < obj2delta)) { Object2.x += Object1.x - Object1.last.x; } } return(true); } else { return(false); } }
/// <summary> /// X-axis component of the object separation process /// </summary> /// <param name="Object1"></param> /// <param name="Object2"></param> /// <returns></returns> static public bool separateX(FlxObject Object1, FlxObject Object2) { //can't separate two immovable objects bool obj1immovable = Object1.immovable; bool obj2immovable = Object2.immovable; if (obj1immovable && obj2immovable) { return(false); } //If one of the objects is a tilemap, just pass it off. if (Object1 is FlxTilemap) { return((Object1 as FlxTilemap).overlapsWithCallback(Object2, separateX)); } if (Object2 is FlxTilemap) { return((Object2 as FlxTilemap).overlapsWithCallback(Object1, separateX, true)); } //First, get the two object deltas float overlap = 0; float obj1delta = Object1.x - Object1.last.x; float obj2delta = Object2.x - Object2.last.x; if (obj1delta != obj2delta) { //Check if the X hulls actually overlap float obj1deltaAbs = (obj1delta > 0) ? obj1delta : -obj1delta; float obj2deltaAbs = (obj2delta > 0) ? obj2delta : -obj2delta; FlxRect obj1rect = new FlxRect(Object1.x - ((obj1delta > 0)?obj1delta:0), Object1.last.y, Object1.width + ((obj1delta > 0)?obj1delta:-obj1delta), Object1.height); FlxRect obj2rect = new FlxRect(Object2.x - ((obj2delta > 0)?obj2delta:0), Object2.last.y, Object2.width + ((obj2delta > 0)?obj2delta:-obj2delta), Object2.height); if ((obj1rect.x + obj1rect.width > obj2rect.x) && (obj1rect.x < obj2rect.x + obj2rect.width) && (obj1rect.y + obj1rect.height > obj2rect.y) && (obj1rect.y < obj2rect.y + obj2rect.height)) { float maxOverlap = obj1deltaAbs + obj2deltaAbs + OVERLAP_BIAS; //If they did overlap (and can), figure out by how much and flip the corresponding flags if (obj1delta > obj2delta) { overlap = Object1.x + Object1.width - Object2.x; if ((overlap > maxOverlap) || !Convert.ToBoolean(Object1.allowCollisions & RIGHT) || !Convert.ToBoolean(Object2.allowCollisions & LEFT)) { overlap = 0; } else { Object1.touching |= RIGHT; Object2.touching |= LEFT; } } else if (obj1delta < obj2delta) { overlap = Object1.x - Object2.width - Object2.x; if ((-overlap > maxOverlap) || !Convert.ToBoolean(Object1.allowCollisions & LEFT) || !Convert.ToBoolean(Object2.allowCollisions & RIGHT)) { overlap = 0; } else { Object1.touching |= LEFT; Object2.touching |= RIGHT; } } } } //Then adjust their positions and velocities accordingly (if there was any overlap) if (overlap != 0) { float obj1v = Object1.velocity.x; float obj2v = Object2.velocity.x; if (!obj1immovable && !obj2immovable) { overlap *= 0.5f; Object1.x = Object1.x - overlap; Object2.x += overlap; float obj1velocity = (float)Math.Sqrt((obj2v * obj2v * Object2.mass) / Object1.mass) * ((obj2v > 0) ? 1f : -1f); float obj2velocity = (float)Math.Sqrt((obj1v * obj1v * Object1.mass) / Object2.mass) * ((obj1v > 0) ? 1f : -1f); float average = (obj1velocity + obj2velocity) * 0.5f; obj1velocity -= average; obj2velocity -= average; Object1.velocity.x = average + obj1velocity * Object1.elasticity; Object2.velocity.x = average + obj2velocity * Object2.elasticity; } else if (!obj1immovable) { Object1.x = Object1.x - overlap; Object1.velocity.x = obj2v - obj1v * Object1.elasticity; } else if (!obj2immovable) { Object2.x += overlap; Object2.velocity.x = obj1v - obj2v * Object2.elasticity; } return(true); } else { return(false); } }
public FlxSprite loadGraphic(Texture2D Graphic, Boolean Animated = false, Boolean Reverse = false, float Width = 0, float Height = 0) { _animated = Animated; texture = Graphic; if (Width == 0) { if (Animated) Width = (uint)Graphic.Width; else Width = (uint)Graphic.Width; } frameWidth = width = Width; if (Height == 0) { if (Animated) Height = (uint)width; else Height = (uint)Graphic.Height; } frameHeight = height = Height; resetHelpers(); sourceRect = new FlxRect(0, 0, width, height); return this; }
/// <summary> /// X-axis component of the object separation process /// </summary> /// <param name="Object1"></param> /// <param name="Object2"></param> /// <returns></returns> public static bool separateX(FlxObject Object1, FlxObject Object2) { //can't separate two immovable objects bool obj1immovable = Object1.immovable; bool obj2immovable = Object2.immovable; if(obj1immovable && obj2immovable) return false; //If one of the objects is a tilemap, just pass it off. if (Object1 is FlxTilemap) return (Object1 as FlxTilemap).overlapsWithCallback(Object2, separateX); if (Object2 is FlxTilemap) return (Object2 as FlxTilemap).overlapsWithCallback(Object1, separateX, true); //First, get the two object deltas float overlap = 0; float obj1delta = Object1.x - Object1.last.x; float obj2delta = Object2.x - Object2.last.x; if(obj1delta != obj2delta) { //Check if the X hulls actually overlap float obj1deltaAbs = (obj1delta > 0) ? obj1delta : -obj1delta; float obj2deltaAbs = (obj2delta > 0) ? obj2delta : -obj2delta; FlxRect obj1rect = new FlxRect(Object1.x-((obj1delta > 0)?obj1delta:0),Object1.last.y,Object1.width+((obj1delta > 0)?obj1delta:-obj1delta),Object1.height); FlxRect obj2rect = new FlxRect(Object2.x-((obj2delta > 0)?obj2delta:0),Object2.last.y,Object2.width+((obj2delta > 0)?obj2delta:-obj2delta),Object2.height); if((obj1rect.x + obj1rect.width > obj2rect.x) && (obj1rect.x < obj2rect.x + obj2rect.width) && (obj1rect.y + obj1rect.height > obj2rect.y) && (obj1rect.y < obj2rect.y + obj2rect.height)) { float maxOverlap = obj1deltaAbs + obj2deltaAbs + OVERLAP_BIAS; //If they did overlap (and can), figure out by how much and flip the corresponding flags if(obj1delta > obj2delta) { overlap = Object1.x + Object1.width - Object2.x; if((overlap > maxOverlap) || !Convert.ToBoolean(Object1.allowCollisions & RIGHT) || !Convert.ToBoolean(Object2.allowCollisions & LEFT)) overlap = 0; else { Object1.touching |= RIGHT; Object2.touching |= LEFT; } } else if(obj1delta < obj2delta) { overlap = Object1.x - Object2.width - Object2.x; if((-overlap > maxOverlap) || !Convert.ToBoolean(Object1.allowCollisions & LEFT) || !Convert.ToBoolean(Object2.allowCollisions & RIGHT)) overlap = 0; else { Object1.touching |= LEFT; Object2.touching |= RIGHT; } } } } //Then adjust their positions and velocities accordingly (if there was any overlap) if(overlap != 0) { float obj1v = Object1.velocity.x; float obj2v = Object2.velocity.x; if(!obj1immovable && !obj2immovable) { overlap *= 0.5f; Object1.x = Object1.x - overlap; Object2.x += overlap; float obj1velocity = (float)Math.Sqrt((obj2v * obj2v * Object2.mass) / Object1.mass) * ((obj2v > 0) ? 1f : -1f); float obj2velocity = (float)Math.Sqrt((obj1v * obj1v * Object1.mass) / Object2.mass) * ((obj1v > 0) ? 1f : -1f); float average = (obj1velocity + obj2velocity) * 0.5f; obj1velocity -= average; obj2velocity -= average; Object1.velocity.x = average + obj1velocity * Object1.elasticity; Object2.velocity.x = average + obj2velocity * Object2.elasticity; } else if(!obj1immovable) { Object1.x = Object1.x - overlap; Object1.velocity.x = obj2v - obj1v*Object1.elasticity; } else if(!obj2immovable) { Object2.x += overlap; Object2.velocity.x = obj1v - obj2v*Object2.elasticity; } return true; } else return false; }
/** * Checks to see if some <code>FlxRect</code> object overlaps this <code>FlxRect</code> object. * * @param Rect The rectangle being tested. * * @return Whether or not the two rectangles overlap. */ public Boolean overlaps(FlxRect Rect) { return((Rect.x + Rect.width > x) && (Rect.x < x + width) && (Rect.y + Rect.height > y) && (Rect.y < y + height)); }
/// <summary> /// Override to null out variables manually /// </summary> public override void destroy() { velocity = null; acceleration = null; drag = null; maxVelocity = null; scrollFactor = null; _point = null; _rect = null; last = null; //cameras = null; if (path != null) path.destroy(); path = null; }
public override void draw() { if (dirty) calcFrame(); if (cameras == null) cameras = FlxG.cameras; FlxCamera camera = FlxG.camera; int i = 0; int l = cameras.Count; while (i < l) { camera = cameras[i++]; //camera = FlxG.camera; if (!onScreen(camera)) { continue; } _point.x = x - (int)(camera.scroll.x * scrollFactor.x) - offset.x; _point.y = y - (int)(camera.scroll.y * scrollFactor.y) - offset.y; _point.x += (_point.x > 0) ? 0.0000001f : -0.0000001f; _point.y += (_point.y > 0) ? 0.0000001f : -0.0000001f; if (visible) { base.draw(); if (texture != null) { //if the sprite is animated then the sourceRect needs to be changed to be the correct frame if (_animated) //adds support for sprite sheets by using the image width/height, frame width/height, and current frame index to split up the source image into rects //insted of using a fixed value of 0and only supporting sprite strips. sourceRect = new FlxRect((((_curIndex) % (texture.Width / frameWidth)) * frameWidth), (((int)(_curIndex / (texture.Width / frameWidth))) * frameHeight), frameWidth, frameHeight); Rectangle rect = new Rectangle((int)sourceRect.x, (int)sourceRect.y, (int)sourceRect.width, (int)sourceRect.height); FlxG.spriteBatch.Draw(texture, getVec2(), rect, _color * alpha, angle, new Vector2(), scale.getVec2(), SpriteEffects.None, 0f); } } } }