patroling
Permet à un objet de se déplacer jusqu'à ce qu'il recontre un objet
parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
speed | number | 60 | |
direction | number | -1 | |
uTrunDelay | number | 0.5 | |
startDelay | number | 0.5 | |
crossEachOther | boolean | true | |
tag | string | "stopper" |
example
patroling({ speed: 60, direction: -1, uTrunDelay: 0.5, startDelay: 0.5, crossEachOther: true, tag: "stopper"}),function patroling(p) { const param = { speed: 60, direction: -1, uTrunDelay: 0.5, startDelay: 0.5, crossEachOther: true, tag: "stopper", ...p } return { id: "partoling", add() { const uTrun = (obj, col) => { const l = col.isLeft() && currentDirection < 0 const r = col.isRight() && currentDirection > 0 const bt = !col.isTop() && !col.isBottom() if ((l || r) && bt && !obj.is("player") && !obj.is("projectile")) { const targetDirection = currentDirection > 0 ? -1 : 1 if (obj.is("stopper")) tween(currentDirection, targetDirection, param.uTrunDelay, (v) => (currentDirection = v), easings.easeInSin) else tween(0, targetDirection, param.startDelay, (v) => (currentDirection = v), easings.easeInSin) this.trigger("facing", vec2(targetDirection, 0)) } } let currentDirection = param.direction this.onCollide("body", (obj, col) => uTrun(obj, col)) this.onCollide("stopper", (obj, col) => uTrun(obj, col)) this.on("respawn", () => { currentDirection = param.direction this.trigger("facing", vec2(param.direction, 0)) }) if (param.crossEachOther) this.collisionIgnore.push("partoling") onUpdate(() => { this.move(param.speed * currentDirection, 0) }) this.trigger("facing", vec2(param.direction, 0)) }, /* changeDirection(obj, col) { const l = col.isLeft() && this.direction.x < 0 const r = col.isRight() && this.direction.x > 0 if ((l || r) && !obj.is("player") && !obj.is("projectile")) { const n = this.direction.x > 0 ? -1 : 1 if (obj.is("stopper")) tween(this.direction.x, n, param.uTrunDelay, (v) => (this.direction.x = v), easings.easeInSin) else tween(0, n, param.startDelay, (v) => (this.direction.x = v), easings.easeInSin) } this.trigger("facing", this.direction) }, update() { this.move(param.speed * this.direction.x, 0) if (this.direction.len() > 0.1) this.facing = this.direction.unit().x }, */ }}