Skip to content

Commit

Permalink
Enemy 1초마다 등장 & 화면 바깥에서 랜덤하게 등장
Browse files Browse the repository at this point in the history
  • Loading branch information
choar816 committed May 31, 2022
1 parent 5e4cf09 commit 3af86d1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/scenes/PlayingScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Phaser from 'phaser';
import Config from "../Config";
import Player, { Direction } from '../characters/Player';
import Enemy from '../characters/Enemy';
import { getRandomPosition } from '../utils/math';

export default class PlayingScene extends Phaser.Scene {
constructor() {
Expand Down Expand Up @@ -34,6 +35,7 @@ export default class PlayingScene extends Phaser.Scene {
// enemy
this.m_enemies = this.physics.add.group();
this.m_enemies.add(new Enemy(this, Config.width / 2 - 200, Config.height / 2 - 200, "bat", "bat_anim", 10));
this.addEnemy("bat", "bat_anim", 10);

// collisions
this.physics.add.overlap(this.m_attacks, this.m_enemies, (attack, enemy) => {
Expand Down Expand Up @@ -71,4 +73,15 @@ export default class PlayingScene extends Phaser.Scene {
this.m_player.move(Direction.Down);
}
}

addEnemy(enemyTexture, enemyAnim, enemyHp) {
this.time.addEvent({
delay: 1000,
callback: () => {
let [x, y] = getRandomPosition(this.m_player.x, this.m_player.y);
this.m_enemies.add(new Enemy(this, x, y, enemyTexture, enemyAnim, enemyHp));
},
loop: true,
});
}
}
14 changes: 14 additions & 0 deletions src/utils/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Config from "../Config";

/**
* (x, y)로부터 r만큼 랜덤하게 떨어진(각도랜덤) 좌표 반환
* @param {Number} x
* @param {Number} y
*/
export function getRandomPosition(x, y) {
const randRad = Math.random() * Math.PI * 2;
const _r = Math.sqrt(Config.width*Config.width + Config.height*Config.height) / 2;
const _x = x + (_r * Math.cos(randRad));
const _y = y + (_r * Math.sin(randRad));
return [_x, _y];
}

0 comments on commit 3af86d1

Please sign in to comment.