Browse Source

stuff

master
mortie 6 years ago
parent
commit
a2d3fa8da4
7 changed files with 82 additions and 34 deletions
  1. 6
    17
      js/Structure.js
  2. 53
    0
      js/Texture.js
  3. 1
    1
      js/assets.js
  4. 1
    1
      js/main.js
  5. 5
    14
      js/structures.js
  6. 14
    0
      js/tiles.js
  7. 2
    1
      js/traits/TPhysics.js

+ 6
- 17
js/Structure.js View File

@@ -1,15 +1,16 @@
import assets from "./assets.js";
import Rect from "./Rect.js";
import Vec2 from "./Vec2.js";
import Texture from "./Texture.js";

function findBounds(arr, bounds) {
arr.forEach(e => {
let right = (e.x + 1) * assets.tiles.tileWidth;
let bottom = (e.y + 1) * assets.tiles.tileHeight;

if (bounds.size.x < right)
if (right > bounds.size.x)
bounds.size.x = right;
if (bounds.size.y < bottom)
if (bottom > bounds.size.y)
bounds.size.y = bottom;
});
}
@@ -86,12 +87,6 @@ function findGeometry(bounds, arr, geometry) {
}
}

function drawToCanvas(ctx, arr) {
arr.forEach(e => {
assets.tiles.drawTile(ctx, e.tile, e.x, e.y);
});
}

function flattened(arr, newArr = []) {
arr.forEach(e => {
if (e instanceof Array)
@@ -104,6 +99,8 @@ function flattened(arr, newArr = []) {

export default class Structure {
constructor(attrs, nestedArr) {
this.texture = new Texture(nestedArr);

this.attrs = {
collides: false,
}
@@ -114,28 +111,20 @@ export default class Structure {
this.nestedArr = nestedArr;
this.bounds = new Rect();
this.geometry = [];
this.can = document.createElement("canvas");
}

init() {
var arr = flattened(this.nestedArr);

findBounds(arr, this.bounds);
this.can.width = this.bounds.size.x;
this.can.height = this.bounds.size.y;

if (this.attrs.collides) {
findGeometry(this.bounds, arr, this.geometry);
}

let ctx = this.can.getContext("2d");
assets.tiles.whenReady(() => drawToCanvas(ctx, arr));
}

draw(ctx) {
ctx.drawImage(
this.can, this.bounds.pos.pixelX, this.bounds.pos.pixelY,
this.bounds.size.x, this.bounds.size.y);
this.texture.draw(ctx, this.bounds.pos.pixelX, this.bounds.pos.pixelY);
}

setAttr(attr) {

+ 53
- 0
js/Texture.js View File

@@ -0,0 +1,53 @@
import Rect from "./Rect.js";
import assets from "./assets.js";

export default class Texture {
constructor(nestedArr) {
this.nestedArr = nestedArr;
this.can = document.createElement("canvas");
this.ctx = this.can.getContext("2d");
this.width = 0;
this.height = 0;

assets.tiles.whenReady(() => this.init());
}

findWidthHeight(arr) {
arr.forEach(e => {
if (e instanceof Array) {
this.findWidthHeight(e);
} else {
let right = (e.x + 1) * assets.tiles.tileWidth;
let bottom = (e.y + 1) * assets.tiles.tileHeight;
if (right > this.width)
this.width = right;
if (bottom > this.height)
this.height = bottom;
}
});
}

fillCanvas(arr) {
arr.forEach(e => {
if (e instanceof Array) {
this.fillCanvas(e);
} else {
assets.tiles.drawTile(this.ctx, e.tile, e.x, e.y);
}
});
}

init() {
this.findWidthHeight(this.nestedArr);
this.can.width = this.width;
this.can.height = this.height;
this.fillCanvas(this.nestedArr);
}

draw(ctx, x, y) {
ctx.drawImage(this.can, x, y, this.width, this.height);
}
}

Texture.fromLine = function(l, m, r, lr) {
}

+ 1
- 1
js/assets.js View File

@@ -1,7 +1,7 @@
import SpriteSheet from "./SpriteSheet.js";

export default {
tiles: new SpriteSheet("assets/tiles.png", 32, 32, 2)
tiles: new SpriteSheet("assets/tiles.png", 32, 32, 1)
.defineTile("ground", 0, 0)
.defineTile("grass", 1, 0)
.defineTile("grass-l", 2, 0)

+ 1
- 1
js/main.js View File

@@ -10,7 +10,7 @@ let level = new Level(canvas);

level.spawnEntity(new Player(level), 10, 1);
level.spawnEntity(new Platform(level), 16, 4);
level.spawnStructure(structures.floor(4), 4, 4);
level.spawnStructure(structures.floor(8), 4, 4);

level.start();


+ 5
- 14
js/structures.js View File

@@ -1,17 +1,8 @@
import Structure from "./Structure.js";
import tiles from "./tiles.js";

export default {
floor: (width) => {
let attrs = [ "collides" ];
if (width <= 1) {
return new Structure(attrs, [ { x: 0, y: 0, tile: "grass-lr" }]);
} else {
return new Structure(attrs, [
{ x: 0, y: 0, tile: "grass-l", },
Array.from({ length: width - 2 }, (_, i) =>
({ x: i + 1, y: 0, tile: "grass" })),
{ x: width - 1, y: 0, tile: "grass-r" },
]);
}
},
}
floor: width => new Structure(
[ "collides" ],
tiles.fromLine(width, "grass")),
};

+ 14
- 0
js/tiles.js View File

@@ -0,0 +1,14 @@
export default {
fromLine: function(width, tile) {
if (width <= 1) {
return [ { x: 0, y: 0, tile: tile+"-lr" }];
} else {
return [
{ x: 0, y: 0, tile: tile+"-l", },
Array.from({ length: width - 2 }, (_, i) =>
({ x: i + 1, y: 0, tile: tile })),
{ x: width - 1, y: 0, tile: tile+"-r" },
];
}
},
}

+ 2
- 1
js/traits/TPhysics.js View File

@@ -49,7 +49,8 @@ export default class TPhysics extends Trait {
}
}

this.relVelocity.y += this.gravity * dt;
if (!this.onGround)
this.relVelocity.y += this.gravity * dt;

// If we just started riding something, adjust relative velocity
if (!this.prevRelativeTo && this.relativeTo) {

Loading…
Cancel
Save