Commit 1c1d7c4e authored by wildfirecode13's avatar wildfirecode13

init

parent 7c5b7fa5
export default class Hero extends FYGE.Sprite {
constructor() {
super();
this.texture = FYGE.Texture.fromUrl('//yun.duiba.com.cn/spark/assets/enemy2_fly1.0f4acd3728e90763d98a7328a5a32dbc78ec204e.png')
}
}
\ No newline at end of file
import Bullet from "./Bullet";
import Enemy from "./Enemy";
import Hero from "./Hero";
import MovableManager from "./MovableManager";
import Vector2 from "./Vector2";
export function addGame(stage: FYGE.Stage) {
const movableManager = new MovableManager(stage);//创建管理器
const bullet = stage.addChild(new Bullet());//创建子弹并设置相关属性
bullet.position.set(0, 1000);
bullet.velocity = new Vector2(0, -8)
const enemy = stage.addChild(new Enemy());//创建敌机并设置相关属性
enemy.velocity = new Vector2(4, 2);
movableManager.add(bullet);
movableManager.add(enemy);
const hero = stage.addChild(new Hero());
}
\ No newline at end of file
export default class DragDropEvent extends FYGE.Event {
static DRAG_START = 'DRAG_START';
static DRAG_END = 'DRAG_END';
}
\ No newline at end of file
import DragDropable from "./DragDropable";
import DragDropEvent from "./DragDropEvent";
//管理一个拖放实例
export default class DragDropManager extends FYGE.EventDispatcher {
//初始化拖放对象事件
public add(item: DragDropable) {
item.addEventListener('DROP', this.onDisplayItemDrop, this)
item.addEventListener(FYGE.MouseEvent.MOUSE_DOWN, this.onMouseDown, this)
item.once(FYGE.Event.REMOVED_FROM_STAGE, this.remove, this)
}
remove = (event: FYGE.Event) => {
const item = event.target;
item.removeEventListener('DROP', this.onDisplayItemDrop, this)
item.removeEventListener(FYGE.MouseEvent.MOUSE_DOWN, this.onMouseDown, this)
}
private onMouseDown(event: FYGE.MouseEvent) {
const item: DragDropable = event.target;
if (!item.checkCanDragDrop()) return;
item.startDrag(event);
this.dispatchEvent(DragDropEvent.DRAG_START, item);
}
private onDisplayItemDrop(e: FYGE.Event) {
const drag: DragDropable = e.target;
drag.mouseEnable = false;
const dragParent = drag.parent;
const drop: DragDropable = dragParent.hitTestPoint(new FYGE.Point(e.data.x / 2, e.data.y / 2), true);
drag.mouseEnable = true;
if (drop) {
drop.onDrop(drag);
}
drag.onDragEnd(drop);
this.dispatchEvent(DragDropEvent.DRAG_END, { drop, drag });
}
}
\ No newline at end of file
export default class DragDropable extends FYGE.Sprite {
//图片起始位置
private _originPos: FYGE.Point;
public get originPos() { return this._originPos }
//鼠标按下起始点
private _startPoint: FYGE.Point;
startDrag = (event: FYGE.MouseEvent) => {
this.stage.addEventListener(FYGE.MouseEvent.MOUSE_MOVE, this.onMouseMove, this);
this.stage.once(FYGE.MouseEvent.MOUSE_UP, this.onMouseUp, this);
this._originPos = new FYGE.Point(this.x, this.y);
this._startPoint = new FYGE.Point(event.stageX, event.stageY);
}
private onMouseUp(event: FYGE.MouseEvent) {
this.stage.removeEventListener(FYGE.MouseEvent.MOUSE_MOVE, this.onMouseMove, this);
this.dispatchEvent('DROP', this.getDropPoint(event));
}
private onMouseMove(event: FYGE.MouseEvent) {
//鼠标当前位置
const currentPoint = { x: event.stageX, y: event.stageY };
//鼠标按下点到鼠标当前点的偏移量
let mouseOffsetX = currentPoint.x - this._startPoint.x;
let mouseOffsetY = currentPoint.y - this._startPoint.y;
this.x = this._originPos.x + mouseOffsetX;
this.y = this._originPos.y + mouseOffsetY;
}
checkCanDragDrop() {
return true;
}
public onDragEnd(drop: DragDropable) {
throw new Error("Method not implemented.");
}
protected getDropPoint(event: FYGE.MouseEvent) {
throw new Error('必须重写此方法')
}
public onDrop = (drag: DragDropable): void => {
throw new Error('必须重写此方法')
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment