Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * PLAYSTATE.HX
- */
- package;
- import flixel.FlxG;
- import flixel.FlxSprite;
- import flixel.FlxState;
- import flixel.text.FlxText;
- import flixel.ui.FlxButton;
- import flixel.util.FlxMath;
- import flixel.util.FlxColor;
- import flixel.FlxCamera;
- class PlayState extends FlxState
- {
- private var _level:TiledLevel;
- public var _player:Player;
- override public function create():Void
- {
- //Set the background color
- FlxG.camera.bgColor = FlxColor.WHITE;
- //Load and add level elements
- _level = new TiledLevel('assets/data/map.tmx');
- add(_level.backgroundTiles);
- add(_level.collisionTiles);
- _player = new Player(0, 0);
- add(_player);
- FlxG.camera.follow(_player, FlxCamera.STYLE_PLATFORMER, 2);
- _level.loadObjects(this);
- super.create();
- }
- override public function update():Void
- {
- super.update();
- FlxG.collide(_player, _level.collisionTiles);
- }
- }
- /*
- * MENUSTATE.HX
- */
- package;
- import flixel.FlxG;
- import flixel.FlxSprite;
- import flixel.FlxState;
- import flixel.text.FlxText;
- import flixel.ui.FlxButton;
- import flixel.util.FlxMath;
- import flixel.util.FlxColor;
- class MenuState extends FlxState
- {
- private var _enterText:FlxText;
- private var _coin:FlxSprite;
- private var _start:Bool = false;
- override public function create():Void
- {
- //On Load Things
- FlxG.mouse.useSystemCursor = true;
- //Create Text
- _enterText = new FlxText(0, 0, 256, 'Press Space', 32, false);
- _enterText.color = FlxColor.WHITE;
- _enterText.x = (FlxG.width / 2 - _enterText.width / 2);
- _enterText.y = (FlxG.height / 2 - _enterText.height / 2);
- _enterText.alignment = "center";
- //Add Items
- add(_enterText);
- super.create();
- }
- private function setStart():Void
- {
- _start = !_start;
- }
- override public function destroy():Void
- {
- super.destroy();
- }
- private function newState():Void
- {
- FlxG.switchState(new PlayState());
- }
- override public function update():Void
- {
- super.update();
- //Switch State
- if ((FlxG.keys.anyJustPressed(["SPACE"]))&&(_start == false))
- {
- newState();
- _start = true;
- }
- }
- }
- /*
- * PLAYER.HX
- */
- package;
- import flixel.FlxBasic;
- import flixel.FlxObject;
- import flixel.FlxG;
- import flixel.FlxCamera;
- import flixel.FlxSprite;
- class Player extends FlxSprite
- {
- //Movement
- public var _speed:Float = 275;
- public var _jumpSpeed:Float = 1000;
- public var _gravity:Float = 2500;
- //Input
- private var _left:Bool;
- private var _right:Bool;
- private var _jump:Bool;
- public function new(X:Float, Y:Float)
- {
- super(X, Y);
- loadGraphic('assets/images/player.png', false, 64, 64);
- acceleration.y = _gravity;
- }
- private function input():Void
- {
- _left = FlxG.keys.anyPressed(["LEFT", "A"]);
- _right = FlxG.keys.anyPressed(["RIGHT", "D"]);
- _jump = FlxG.keys.anyJustPressed(["SPACE"]);
- if (_left && _right)
- _left = _right = false;
- }
- private function movement():Void
- {
- velocity.x = 0;
- if (_left)
- velocity.x = -_speed;
- if (_right)
- velocity.x = _speed;
- if ((isTouching(FlxObject.DOWN)) && (_jump))
- velocity.y -= _jumpSpeed;
- }
- override public function update()
- {
- movement();
- super.update();
- input();
- }
- }
- /*
- * TILEDLEVEL.HX
- */
- package;
- import openfl.Assets;
- import haxe.io.Path;
- import haxe.xml.Parser;
- import flixel.FlxG;
- import flixel.FlxObject;
- import flixel.FlxSprite;
- import flixel.group.FlxGroup;
- import flixel.tile.FlxTilemap;
- import flixel.addons.editors.tiled.TiledMap;
- import flixel.addons.editors.tiled.TiledObject;
- import flixel.addons.editors.tiled.TiledObjectGroup;
- import flixel.addons.editors.tiled.TiledTileSet;
- class TiledLevel extends TiledMap
- {
- private inline static var c_PATH_LEVEL_TILESHEETS = "assets/data/maps/";
- // Array of tilemaps used for collision
- public var foregroundTiles:FlxGroup;
- public var backgroundTiles:FlxGroup;
- public var collisionTiles:FlxGroup;
- private var collidableTileLayers:Array<FlxTilemap>;
- public function new(tiledLevel:Dynamic)
- {
- super(tiledLevel);
- foregroundTiles = new FlxGroup();
- backgroundTiles = new FlxGroup();
- collisionTiles = new FlxGroup();
- collisionTiles.visible = false;
- FlxG.worldBounds.set(0, 0, fullWidth, fullHeight);
- // Load Tile Maps
- for (tileLayer in layers)
- {
- var tileSheetName:String = tileLayer.properties.get("tileset");
- var tileLayerName:String = tileLayer.properties.get("layer");
- //Errors
- if (tileSheetName == null)
- throw "'tileset' property not defined for the '" + tileLayer.name + "' layer. Please add the property to the layer.";
- if (tileLayerName == null)
- throw "'layer' property not defined for the '" + tileLayer.name + "' layer. Please add the property to the layer.";
- var tileSet:TiledTileSet = null;
- for (ts in tilesets)
- {
- if (ts.name == tileSheetName)
- {
- tileSet = ts;
- break;
- }
- }
- if (tileSet == null)
- throw "Tileset '" + tileSheetName + " not found. Did you mispell the 'tilesheet' property in " + tileLayer.name + "' layer?";
- var imagePath = new Path(tileSet.imageSource);
- var processedPath = c_PATH_LEVEL_TILESHEETS + imagePath.file + "." + imagePath.ext;
- var tilemap:FlxTilemap = new FlxTilemap();
- tilemap.widthInTiles = width;
- tilemap.heightInTiles = height;
- tilemap.loadMap(tileLayer.tileArray, processedPath, tileSet.tileWidth, tileSet.tileHeight, 0, tileSet.firstGID, 1, 1);
- if (tileLayerName == 'background')
- {
- if (collidableTileLayers == null)
- collidableTileLayers = new Array<FlxTilemap>();
- collisionTiles.add(tilemap);
- collidableTileLayers.push(tilemap);
- backgroundTiles.add(tilemap);
- }
- }
- }
- public function loadObjects(state:PlayState)
- {
- for (group in objectGroups)
- {
- for (o in group.objects)
- {
- loadObject(o, group, state);
- }
- }
- }
- private function loadObject(o:TiledObject, g:TiledObjectGroup, state:PlayState)
- {
- var x:Int = o.x;
- var y:Int = o.y;
- // objects in tiled are aligned bottom-left (top-left in flixel)
- if (o.gid != -1)
- y -= g.map.getGidOwner(o.gid).tileHeight;
- switch (o.type.toLowerCase())
- {
- case 'player':
- state._player.x = x;
- state._player.y = y;
- }
- }
- public function collideWithLevel(obj:FlxObject, ?notifyCallback:FlxObject->FlxObject->Void, ?processCallback:FlxObject->FlxObject->Bool):Bool
- {
- if (collidableTileLayers != null)
- {
- for (map in collidableTileLayers)
- {
- // IMPORTANT: Always collide the map with objects, not the other way around.
- // This prevents odd collision errors (collision separation code off by 1 px).
- return FlxG.overlap(map, obj, notifyCallback, processCallback != null ? processCallback : FlxObject.separate);
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement