Handle events or whatever
This commit is contained in:
parent
b2624eb989
commit
b7e86ce8ac
@ -33,13 +33,41 @@
|
||||
"x":0,
|
||||
"y":0
|
||||
},
|
||||
{
|
||||
"draworder":"topdown",
|
||||
"id":3,
|
||||
"name":"Object Layer 1",
|
||||
"objects":[
|
||||
{
|
||||
"height":64,
|
||||
"id":1,
|
||||
"name":"",
|
||||
"properties":[
|
||||
{
|
||||
"name":"event",
|
||||
"type":"string",
|
||||
"value":"logSomething"
|
||||
}],
|
||||
"rotation":0,
|
||||
"type":"",
|
||||
"visible":true,
|
||||
"width":64,
|
||||
"x":128,
|
||||
"y":256
|
||||
}],
|
||||
"opacity":1,
|
||||
"type":"objectgroup",
|
||||
"visible":true,
|
||||
"x":0,
|
||||
"y":0
|
||||
},
|
||||
{
|
||||
"data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 182, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 203, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 223, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
182, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
182, 0, 0, 0, 0, 188, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 189, 229, 0, 0, 0, 190, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
@ -64,8 +92,8 @@
|
||||
"x":0,
|
||||
"y":0
|
||||
}],
|
||||
"nextlayerid":3,
|
||||
"nextobjectid":1,
|
||||
"nextlayerid":4,
|
||||
"nextobjectid":4,
|
||||
"orientation":"orthogonal",
|
||||
"renderorder":"right-down",
|
||||
"tiledversion":"1.11.2",
|
||||
|
@ -6,8 +6,8 @@ export default class Game {
|
||||
this.canvas = canvas
|
||||
this.ctx = canvas.getContext("2d")
|
||||
this.timestamp = 0
|
||||
this.actors = []
|
||||
this.actors.push(new Player(this, 200, 200))
|
||||
this.player = new Player(this, 200, 200)
|
||||
this.actors = [this.player]
|
||||
|
||||
this.input = new Input().initialize()
|
||||
|
||||
@ -30,6 +30,7 @@ export default class Game {
|
||||
|
||||
tick(dt) {
|
||||
this.actors.forEach(actor => actor.tick(dt))
|
||||
this.currentRoom.tick(this, dt)
|
||||
}
|
||||
|
||||
draw() {
|
||||
|
30
src/room.js
30
src/room.js
@ -1,3 +1,4 @@
|
||||
import { doRectanglesOverlap } from "./util.js"
|
||||
export default class Room {
|
||||
constructor(json, name) {
|
||||
this.json = json
|
||||
@ -5,6 +6,19 @@ export default class Room {
|
||||
console.log(json)
|
||||
}
|
||||
|
||||
tick(game, dt) {
|
||||
this.json.layers.forEach(layer => {
|
||||
if (layer.type == "objectgroup") this.tickObjectGroup(game, layer)
|
||||
})
|
||||
}
|
||||
|
||||
tickObjectGroup(game, layer) {
|
||||
const { player } = game
|
||||
layer.objects.forEach(object => {
|
||||
if (doRectanglesOverlap(player, object)) console.log(object)
|
||||
})
|
||||
}
|
||||
|
||||
get tilesetsToLoad() {
|
||||
const ts = {}
|
||||
this.json.tilesets.forEach((tileset, index) => {
|
||||
@ -23,7 +37,15 @@ export default class Room {
|
||||
}
|
||||
|
||||
draw(ctx) {
|
||||
this.json.layers.forEach(layer => {
|
||||
this.json.layers.forEach(this.drawLayer.bind(this, ctx))
|
||||
}
|
||||
|
||||
drawLayer(ctx, layer) {
|
||||
if (layer.type == "tilelayer") this.drawTileLayer(ctx, layer)
|
||||
else if (layer.type == "objectgroup") this.drawObjectGroup(ctx, layer)
|
||||
}
|
||||
|
||||
drawTileLayer(ctx, layer) {
|
||||
for (let y = 0; y < layer.height; y++) {
|
||||
for (let x = 0; x < layer.width; x++) {
|
||||
const index = x + (y * layer.width)
|
||||
@ -43,6 +65,12 @@ export default class Room {
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
drawObjectGroup(ctx, layer) {
|
||||
layer.objects.forEach(object => {
|
||||
ctx.fillStyle = "#FF000066"
|
||||
ctx.fillRect(object.x, object.y, object.width, object.height)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
15
src/util.js
15
src/util.js
@ -2,7 +2,20 @@ const SQRT_OF_TWO = Math.sqrt(2)
|
||||
|
||||
const isZeroVector = vector => !vector.x && !vector.y
|
||||
|
||||
const doRectanglesOverlap = (rect1, rect2) => {
|
||||
return (
|
||||
doLengthsOverlap({ x: rect1.x, width: rect1.width }, { x: rect2.x, width: rect2.width })
|
||||
&&
|
||||
doLengthsOverlap({ x: rect1.y, width: rect1.height }, { x: rect2.y, width: rect2.height })
|
||||
)
|
||||
}
|
||||
|
||||
const doLengthsOverlap = (l1, l2) => {
|
||||
return !((l1.x + l1.width < l2.x) || (l2.x + l2.width) < l1.x)
|
||||
}
|
||||
|
||||
export {
|
||||
SQRT_OF_TWO,
|
||||
isZeroVector
|
||||
isZeroVector,
|
||||
doRectanglesOverlap
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user