오른쪽 방향키로 이동하는 이벤트를, 왼쪽 WASD를 통해 이동이 가능하도록 하였다. 사용자가 마우스도 사용해야하는데, 오른쪽 방향키로 이동하게 하면, 불편이 있을거라 판단했다. 구현하는 김에, 이후 키보드 이벤트를 더욱 등록하기 편하도록 cursor()
이벤트에서 keyboard()
이벤트로 바꾸는 작업도 진행하였다.
// 방향기 설정
this.inputKeys = this.scene.input.keyboard.addKeys({
up: Phaser.Input.Keyboard.KeyCodes.W,
down: Phaser.Input.Keyboard.KeyCodes.S,
left: Phaser.Input.Keyboard.KeyCodes.A,
right: Phaser.Input.Keyboard.KeyCodes.D,
// shift: Phaser.Input.Keyboard.KeyCodes.SHIFT,
// riding: Phaser.Input.Keyboard.KeyCodes.C,
});
캐릭터가 탈 것을 넣는 기능을 넣어보려고 한다. 캐릭터가 일단은 도사이기 때문에, 근두운을 타는 모습을 상상하며 코드를 작성하였다. 결과적으로 기능은 구현되었으나, 도사와 어울리는 탈 것을 찾을 수 없었고, 이미지 오브젝트가 캐릭터을 덮는 듯한 결과가 나와서, 보기 좋지도 않았다.
도사라면 어떤 걸 보여주는게 나을까 생각하던 중, skill을 사용하는 것이 오히려 임팩트가 있을 거 같다고 판단하
여, 탈 것 코드를 스킬 기능으로 바꾸어 만들어보기로 하였다.
isSkill
과 스킬 이벤트 중 임을 보여주는 skillIcon
을 통해 관리한다.기존의 shift 키를 통해 움직이는 빠른 무빙을 스킬로 바꿔보았다. isHaste
와 hasteIcon
의 관리하에 스킬 흐름이 관리되며, isHaste
상태 시, 캐릭터의 이동이 한층 빨라진다.
this.scene.input.keyboard.on('keydown-' + 'X', this.skilllevitation.bind(this));
update() {
let speed: number;
if (this.isHaste) {
speed = 750;
} else {
speed = 200;
}
}
skillHaste() {
if (this.isHaste) {
this.hasteIcon?.destroy();
this.isHaste = false;
} else {
this.hasteIcon = new Phaser.GameObjects.Sprite(
this.scene,
this.me.x,
this.me.y,
'items',
61,
);
// this.me.y -= 10;
this.hasteIcon.y -= 45;
this.scene.add.existing(this.hasteIcon);
this.isHaste = true;
}
}
스킬 사용 시 바다의 collider
무시하고 이동하게 하는, 레비테이션 (공중도약) 스킬을 생성했다.
collider
를 추가하는 경우, 모든 collider
는 Phaser.Scene.Physics.world.colliders
안에서 관리가 된다. 스킬을 시전하는 경우 collider
를 지우고, 스킬을 쓰는 경우, 해당 colilder
를 추가하는 형태로 진행했다.
똑같은 collider
가 계속 쌓이지 않도록 name
이라는 속성을 지정하였다. 만약 collider.name
이 world
인 경우 world
와 일치하는 데이터를 find()
하고 해당 값이 없을 경우에만 collider
를 생성하게했다.
이러면 어떻게 생성하드라도, 계속 collider
가 중복이 되지 않을테니, 삭제할 때는 한개만 찾아 삭제해주면 정상적으로 작동한다.
skilllevitation() {
const temp = this.scene.physics.world.colliders;
if (this.isLevitation) {
this.levitationIcon?.destroy();
// ?.destroy();
this.isLevitation = false;
if (!temp.getActive().find(el => el.name == 'world'))
this.createColliderForWorldLayer();
// this.mana.increase();
// console.log(temp);
} else {
this.levitationIcon = new Phaser.GameObjects.Sprite(
this.scene,
this.me.x,
this.me.y,
'items',
56,
);
temp.remove(temp.getActive().filter(el => el.name == 'world')[0]);
// this.me.y -= 10;
this.levitationIcon.y -= 45;
// this.scene.physics.world.removeCollider();
this.scene.add.existing(this.levitationIcon);
this.isLevitation = true;
}
}
createColliderForWorldLayer() {
this.scene.physics.add.collider(this, this.worldLayer, player => {
if (!player.body.checkCollision.none) {
console.log('바다와 부딪힘');
}
}).name = 'world';
}
글은 나중에 큰 도움이 된다. 회고 밀리지 말자.
무한대로 스킬을 쓰면 재미없지 않은가, 그래서 마나(도력)을 만들어보려고 한다. 마나가 0이면 스킬을 사용하지 못하거나, 스킬 사용시 일정 마나가 소모되는 이벤트를 만들어보려고 한다.