2023-09-07 16:46:35 -05:00
|
|
|
var Input = {
|
|
|
|
setgame() { cmd(77); },
|
|
|
|
setnuke() { cmd(78); },
|
|
|
|
};
|
|
|
|
|
2023-09-11 17:09:21 -05:00
|
|
|
var Mouse = {
|
|
|
|
get pos() {
|
|
|
|
return cmd(45);
|
|
|
|
},
|
|
|
|
|
|
|
|
get screenpos() {
|
|
|
|
var p = this.pos;
|
|
|
|
p.y = Window.dimensions.y - p.y;
|
|
|
|
return p;
|
|
|
|
},
|
|
|
|
|
|
|
|
get worldpos() {
|
|
|
|
return screen2world(cmd(45));
|
|
|
|
},
|
|
|
|
|
|
|
|
disabled() {
|
|
|
|
cmd(46, 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
hidden() {
|
|
|
|
cmd(46, 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
normal() {
|
|
|
|
cmd(46, 0);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
var Keys = {
|
|
|
|
shift() {
|
|
|
|
return cmd(50, 340);// || cmd(50, 344);
|
|
|
|
},
|
|
|
|
|
|
|
|
ctrl() {
|
|
|
|
return cmd(50, 341);// || cmd(50, 344);
|
|
|
|
},
|
|
|
|
|
|
|
|
alt() {
|
|
|
|
return cmd(50, 342);// || cmd(50, 346);
|
|
|
|
},
|
|
|
|
|
|
|
|
super() {
|
|
|
|
return cmd(50, 343);// || cmd(50, 347);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-09-07 16:46:35 -05:00
|
|
|
Input.state2str = function(state) {
|
|
|
|
if (typeof state === 'string') return state;
|
|
|
|
switch (state) {
|
|
|
|
case 0:
|
|
|
|
return "down";
|
|
|
|
case 1:
|
|
|
|
return "pressed";
|
|
|
|
case 2:
|
|
|
|
return "released";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Input.print_pawn_kbm = function(pawn) {
|
|
|
|
if (!('inputs' in pawn)) return;
|
|
|
|
var str = "";
|
|
|
|
for (var key in pawn.inputs) {
|
2023-09-22 09:44:58 -05:00
|
|
|
if (!pawn.inputs[key].doc) continue;
|
2023-09-07 16:46:35 -05:00
|
|
|
str += `${key} | ${pawn.inputs[key].doc}\n`;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
|
|
|
|
Input.print_md_kbm = function(pawn) {
|
|
|
|
if (!('inputs' in pawn)) return;
|
|
|
|
|
|
|
|
var str = "";
|
|
|
|
str += "|control|description|\n|---|---|\n";
|
|
|
|
|
|
|
|
for (var key in pawn.inputs) {
|
|
|
|
str += `|${key}|${pawn.inputs[key].doc}|`;
|
|
|
|
str += "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
|
|
|
|
Input.has_bind = function(pawn, bind) {
|
|
|
|
return (typeof pawn.inputs?.[bind] === 'function');
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var Action = {
|
|
|
|
add_new(name) {
|
|
|
|
var action = Object.create(Action);
|
|
|
|
action.name = name;
|
|
|
|
action.inputs = [];
|
|
|
|
this.actions.push(action);
|
|
|
|
|
|
|
|
return action;
|
|
|
|
},
|
|
|
|
actions: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
/* May be a human player; may be an AI player */
|
|
|
|
var Player = {
|
|
|
|
players: [],
|
|
|
|
input(fn, ...args) {
|
|
|
|
this.pawns.forEach(x => x[fn]?.(...args));
|
|
|
|
},
|
|
|
|
|
2023-09-12 17:19:46 -05:00
|
|
|
mouse_input(type, ...args) {
|
2023-09-27 17:40:04 -05:00
|
|
|
for (var pawn of this.pawns.reversed()) {
|
2023-09-12 17:19:46 -05:00
|
|
|
if (typeof pawn.inputs?.mouse?.[type] === 'function') {
|
|
|
|
pawn.inputs.mouse[type].call(pawn,...args);
|
2023-09-13 16:49:22 -05:00
|
|
|
pawn.inputs.post?.call(pawn);
|
2023-09-12 17:19:46 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-09-14 12:49:29 -05:00
|
|
|
char_input(c) {
|
2023-09-27 17:40:04 -05:00
|
|
|
for (var pawn of this.pawns.reversed()) {
|
2023-09-14 12:49:29 -05:00
|
|
|
if (typeof pawn.inputs?.char === 'function') {
|
|
|
|
pawn.inputs.char.call(pawn, c);
|
|
|
|
pawn.inputs.post?.call(pawn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2023-09-07 16:46:35 -05:00
|
|
|
raw_input(cmd, state, ...args) {
|
2023-09-27 17:40:04 -05:00
|
|
|
for (var pawn of this.pawns.reversed()) {
|
2023-09-07 16:46:35 -05:00
|
|
|
if (typeof pawn.inputs?.any === 'function') {
|
|
|
|
pawn.inputs.any(cmd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!pawn.inputs?.[cmd]) continue;
|
|
|
|
|
|
|
|
var fn = null;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case 'pressed':
|
|
|
|
fn = pawn.inputs[cmd];
|
|
|
|
break;
|
|
|
|
case 'rep':
|
|
|
|
fn = pawn.inputs[cmd].rep ? pawn.inputs[cmd] : null;
|
|
|
|
break;
|
|
|
|
case 'released':
|
|
|
|
fn = pawn.inputs[cmd].released;
|
|
|
|
break;
|
|
|
|
case 'down':
|
|
|
|
fn = pawn.inputs[cmd].down;
|
|
|
|
}
|
|
|
|
|
2023-09-13 16:49:22 -05:00
|
|
|
if (typeof fn === 'function') {
|
2023-09-07 16:46:35 -05:00
|
|
|
fn.call(pawn, ... args);
|
2023-09-13 16:49:22 -05:00
|
|
|
pawn.inputs.post?.call(pawn);
|
2023-09-27 17:40:04 -05:00
|
|
|
return;
|
2023-09-13 16:49:22 -05:00
|
|
|
}
|
2023-09-07 16:46:35 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
uncontrol(pawn) {
|
|
|
|
this.players.forEach(function(p) {
|
|
|
|
p.pawns = p.pawns.filter(x => x !== pawn);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
obj_controlled(obj) {
|
|
|
|
for (var p in Player.players) {
|
|
|
|
if (p.pawns.contains(obj))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
create() {
|
|
|
|
var n = Object.create(this);
|
|
|
|
n.pawns = [];
|
|
|
|
n.gamepads = [];
|
|
|
|
n.control = function(pawn) { n.pawns.push_unique(pawn); };
|
|
|
|
n.uncontrol = function(pawn) { n.pawns = n.pawns.filter(x => x !== pawn); };
|
|
|
|
this.players.push(n);
|
|
|
|
return n;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (var i = 0; i < 4; i++) {
|
|
|
|
Player.create();
|
|
|
|
}
|