Skip to content

Instantly share code, notes, and snippets.

@joonjoonjoon
Last active August 29, 2015 14:01
Show Gist options
  • Save joonjoonjoon/28290c38021108a9b34c to your computer and use it in GitHub Desktop.
Save joonjoonjoon/28290c38021108a9b34c to your computer and use it in GitHub Desktop.
Weird piling behavior
package;
import flash.Lib;
import flixel.FlxGame;
import flixel.FlxG;
import flixel.system.scaleModes.FixedScaleMode;
class GameClass extends FlxGame
{
public function new()
{
var stageWidth:Int = Lib.current.stage.stageWidth;
var stageHeight:Int = Lib.current.stage.stageHeight;
//var ratioX:Float = stageWidth / 640;
//var ratioY:Float = stageHeight / 400;
//var ratio:Float = Math.min(ratioX, ratioY);
var fps:Int = 60;
//super(Math.ceil(stageWidth / ratio), Math.ceil(stageHeight / ratio), MenuState, ratio, fps, fps);
var scaledRatio:Float = 1.5;
var scaledWidth:Float = 640 * scaledRatio;
var scaledHeight:Float = 400 * scaledRatio;
super(Math.ceil(scaledWidth), Math.ceil(scaledHeight), PlayStateTestCollision, 1, 65, 60);
//FlxG.fixedTimestep = false;
}
}
package ;
import flash.Lib;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.group.FlxGroup;
import flixel.util.FlxColor;
class PlayStateTestCollision extends FlxState
{
public static var bodiesGroup:FlxGroup;
private var player:FlxSprite;
private var bodies:FlxGroup;
private var floor:FlxSprite;
private var wall:FlxSprite;
private var playerAlive:Bool;
private var climbedThisFrame:Int;
private var climbedLastFrame:Int;
private var lastPlayerVelocity:Float;
public static inline var RUN_SPEED:Int = 600; // 750 // 450
public static inline var GRAVITY:Int = 1820;
public static inline var KNEEHEIGHT:Int = 40;
override public function create():Void
{
super.create();
// create a player
newPlayer();
// create a floor
floor = new FlxSprite(0, 450);
floor.makeGraphic(1000, 100);
floor.immovable = true;
add(floor);
// create a wall
wall = new FlxSprite(700,00);
wall.makeGraphic(100, 1000,FlxColor.GOLDEN);
wall.immovable = true;
add(wall);
// create a group for bodies
bodies = new FlxGroup();
add(bodies);
}
override public function update():Void
{
lastPlayerVelocity = player.velocity.x;
// if the player is lying still, make a new player
if (Math.abs(player.x - player.last.x) < 1 && Math.abs(player.y - player.last.y) < 1 && player.x > 20) // the >20 is there so it doesn't trigger in the first frames
{
newPlayer();
}
//Lib.trace(player.velocity.x + " " + player.velocity.y);
// collisions
FlxG.collide(player, floor);
FlxG.collide(player, bodies, callbackBody);
FlxG.collide(player, wall, callbackWall);
FlxG.collide(bodies, floor);
// remember how much we climbed in the last frame
climbedLastFrame = climbedThisFrame;
climbedThisFrame = 0;
super.update();
}
public function callbackBody(player:FlxSprite, body:FlxSprite):Void
{
// calculate how much needs to be climbed
var climbNow:Int = Std.int(player.y + player.height - body.y);
// check if that is allowed, we can maximum go the height of KNEEHEIGHT over 3 frames
if (climbNow + climbedThisFrame + climbedLastFrame < KNEEHEIGHT)
{
if (climbNow != 0)
{
// move player on top of obstacle
player.y -= climbNow + 1;
// continue velocity
player.velocity.x = lastPlayerVelocity;
// record this climb
climbedThisFrame += climbNow;
Lib.trace("Step up. " + climbNow + " (" + climbedThisFrame + "," + climbedLastFrame + ") " + bodies.members.indexOf(body));
}
}
else
{
// it's too high, so kill the player by stopping the acceleration, and let the update loop take care of it
player.x = player.last.x;
player.y += climbedThisFrame + climbedLastFrame;
killPlayer();
Lib.trace("too high. " + " (" + climbedThisFrame + "," + climbedLastFrame + ") " + bodies.members.indexOf(body));
}
}
public function callbackWall(player:FlxSprite, wall:FlxSprite):Void
{
// player hit the wall... kill player by stopping the accel
killPlayer();
}
public function newPlayer():Void
{
// if a player exists, transform him into a dead body
if (player != null)
{
bodies.add(player);
player.immovable = true;
player.acceleration.x = 0;
player.acceleration.y = 0;
}
// create a new player
var newPlayer:FlxSprite;
newPlayer = new FlxSprite(10, 200);
newPlayer.makeGraphic(30, 30, FlxColor.CHARTREUSE);
newPlayer.acceleration.x = newPlayer.drag.x;
newPlayer.drag.set(RUN_SPEED * 8, RUN_SPEED * 80);
newPlayer.maxVelocity.set(RUN_SPEED, 780);
newPlayer.acceleration.y = GRAVITY;
newPlayer.acceleration.x = newPlayer.drag.x;
add(newPlayer);
this.player = newPlayer;
playerAlive = true;
}
public function killPlayer():Void
{
if (playerAlive)
{
player.acceleration.x = 0;
playerAlive = false;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<project>
<app title="Try Harder" file="TryHarder" main="Main" version="0.0.1" company="Glitchnap" />
<window width="640" height="400" fps="30" orientation="landscape" fullscreen="false" hardware="true" vsync="false" background="0x000000" if="flash" />
<!--The flixel preloader gets stuck in Chrome, so it's disabled by default for now.
Safe to use if you embed the swf into a html file!-->
<!--<app preloader="flixel.system.FlxPreloader" />-->
<!--The swf version should be at least 11.2 if you want to use the FLX_MOUSE_ADVANCED option-->
<set name="SWF_VERSION" value="11.2" />
<set name="BUILD_DIR" value="export" />
<classpath name="source" />
<assets path="assets" />
<haxelib name="openfl" version="1.4.0"/>
<haxelib name="flixel" version="git"/>
<!-- https://github.com/joonjoonjoon/flixel/tree/try-har -- based on 3.2.0 -->
<haxelib name="flixel-addons"/>
<haxelib name="actuate"/>
<!--In case you want to use nape with flixel-->
<!--<haxelib name="nape" />-->
<!--Enable this for nape release builds for a serious peformance improvement-->
<!--<haxedef name="NAPE_RELEASE_BUILD" />-->
<!--Enable the flixel core recording system-->
<!--<haxedef name="FLX_RECORD" />-->
<!--Enable right and middle click support for the mouse. Flash player version 11.2+, no HTML5 support -->
<!--<haxedef name="FLX_MOUSE_ADVANCED" />-->
<!--Disable the Native cursor api for Flash target-->
<!-- <haxedef name="FLX_NO_NATIVE_CURSOR" /> -->
<!--Optimise inputs, be careful you will get null errors if you don't use conditionals in your game-->
<haxedef name="FLX_NO_MOUSE" />
<!--<haxedef name="FLX_NO_KEYBOARD" if="mobile" />-->
<haxedef name="FLX_NO_TOUCH" />
<haxedef name="FLX_NO_GAMEPAD" />
<!--Disable the Flixel core sound tray-->
<haxedef name="FLX_NO_SOUND_TRAY" />
<!--Disable the Flixel core focus lost screen-->
<haxedef name="FLX_NO_FOCUS_LOST_SCREEN" />
<!--Disable the Flixel core debugger. Automatically gets set whenever you compile in release mode!-->
<haxedef name="FLX_NO_DEBUG" unless="debug" />
<haxedef name="advanced-telemetry" />
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment