mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
added tiled level support
This commit is contained in:
parent
83e78a5732
commit
3782fa345b
13 changed files with 113 additions and 39 deletions
|
|
@ -15,11 +15,15 @@ function (Parent, Settings, NotificationCenter) {
|
|||
Tile.prototype.createMesh = function() {
|
||||
var self = this;
|
||||
|
||||
/*
|
||||
var texturePath = Settings.GRAPHICS_PATH
|
||||
+ Settings.GRAPHICS_SUBPATH_TILES
|
||||
+ this.options.m + '/'
|
||||
+ this.options.s + ''
|
||||
+ (this.options.r || 0) + '.gif';
|
||||
*/
|
||||
|
||||
var texturePath = Settings.MAPS_PATH + this.options.t;
|
||||
|
||||
var callback = function(mesh) {
|
||||
self.mesh = mesh;
|
||||
|
|
|
|||
9
app/Game/Client/Loader/TiledLevel.js
Normal file
9
app/Game/Client/Loader/TiledLevel.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
define([
|
||||
"Game/Core/Loader/TiledLevel"
|
||||
],
|
||||
|
||||
function (Parent) {
|
||||
|
||||
return Parent;
|
||||
|
||||
});
|
||||
|
|
@ -15,10 +15,10 @@ define({
|
|||
GRAPHICS_SUBPATH_ITEMS: 'Items/',
|
||||
GRAPHICS_SUBPATH_CHARACTERS: 'Characters/',
|
||||
GRAPHICS_SUBPATH_TILES: 'Tiles/',
|
||||
MAPS_PATH: 'static/maps/chuck/',
|
||||
MAPS_PATH: 'static/maps/tiled/',
|
||||
|
||||
RATIO: 21, //35
|
||||
TILE_SIZE: 15, //15, 25 is original picture
|
||||
TILE_SIZE: 25, //15, 25 is original picture
|
||||
CAMERA_IS_ORTHOGRAPHIC: true,
|
||||
VIEW_CONTROLLER: 0 ? 'Three' : 'Pixi',
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
define([
|
||||
"Game/" + GLOBALS.context + "/Physics/Engine",
|
||||
"Game/" + GLOBALS.context + "/Loader/Level",
|
||||
"Game/" + GLOBALS.context + "/Loader/TiledLevel",
|
||||
"Game/" + GLOBALS.context + "/Player"
|
||||
],
|
||||
|
||||
function (PhysicsEngine, Level, Player) {
|
||||
function (PhysicsEngine, TiledLevel, Player) {
|
||||
|
||||
function GameController () {
|
||||
this.players = {};
|
||||
|
|
@ -37,7 +37,7 @@ function (PhysicsEngine, Level, Player) {
|
|||
};
|
||||
}
|
||||
|
||||
this.level = new Level(levelUid, this.physicsEngine, this.gameObjects);
|
||||
this.level = new TiledLevel(levelUid, this.physicsEngine, this.gameObjects);
|
||||
}
|
||||
|
||||
GameController.prototype.onResetLevel = function() {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
define([
|
||||
"Game/" + GLOBALS.context + "/GameObjects/GameObject",
|
||||
"Lib/Vendor/Box2D",
|
||||
"Game/Config/Settings"
|
||||
"Game/Config/Settings",
|
||||
"Lib/Utilities/Exception"
|
||||
],
|
||||
|
||||
function (Parent, Box2D, Settings) {
|
||||
function (Parent, Box2D, Settings, Exception) {
|
||||
|
||||
function Tile(physicsEngine, uid, options) {
|
||||
this.options = options;
|
||||
|
|
@ -97,6 +98,7 @@ function (Parent, Box2D, Settings) {
|
|||
break;
|
||||
|
||||
default:
|
||||
throw new Exception("Tile Creation - no shape given");
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ define([
|
|||
|
||||
], function (Settings, Box2D, NotificationCenter, CollisionDetector, Tile, Item, Skateboard) {
|
||||
|
||||
// Public
|
||||
function Level (uid, engine, gameObjects) {
|
||||
this.uid = uid;
|
||||
this.engine = engine;
|
||||
|
|
@ -23,7 +22,7 @@ define([
|
|||
var path = Settings.MAPS_PATH + uid + ".json"
|
||||
this.loadLevelDataFromPath(path, function(levelData) {
|
||||
self.createTiles(levelData);
|
||||
self.createItems(levelData);
|
||||
//self.createItems(levelData);
|
||||
NotificationCenter.trigger("game/level/loaded");
|
||||
});
|
||||
}
|
||||
|
|
@ -37,8 +36,6 @@ define([
|
|||
}
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
Level.prototype.createTiles = function (levelData) {
|
||||
|
||||
if (!levelData || !levelData.tiles || levelData.tiles.length < 1) {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
define([
|
||||
"Game/" + GLOBALS.context + "/Loader/Level",
|
||||
"Game/Config/Settings",
|
||||
"Lib/Vendor/Box2D",
|
||||
"Game/" + GLOBALS.context + "/Collision/Detector",
|
||||
|
|
@ -6,41 +7,75 @@ define([
|
|||
"Game/" + GLOBALS.context + "/GameObjects/Item",
|
||||
"Game/" + GLOBALS.context + "/GameObjects/Items/Skateboard",
|
||||
|
||||
], function (Settings, Box2D, CollisionDetector, Tile, Item, Skateboard) {
|
||||
], function (Parent, Settings, Box2D, CollisionDetector, Tile, Item, Skateboard) {
|
||||
|
||||
// Public
|
||||
function Level (path, engine, gameObjects) {
|
||||
this.path = path;
|
||||
this.engine = engine;
|
||||
this.levelObject = null;
|
||||
this.gameObjects = gameObjects;
|
||||
function TiledLevel (path, engine, gameObjects) {
|
||||
this.levelData = null;
|
||||
Parent.call(this, path, engine, gameObjects);
|
||||
}
|
||||
|
||||
Level.prototype.loadLevelInToEngine = function () {
|
||||
this.loadLevelObjectFromPath(this.path);
|
||||
this.createTiles();
|
||||
//this.createItems();
|
||||
}
|
||||
TiledLevel.prototype = Object.create(Parent.prototype);
|
||||
|
||||
// Private
|
||||
TiledLevel.prototype.createTiles = function (levelData) {
|
||||
this.levelData = levelData;
|
||||
|
||||
Level.prototype.createTiles = function () {
|
||||
if (!this.levelObject || !this.levelObject.tiles || this.levelObject.tiles.length < 1) {
|
||||
throw "Level: Can't create physic tiles, no tiles found";
|
||||
if (!levelData) {
|
||||
throw "Level: Can't create level, nothing found";
|
||||
}
|
||||
|
||||
var tiles = this.levelObject.tiles;
|
||||
var collisionLayer = null;
|
||||
|
||||
for (var i = 0; i < tiles.length; i++) {
|
||||
var options = tiles[i];
|
||||
options.m = this.tileAtPositionExists(options.x, options.y - 1) ? "Soil" : "GrassSoil";
|
||||
this.gameObjects.fixed.push(new Tile(this.engine, "tile-" + i, options));
|
||||
for (var i = 0; i < levelData.layers.length; i++) {
|
||||
if(levelData.layers[i].name === "collision") {
|
||||
collisionLayer = levelData.layers[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(collisionLayer) {
|
||||
|
||||
for (var i = 0; i < collisionLayer.data.length; i++) {
|
||||
|
||||
var gid = collisionLayer.data[i];
|
||||
if(gid === 0) continue;
|
||||
|
||||
var imagePath = this.getTileImagePath(gid);
|
||||
|
||||
|
||||
var parts = imagePath.split("/");
|
||||
var tileType = parts[parts.length - 1].split(".")[0].split("");
|
||||
|
||||
// FIXME rename s to shape, r to rotation etc.
|
||||
|
||||
var options = {
|
||||
s: parseInt(tileType[0], 10),
|
||||
r: parseInt(tileType[1], 10),
|
||||
t: imagePath,
|
||||
x: i % collisionLayer.width,
|
||||
y: parseInt(i / collisionLayer.height , 10)
|
||||
}
|
||||
|
||||
this.gameObjects.fixed.push(new Tile(this.engine, "tile-" + i, options));
|
||||
}
|
||||
|
||||
} else {
|
||||
console.warn("Level: No collision Layer given");
|
||||
}
|
||||
|
||||
this.levelData = null; // free up memory
|
||||
}
|
||||
|
||||
TiledLevel.prototype.getTileImagePath = function(gid) {
|
||||
//console.log(this.levelData.tilesets)
|
||||
for (var i = 0; i < this.levelData.tilesets.length; i++) {
|
||||
var tileset = this.levelData.tilesets[i];
|
||||
var offset = tileset.firstgid;
|
||||
if(gid >= offset && gid < offset + Object.keys(tileset.tiles).length) {
|
||||
return tileset.tiles["" + (gid - offset)].image;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Level.prototype.loadLevelObjectFromPath = function (path) {
|
||||
|
||||
}
|
||||
|
||||
return Level;
|
||||
return TiledLevel;
|
||||
})
|
||||
|
|
@ -13,7 +13,7 @@
|
|||
var self = this;
|
||||
|
||||
this.options = options = Options.merge(options, {
|
||||
levelUids: ["dungeon"]
|
||||
levelUids: ["circles", "dungeon"]
|
||||
});
|
||||
|
||||
this.name = name;
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
|||
|
||||
GameController.prototype.spawnPlayer = function(player) {
|
||||
var x = 150 + Math.random() * 300,
|
||||
y = 50;
|
||||
y = 0;
|
||||
player.spawn(x, y);
|
||||
this.gameObjects.animated.push(player.getDoll());
|
||||
|
||||
|
|
|
|||
9
app/Game/Server/Loader/TiledLevel.js
Normal file
9
app/Game/Server/Loader/TiledLevel.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
define([
|
||||
"Game/Core/Loader/TiledLevel"
|
||||
],
|
||||
|
||||
function (Parent) {
|
||||
|
||||
return Parent;
|
||||
|
||||
});
|
||||
18
snippets/sublime/js-define-child.sublime-snippet
Normal file
18
snippets/sublime/js-define-child.sublime-snippet
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<snippet>
|
||||
<content><![CDATA[
|
||||
define([
|
||||
"${1:Path}"
|
||||
],
|
||||
|
||||
function (Parent) {
|
||||
|
||||
return Parent;
|
||||
|
||||
});
|
||||
]]></content>
|
||||
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
|
||||
<tabTrigger>defc</tabTrigger>
|
||||
<!-- Optional: Set a scope to limit where the snippet will trigger -->
|
||||
<scope>source.js</scope>
|
||||
<description>define(Child) - create a new empty child require.js module</description>
|
||||
</snippet>
|
||||
BIN
static/img/tile_shape_rotation_map.jpg
Normal file
BIN
static/img/tile_shape_rotation_map.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue