/// <summary> /// Called whenever the game is reset, doesn't have to do quite as much work as the basic initialization stuff. /// </summary> internal static void reset() { //FlxG.clearBitmapCache(); FlxG.resetInput(); FlxG.destroySounds(true); FlxG.levels = null; FlxG.scores = null; FlxG.level = 0; FlxG.score = 0; FlxG.paused = false; FlxG.timeScale = 1.0f; FlxG.elapsed = 0; FlxG.globalSeed = (float) FlxU.Random.NextDouble(); FlxG.worldBounds = new FlxRect(-10, -10, FlxG.width + 20, FlxG.height + 20); FlxG.worldDivisions = 6; /* var debugPathDisplay:DebugPathDisplay = FlxG.getPlugin(DebugPathDisplay) as DebugPathDisplay; if(debugPathDisplay != null) debugPathDisplay.clear(); */ }
/// <summary> /// Called by <code>FlxGame</code> to set up <code>FlxG</code> during <code>FlxGame</code>'s constructor. /// </summary> internal static void init(FlxGame game, int width, int height, float zoom) { FlxG._game = game; FlxG.width = width; FlxG.height = height; FlxG.mute = false; FlxG.volume = 1.0f; FlxG.sounds = new FlxGroup(); FlxG.volumeHandler = delegate { }; //FlxG.clearBitmapCache(); //if(flashGfxSprite == null) //{ // flashGfxSprite = new Sprite(); // flashGfx = flashGfxSprite.graphics; //} FlxCamera.DefaultZoom = zoom; //FlxG._cameraRect = new Rectangle(); FlxG.cameras = new List<FlxCamera>(); //FlxG.UseBufferLocking = false; FlxG.plugins = new List<FlxBasic>(); //addPlugin(new DebugPathDisplay()); //addPlugin(new TimerManager()); FlxG.mouse = new FlxMouse(); FlxG.keys = new FlxKeyboard(); //FlxG.mobile = false; FlxG.levels = null; FlxG.scores = null; FlxG.visualDebug = false; // flx# stuff FlxG.cameras = new List<FlxCamera>(); //FlxG.width = FlxG.graphics.PreferredBackBufferWidth; //FlxG.height = FlxG.graphics.PreferredBackBufferHeight; FlxG.bgColor = Color.Black; FlxG.mute = false; FlxG.sounds = new FlxGroup(); FlxG.worldBounds = new FlxRect(); FlxG.defaultFont = FlxS.ContentManager.Load<SpriteFont>("ConsoleFont"); //FlxG.zoom = 1f; 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(); 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(); }
/// <summary> /// Override this function to null out variables or /// manually call destroy() on class members if necessary. /// Don't forget to call super.destroy()! /// </summary> public override void destroy() { Velocity = null; Acceleration = null; Drag = null; MaxVelocity = null; ScrollFactor = null; _tagPoint = null; _tagRect = null; Last = null; // flx# - ? //cameras = null; if (Path != null) { Path.destroy(); Path = null; } base.destroy(); }
/// <summary> /// The Y-axis component of the object separation process. /// </summary> /// <param name="objectOne">Any <code>FlxObject</code>.</param> /// <param name="objectTwo">Any other <code>FlxObject</code>.</param> /// <returns></returns> public static bool separateY(FlxObject objectOne, FlxObject objectTwo) { //can't separate two immovable objects bool obj1immovable = objectOne.Immovable; bool obj2immovable = objectTwo.Immovable; if(obj1immovable && obj2immovable) return false; ////If one of the objects is a tilemap, just pass it off. if (objectOne is FlxTilemap) return (objectOne as FlxTilemap).overlapsWithCallback(objectTwo, separateY); if (objectTwo is FlxTilemap) return (objectTwo as FlxTilemap).overlapsWithCallback(objectOne, separateY, true); //First, get the two object deltas float overlap = 0; float obj1delta = objectOne.Y - objectOne.Last.Y; float obj2delta = objectTwo.Y - objectTwo.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(objectOne.X,objectOne.Y-((obj1delta > 0)?obj1delta:0),objectOne.Width,objectOne.Height+obj1deltaAbs); FlxRect obj2rect = new FlxRect(objectTwo.X,objectTwo.Y-((obj2delta > 0)?obj2delta:0),objectTwo.Width,objectTwo.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 + OverlapBias; //If they did overlap (and can), figure out by how much and flip the corresponding flags if(obj1delta > obj2delta) { overlap = objectOne.Y + objectOne.Height - objectTwo.Y; if((overlap > maxOverlap) || !Convert.ToBoolean(objectOne.AllowCollisions & Down) || !Convert.ToBoolean(objectTwo.AllowCollisions & Up)) overlap = 0; else { objectOne.Touching |= Down; objectTwo.Touching |= Up; } } else if(obj1delta < obj2delta) { overlap = objectOne.Y - objectTwo.Height - objectTwo.Y; if((-overlap > maxOverlap) || !Convert.ToBoolean(objectOne.AllowCollisions & Up) || !Convert.ToBoolean(objectTwo.AllowCollisions & Down)) overlap = 0; else { objectOne.Touching |= Up; objectTwo.Touching |= Down; } } } } //Then adjust their positions and velocities accordingly (if there was any overlap) if(overlap != 0) { float obj1v = objectOne.Velocity.Y; float obj2v = objectTwo.Velocity.Y; if(!obj1immovable && !obj2immovable) { overlap *= 0.5f; objectOne.Y = objectOne.Y - overlap; objectTwo.Y += overlap; float obj1velocity = (float)Math.Sqrt((obj2v * obj2v * objectTwo.Mass) / objectOne.Mass) * ((obj2v > 0) ? 1f : -1f); float obj2velocity = (float)Math.Sqrt((obj1v * obj1v * objectOne.Mass) / objectTwo.Mass) * ((obj1v > 0) ? 1f : -1f); float average = (obj1velocity + obj2velocity) * 0.5f; obj1velocity -= average; obj2velocity -= average; objectOne.Velocity.Y = average + obj1velocity * objectOne.Elasticity; objectTwo.Velocity.Y = average + obj2velocity * objectTwo.Elasticity; } else if(!obj1immovable) { objectOne.Y = objectOne.Y - overlap; objectOne.Velocity.Y = obj2v - obj1v*objectOne.Elasticity; //This is special case code that handles cases like horizontal moving platforms you can ride if(objectTwo.Active && objectTwo.Moves && (obj1delta > obj2delta)) objectOne.X += objectTwo.X - objectTwo.Last.X; } else if(!obj2immovable) { objectTwo.Y += overlap; objectTwo.Velocity.Y = obj1v - obj2v*objectTwo.Elasticity; //This is special case code that handles cases like horizontal moving platforms you can ride if(objectOne.Active && objectOne.Moves && (obj1delta < obj2delta)) objectTwo.X += objectOne.X - objectOne.Last.X; } return true; } else return false; }
/// <summary> /// The X-axis component of the object separation process. /// </summary> /// <param name="objectOne">Any <code>FlxObject</code>.</param> /// <param name="objectTwo">Any other <code>FlxObject</code>.</param> /// <returns></returns> public static bool separateX(FlxObject objectOne, FlxObject objectTwo) { // can't separate two immovable objects bool obj1immovable = objectOne.Immovable; bool obj2immovable = objectTwo.Immovable; if(obj1immovable && obj2immovable) return false; // If one of the objects is a tilemap, just pass it off. if (objectOne is FlxTilemap) return (objectOne as FlxTilemap).overlapsWithCallback(objectTwo, separateX); if (objectTwo is FlxTilemap) return (objectTwo as FlxTilemap).overlapsWithCallback(objectOne, separateX, true); // First, get the two object deltas float overlap = 0; float obj1delta = objectOne.X - objectOne.Last.X; float obj2delta = objectTwo.X - objectTwo.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(objectOne.X-((obj1delta > 0)?obj1delta:0),objectOne.Last.Y,objectOne.Width+((obj1delta > 0)?obj1delta:-obj1delta),objectOne.Height); FlxRect obj2rect = new FlxRect(objectTwo.X-((obj2delta > 0)?obj2delta:0),objectTwo.Last.Y,objectTwo.Width+((obj2delta > 0)?obj2delta:-obj2delta),objectTwo.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 + OverlapBias; //If they did overlap (and can), figure out by how much and flip the corresponding flags if(obj1delta > obj2delta) { overlap = objectOne.X + objectOne.Width - objectTwo.X; if((overlap > maxOverlap) || !Convert.ToBoolean(objectOne.AllowCollisions & Right) || !Convert.ToBoolean(objectTwo.AllowCollisions & Left)) overlap = 0; else { objectOne.Touching |= Right; objectTwo.Touching |= Left; } } else if(obj1delta < obj2delta) { overlap = objectOne.X - objectTwo.Width - objectTwo.X; if((-overlap > maxOverlap) || !Convert.ToBoolean(objectOne.AllowCollisions & Left) || !Convert.ToBoolean(objectTwo.AllowCollisions & Right)) overlap = 0; else { objectOne.Touching |= Left; objectTwo.Touching |= Right; } } } } //Then adjust their positions and velocities accordingly (if there was any overlap) if(overlap != 0) { float obj1v = objectOne.Velocity.X; float obj2v = objectTwo.Velocity.X; if(!obj1immovable && !obj2immovable) { overlap *= 0.5f; objectOne.X = objectOne.X - overlap; objectTwo.X += overlap; float obj1velocity = (float)Math.Sqrt((obj2v * obj2v * objectTwo.Mass) / objectOne.Mass) * ((obj2v > 0) ? 1f : -1f); float obj2velocity = (float)Math.Sqrt((obj1v * obj1v * objectOne.Mass) / objectTwo.Mass) * ((obj1v > 0) ? 1f : -1f); float average = (obj1velocity + obj2velocity) * 0.5f; obj1velocity -= average; obj2velocity -= average; objectOne.Velocity.X = average + obj1velocity * objectOne.Elasticity; objectTwo.Velocity.X = average + obj2velocity * objectTwo.Elasticity; } else if(!obj1immovable) { objectOne.X = objectOne.X - overlap; objectOne.Velocity.X = obj2v - obj1v*objectOne.Elasticity; } else if(!obj2immovable) { objectTwo.X += overlap; objectTwo.Velocity.X = obj1v - obj2v*objectTwo.Elasticity; } return true; } else return false; }
/// <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; 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; _tagPoint = new FlxPoint(); _tagRect = new FlxRect(); Path = null; PathSpeed = 0; PathAngle = 0; // flx# - ? //Health = 1; }