После этого ещё был небольшой баг с тем что неправильно определялась позиция касания
Это из-за моего скейла в 0.7 того самого из-за которого мне пришлось переписывать код движка, но в этот раз получилось исправить все в игре, просто поделил позицию на gscale (0.7)
Все заработало как надо
И я добавил кнопку прыжка
Это было просто и с первого раза она заработала как надо
Затем сделал так чтобы кнопки измеряли позицию и размер при повороте это видно на видео в предыдущем посте
И наконец переместил кнопки меню и состояния вверх по центру чтобы случайно не задевать их при прыжке
На этом все😎
Тот самый код для управления свайпами:
export default function swipeComp() {
let left
let right
let up
let touches = []
return {
id: "swipeComp",
require: [],
direction: vec2(0),
move: false,
isAction: false,
action: false,
add() {
left = this.add([
pos(0,height()),
rect(width() / 2, height()/2),
color(RED),
opacity(0),
fixed(),
area(),
anchor("botleft")
])
// left.hidden = true
right = this.add([
pos(width() / 2, height()),
rect(width() / 2, height()/2),
color(GREEN),
opacity(0),
fixed(),
area(),
anchor("botleft")
])
// right.hidden = true
up = this.add([
pos(),
rect(width(), height()/2),
color(BLUE),
opacity(0),
fixed(),
area(),
])
onResize(() => {
left.pos = vec2(0,height())
left.width = width() / 2
left.height = height() / 2
right.pos = vec2(width() / 2, height())
right.width = width() / 2
right.height = height() / 2
up.pos = vec2(0)
up.width = width()
up.height = height() / 2
})
left.onClick(() => {
this.parent.trigger("startMove")
})
right.onClick(() => {
this.parent.trigger("startMove")
})
let lastTouchPos = null
onTouchStart((pos, t) => {
lastTouchPos = mousePos()
touches.push(t)
})
onTouchMove((pos, t) => {
touches.splice(t.identifier, 1, t)
// debug.log(touches[0].screenX)
this.isAction = true
if (mousePos().dist(lastTouchPos) > 10) {
this.parent.trigger("action")
if (!this.action) {
this.parent.trigger("startAction")
this.action = true
}
this.direction = mousePos().sub(lastTouchPos).unit()
}
lastTouchPos = mousePos()
})
onTouchEnd((pos, t) => {
touches = touches.filter((elem) => elem.identifier !== t.identifier)
if (touches.length <= 0) this.parent.trigger("endMove")
lastTouchPos = null
this.direction = vec2(0)
this.action = false
this.move = false
this.isAction = false
})
},
update() {
if (touches.length <= 0) return
this.parent.trigger("input", this.direction)
if (this.isAction) return
for (const t of touches) {
if (left.hasPoint(vec2(t.clientX / gscale,
t.clientY/ gscale))) {
this.direction.x = -1
left.opacity = 0.2
}
else {
left.opacity = 0
}
if (right.hasPoint(vec2(t.clientX/ gscale,
t.clientY/ gscale))) {
this.direction.x = 1
right.opacity = 0.2
}
else {
right.opacity = 0
}
if (up.hasPoint(vec2(t.clientX/ gscale,
t.clientY/ gscale))) {
this.direction.y = -1
up.opacity = 0.2
}
else {
up.opacity = 0
}
}
},
}
}Завтра надеюсь добавлю переключение управления в настройках между свайпами и джойстиком
Сегодня я превзошел самого себя) впрочем как обычно