Object collision
This commit is contained in:
parent
9ea7564ac8
commit
d65f6af7f0
@ -92,6 +92,11 @@
|
|||||||
"id":5,
|
"id":5,
|
||||||
"name":"sign_crate",
|
"name":"sign_crate",
|
||||||
"properties":[
|
"properties":[
|
||||||
|
{
|
||||||
|
"name":"collides",
|
||||||
|
"type":"bool",
|
||||||
|
"value":true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name":"interactEvent",
|
"name":"interactEvent",
|
||||||
"type":"string",
|
"type":"string",
|
||||||
|
@ -35,6 +35,7 @@ export default class Message {
|
|||||||
ctx.fillRect(0, 0, ctx.canvas.width, this.backgroundHeight)
|
ctx.fillRect(0, 0, ctx.canvas.width, this.backgroundHeight)
|
||||||
ctx.font = `bold ${this.textSize}px sans-serif`
|
ctx.font = `bold ${this.textSize}px sans-serif`
|
||||||
ctx.textBaseline = "top"
|
ctx.textBaseline = "top"
|
||||||
|
ctx.textAlign = "left"
|
||||||
ctx.fillStyle = this.textColor
|
ctx.fillStyle = this.textColor
|
||||||
ctx.fillText(this.currentText.substring(0, this.textIndex), 5, 5)
|
ctx.fillText(this.currentText.substring(0, this.textIndex), 5, 5)
|
||||||
if (this.messageComplete()) {
|
if (this.messageComplete()) {
|
||||||
|
@ -27,9 +27,7 @@ export default class Player extends Actor {
|
|||||||
this.x += this.xVel
|
this.x += this.xVel
|
||||||
this.y += this.yVel
|
this.y += this.yVel
|
||||||
|
|
||||||
const tur = this.game.currentRoom.tilesUnderRectangle(this).filter(x => x)
|
if (this.collidesWithAbsolutelyAnything()) {
|
||||||
const colliders = tur.filter(tile => tile.properties.find(prop => prop.name == "collides" && prop.value))
|
|
||||||
if (tur.length >= 1) {
|
|
||||||
this.x -= this.xVel
|
this.x -= this.xVel
|
||||||
this.y -= this.yVel
|
this.y -= this.yVel
|
||||||
}
|
}
|
||||||
@ -40,6 +38,21 @@ export default class Player extends Actor {
|
|||||||
else this.interactHitbox = null
|
else this.interactHitbox = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
collidesWithAbsolutelyAnything() {
|
||||||
|
return this.collidesWithTiles() || this.collidesWithObjects()
|
||||||
|
}
|
||||||
|
|
||||||
|
collidesWithObjects() {
|
||||||
|
const objects = this.game.currentRoom.objectsUnderRectangle(this)
|
||||||
|
return !!objects.find(object => object.collides())
|
||||||
|
}
|
||||||
|
|
||||||
|
collidesWithTiles() {
|
||||||
|
const tur = this.game.currentRoom.tilesUnderRectangle(this).filter(x => x)
|
||||||
|
const colliders = tur.filter(tile => tile.properties.find(prop => prop.name == "collides" && prop.value))
|
||||||
|
return !!colliders.length
|
||||||
|
}
|
||||||
|
|
||||||
createInteractHitbox() {
|
createInteractHitbox() {
|
||||||
this.interactHitbox = {
|
this.interactHitbox = {
|
||||||
width: this.width,
|
width: this.width,
|
||||||
|
11
src/room.js
11
src/room.js
@ -1,11 +1,12 @@
|
|||||||
import RoomObject from "./roomObject.js"
|
import RoomObject from "./roomObject.js"
|
||||||
|
import { doRectanglesOverlap } from "./util.js"
|
||||||
|
|
||||||
export default class Room {
|
export default class Room {
|
||||||
constructor(game, json, name) {
|
constructor(game, json, name) {
|
||||||
this.game = game
|
this.game = game
|
||||||
this.json = json
|
this.json = json
|
||||||
this.name = name
|
this.name = name
|
||||||
const objectJson = this.json.layers.find(layer => layer.type == "objectgroup")?.objects || []
|
const objectJson = this.objectLayers.map(layer => layer.objects).flat()
|
||||||
this.objects = objectJson.map(RoomObject.fromJson.bind(null, this.game))
|
this.objects = objectJson.map(RoomObject.fromJson.bind(null, this.game))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,6 +38,14 @@ export default class Room {
|
|||||||
return this.json.layers.filter(layer => layer.type == "tilelayer")
|
return this.json.layers.filter(layer => layer.type == "tilelayer")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get objectLayers() {
|
||||||
|
return this.json.layers.filter(layer => layer.type == "objectgroup")
|
||||||
|
}
|
||||||
|
|
||||||
|
objectsUnderRectangle(rect) {
|
||||||
|
return this.objects.filter(object => doRectanglesOverlap(object, rect))
|
||||||
|
}
|
||||||
|
|
||||||
tilesUnderRectangle(rect) {
|
tilesUnderRectangle(rect) {
|
||||||
return this.tileLayers.map(layer => this.tilesUnderRectangleInLayer(layer, rect)).flat()
|
return this.tileLayers.map(layer => this.tilesUnderRectangleInLayer(layer, rect)).flat()
|
||||||
}
|
}
|
||||||
|
@ -45,4 +45,8 @@ export default class RoomObject {
|
|||||||
ctx.fillStyle = this.getProperty("color") || "#00000000"
|
ctx.fillStyle = this.getProperty("color") || "#00000000"
|
||||||
ctx.fillRect(this.x, this.y, this.width, this.height)
|
ctx.fillRect(this.x, this.y, this.width, this.height)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
collides() {
|
||||||
|
return this.getProperty("collides") || false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user