按键分组与辅助键检测

This commit is contained in:
unanmed 2023-08-09 12:27:57 +08:00
parent e6148ae7fe
commit 1673eb7a4b
4 changed files with 131 additions and 57 deletions

View File

@ -87,3 +87,9 @@ dam4.png ---- 存档 59
[] 优化资源分离,音乐放到 bgm 目录下 [] 优化资源分离,音乐放到 bgm 目录下
[] 一次性道具拾取与清怪 [] 一次性道具拾取与清怪
[] 重构数据统计 [] 重构数据统计
[] 优化路径显示,瞬移可以闪一下再熄灭
[] 勇士身上显示攻防血
[] 优化地图拖动
[] 楼层转换假如随机小贴士
[] ui 中如果元素发生改变,那么做出背景亮一下再熄灭的效果
[] 双击怪物手册拐点可以直接在拖动条上定位

View File

@ -4,6 +4,7 @@ import { EventEmitter } from './common/eventEmitter';
import { loading, readyAllResource } from './loader/load'; import { loading, readyAllResource } from './loader/load';
import { ResourceStore, ResourceType } from './loader/resource'; import { ResourceStore, ResourceType } from './loader/resource';
import { GameEvent, hook } from './main/game'; import { GameEvent, hook } from './main/game';
import { GameStorage } from './main/storage';
import { resolvePlugin } from './plugin'; import { resolvePlugin } from './plugin';
interface AncTePlugin { interface AncTePlugin {
@ -41,6 +42,7 @@ export interface AncTe {
plugin: AncTePlugin; plugin: AncTePlugin;
game: { game: {
hook: EventEmitter<GameEvent>; hook: EventEmitter<GameEvent>;
storage: GameStorage<any>[];
}; };
} }
@ -53,7 +55,8 @@ function ready() {
// @ts-ignore // @ts-ignore
plugin: {}, plugin: {},
game: { game: {
hook hook,
storage: GameStorage.list
} }
}; };

View File

@ -1,5 +1,5 @@
import { KeyCode } from '../../../plugin/keyCodes'; import { KeyCode } from '../../../plugin/keyCodes';
import { deleteWith } from '../../../plugin/utils'; import { deleteWith, generateBinary, has } from '../../../plugin/utils';
import { EmitableEvent, EventEmitter } from '../../common/eventEmitter'; import { EmitableEvent, EventEmitter } from '../../common/eventEmitter';
import { GameStorage } from '../storage'; import { GameStorage } from '../storage';
@ -16,13 +16,22 @@ interface HotkeyData {
ctrl?: boolean; ctrl?: boolean;
alt?: boolean; alt?: boolean;
shift?: boolean; shift?: boolean;
group?: string;
func: (code: KeyCode, e: KeyboardEvent) => any; func: (code: KeyCode, e: KeyboardEvent) => any;
} }
interface GroupInfo {
name: string;
includes: string[];
}
type RegisterData = Omit<HotkeyData, 'id' | 'key' | 'name'>;
export class Hotkey extends EventEmitter<HotkeyEvent> { export class Hotkey extends EventEmitter<HotkeyEvent> {
keyMap: Map<KeyCode, HotkeyData[]> = new Map(); keyMap: Map<KeyCode, HotkeyData[]> = new Map();
list: Record<string, HotkeyData> = {}; list: Record<string, HotkeyData> = {};
storage: GameStorage<Record<string, KeyCode>>; storage: GameStorage<Record<string, KeyCode>>;
groups: Record<string, GroupInfo> = {};
constructor(id: string) { constructor(id: string) {
super(); super();
@ -33,9 +42,10 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
* *
* @param data * @param data
*/ */
register(id: string, data: Omit<HotkeyData, 'id' | 'key'>) { register(id: string, name: string, data: RegisterData) {
const key = { const key = {
id, id,
name,
key: this.storage.getValue(id, data.defaults), key: this.storage.getValue(id, data.defaults),
...data ...data
}; };
@ -75,7 +85,48 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
*/ */
emitKey(key: KeyCode, e: KeyboardEvent): any[] { emitKey(key: KeyCode, e: KeyboardEvent): any[] {
this.emit('emit', key, e); this.emit('emit', key, e);
return this.getData(key).map(v => v.func(key, e)); return this.getData(key).map(v => {
const assist = generateBinary([e.ctrlKey, e.altKey, e.shiftKey]);
const need = generateBinary([!!v.ctrl, !!v.alt, !!v.shift]);
if (assist & need) {
v.func(key, e);
}
});
}
/**
*
* @param id id
* @param name
* @param ids
*/
group(id: string, name: string, ids: string[]) {
this.groups[id] = {
name,
includes: ids
};
ids.forEach(v => {
const data = this.list[v];
if (has(data.group)) {
deleteWith(this.groups[data.group].includes, v);
}
data.group = id;
});
return this;
}
/**
*
* @param id id
* @param name
*/
groupRest(id: string, name: string) {
const rest = Object.values(this.list)
.filter(v => !has(v.group))
.map(v => v.id);
this.group(id, name, rest);
return this;
} }
private ensureKey(key: KeyCode) { private ensureKey(key: KeyCode) {
@ -89,133 +140,137 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
export const hotkey = new Hotkey('gameKey'); export const hotkey = new Hotkey('gameKey');
hotkey hotkey
.register('book', { .register('book', '怪物手册', {
name: '怪物手册',
defaults: KeyCode.KeyX, defaults: KeyCode.KeyX,
func: () => {} func: () => {}
}) })
.register('save', { .register('save', '存档界面', {
name: '存档界面',
defaults: KeyCode.KeyS, defaults: KeyCode.KeyS,
func: () => {} func: () => {}
}) })
.register('load', { .register('load', '读档界面', {
name: '读档界面',
defaults: KeyCode.KeyD, defaults: KeyCode.KeyD,
func: () => {} func: () => {}
}) })
.register('undo1', { .register('undo1', '撤回', {
name: '撤回',
defaults: KeyCode.KeyA, defaults: KeyCode.KeyA,
func: () => {} func: () => {}
}) })
.register('undo2', { .register('undo2', '撤回 手机端', {
name: '撤回',
defaults: KeyCode.Digit5, defaults: KeyCode.Digit5,
func: () => {} func: () => {}
}) })
.register('redo1', { .register('redo1', '重做', {
name: '重做',
defaults: KeyCode.KeyW, defaults: KeyCode.KeyW,
func: () => {} func: () => {}
}) })
.register('redo2', { .register('redo2', '重做 手机端', {
name: '重做',
defaults: KeyCode.Digit6, defaults: KeyCode.Digit6,
func: () => {} func: () => {}
}) })
.register('toolbox', { .register('toolbox', '道具栏', {
name: '道具栏',
defaults: KeyCode.KeyT, defaults: KeyCode.KeyT,
func: () => {} func: () => {}
}) })
.register('equipbox', { .register('equipbox', '装备栏', {
name: '装备栏',
defaults: KeyCode.KeyQ, defaults: KeyCode.KeyQ,
func: () => {} func: () => {}
}) })
.register('fly', { .register('fly', '楼层传送', {
name: '楼层传送',
defaults: KeyCode.KeyG, defaults: KeyCode.KeyG,
func: () => {} func: () => {}
}) })
.register('turn', { .register('turn', '勇士转向', {
name: '勇士转向',
defaults: KeyCode.KeyZ, defaults: KeyCode.KeyZ,
func: () => {} func: () => {}
}) })
.register('getNext1', { .register('getNext1', '轻按', {
name: '轻按',
defaults: KeyCode.Space, defaults: KeyCode.Space,
func: () => {} func: () => {}
}) })
.register('getNext2', { .register('getNext2', '轻按 手机端', {
name: '轻按',
defaults: KeyCode.Digit7, defaults: KeyCode.Digit7,
func: () => {} func: () => {}
}) })
.register('menu', { .register('menu', '菜单', {
name: '菜单',
defaults: KeyCode.Escape, defaults: KeyCode.Escape,
func: () => {} func: () => {}
}) })
.register('replay', { .register('replay', '录像回放', {
name: '录像回放',
defaults: KeyCode.KeyR, defaults: KeyCode.KeyR,
func: () => {} func: () => {}
}) })
.register('restart', { .register('restart', '开始菜单', {
name: '开始菜单',
defaults: KeyCode.KeyN, defaults: KeyCode.KeyN,
func: () => {} func: () => {}
}) })
.register('shop', { .register('shop', '快捷商店', {
name: '快捷商店',
defaults: KeyCode.KeyV, defaults: KeyCode.KeyV,
func: () => {} func: () => {}
}) })
.register('statistics', { .register('statistics', '数据统计', {
name: '数据统计',
defaults: KeyCode.KeyB, defaults: KeyCode.KeyB,
func: () => {} func: () => {}
}) })
.register('viewMap1', { .register('viewMap1', '浏览地图', {
name: '浏览地图',
defaults: KeyCode.PageUp, defaults: KeyCode.PageUp,
func: () => {} func: () => {}
}) })
.register('viewMap2', { .register('viewMap2', '浏览地图', {
name: '浏览地图',
defaults: KeyCode.PageDown, defaults: KeyCode.PageDown,
func: () => {} func: () => {}
}) })
.register('comment', { .register('comment', '评论区', {
name: '评论区',
defaults: KeyCode.KeyP, defaults: KeyCode.KeyP,
func: () => {} func: () => {}
}) })
.register('mark', { .register('mark', '标记怪物', {
name: '标记怪物',
defaults: KeyCode.KeyM, defaults: KeyCode.KeyM,
func: () => {} func: () => {}
}) })
.register('skillTree', { .register('skillTree', '技能树', {
name: '技能树',
defaults: KeyCode.KeyJ, defaults: KeyCode.KeyJ,
func: () => {} func: () => {}
}) })
.register('desc', { .register('desc', '百科全书', {
name: '百科全书',
defaults: KeyCode.KeyH, defaults: KeyCode.KeyH,
func: () => {} func: () => {}
}) })
.register('special', { .register('special', '鼠标位置怪物属性', {
name: '鼠标位置怪物属性',
defaults: KeyCode.KeyE, defaults: KeyCode.KeyE,
func: () => {} func: () => {}
}) })
.register('critical', { .register('critical', '鼠标位置怪物临界', {
name: '鼠标位置怪物临界',
defaults: KeyCode.KeyC, defaults: KeyCode.KeyC,
func: () => {} func: () => {}
}); })
.group('action', '游戏操作', [
'save',
'load',
'undo1',
'undo2',
'redo1',
'redo2',
'turn',
'getNext1',
'getNext2',
'mark'
])
.group('view', '快捷查看', [
'book',
'toolbox',
'equipbox',
'fly',
'menu',
'replay',
'shop',
'statistics',
'viewMap1',
'viewMap2',
'skillTree',
'desc',
'special',
'critical'
])
.group('system', '系统按键', ['comment'])
.groupRest('unClassed', '未分类按键');

View File

@ -286,3 +286,13 @@ export async function triggerFullscreen(full: boolean) {
}); });
} }
} }
export function generateBinary(arr: boolean[]) {
let num = 0;
arr.forEach((v, i) => {
if (v) {
num += 1 << i;
}
});
return num;
}