Skip to content

Instantly share code, notes, and snippets.

@zachwlewis
Created July 23, 2013 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachwlewis/6060007 to your computer and use it in GitHub Desktop.
Save zachwlewis/6060007 to your computer and use it in GitHub Desktop.
Lightly tweened movement on a fixed grid.
package worlds
{
import flash.display.BitmapData;
import flash.geom.Point;
import flash.geom.Rectangle;
import net.flashpunk.Entity;
import net.flashpunk.FP;
import net.flashpunk.World;
import net.flashpunk.graphics.Image;
import net.flashpunk.graphics.Tilemap;
import net.flashpunk.masks.Grid;
import net.flashpunk.tweens.misc.MultiVarTween;
import net.flashpunk.tweens.motion.LinearPath;
import net.flashpunk.utils.Ease;
import net.flashpunk.utils.Input;
import net.flashpunk.utils.Key;
public class GridWorld extends World
{
protected var _player:Entity;
protected var _map:Entity;
protected var _grid:Grid;
protected var _tilemap:Tilemap;
protected var _playerLocation:Point;
protected var _playerTween:MultiVarTween;
protected var _tween:LinearPath;
public var px:Number;
public var py:Number;
public function GridWorld()
{
px = 0;
py = 0;
}
override public function begin():void
{
createGrid();
// Create the player.
_player = add(new Entity(px, py, new Image(new BitmapData(24, 24, true, 0xffff3366))));
_player.graphic.x = 4;
_player.graphic.y = 4;
_player.setHitbox(32, 32, 0, 0);
}
override public function update():void
{
var inX:int = 0;
var inY:int = 0;
// Get player input.
if (Input.pressed(Key.LEFT)) inX--;
if (Input.pressed(Key.RIGHT)) inX++;
// Check horizontal collision with map.
if (inX != 0 && !_player.collideWith(_map, (_playerLocation.x + inX) * 32, (_playerLocation.y) * 32))
{
_playerLocation.x += inX;
}
if (Input.pressed(Key.UP)) inY--;
if (Input.pressed(Key.DOWN)) inY++;
// Check vertical collision with map.
if (inY != 0 && !_player.collideWith(_map, (_playerLocation.x) * 32, (_playerLocation.y + inY) * 32))
{
_playerLocation.y += inY;
}
setPlayerLocation(_playerLocation);
// Check for scenario restart.
if (Input.pressed(Key.R))
{
FP.world = new GridWorld();
}
// Update the player's location.
if (_tween != null)
{
_player.x = Math.floor(_tween.x);
_player.y = Math.floor(_tween.y);
}
}
protected function setPlayerLocation(location:Point):void
{
if (_tween == null || !_tween.active)
{
_tween = new LinearPath();
_tween.addPoint(_player.x, _player.y);
_tween.addPoint(location.x * 32, location.y * 32);
_tween.setMotionSpeed(150, Ease.circOut);
addTween(_tween, true);
}
else
{
_tween.addPoint(location.x * 32, location.y * 32);
}
}
protected function createGrid():void
{
var tiles:BitmapData = new BitmapData(64, 32, true, 0xffffffff);
tiles.fillRect(new Rectangle(0, 0, 32, 32), 0x00000000);
_grid = new Grid(640, 480, 32, 32);
_tilemap = new Tilemap(tiles, 640, 480, 32, 32);
// Fill the entire map.
_grid.setRect(0, 0, 20, 15, true);
_tilemap.setRect(0, 0, 20, 15, 1);
// Clear out space for the player.
for (var ix:uint = 1; ix < _grid.columns - 1; ix++)
{
for (var iy:uint = 1; iy < _grid.rows - 1; iy++)
{
var isSolid:Boolean = (FP.random < 0.1);
_grid.setTile(ix, iy, isSolid);
_tilemap.setTile(ix, iy, isSolid ? 1 : 0);
// Put the player in an empty square.
if (!isSolid)
{
px = ix * 32;
py = iy * 32;
_playerLocation = new Point(ix, iy);
}
}
}
// Add the tilemap and grid to the world.
_map = add(new Entity(0, 0, _tilemap, _grid));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment