-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrols.js
146 lines (141 loc) · 3.37 KB
/
controls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Controls for the game.
*/
/* Key bindings */
const MOVE_RIGHT = "ArrowRight";
const MOVE_LEFT = "ArrowLeft";
const MOVE_UP = "ArrowUp";
const MOVE_DOWN = "ArrowDown";
const EXAMINE = "e";
const INVENTORY = "i";
const USE = "u";
const PICKUP = "p";
const CONTROLS = "c";
const DROP = "d";
const ATTACK = "f";
const EQUIPMENT = "q";
const REMOVE = "r";
const STATS = "s";
const PARTS = "m";
const BUILD = "b";
const TALK = "t";
const WORLD_MAP = "n";
/* Show the key bindings on the 'controls' list */
document.getElementById("examine-key").innerHTML = "'"+EXAMINE+"'";
document.getElementById("inventory-key").innerHTML = "'"+INVENTORY+"'";
document.getElementById("use-key").innerHTML = "'"+USE+"'";
document.getElementById("pickup-key").innerHTML = "'"+PICKUP+"'";
document.getElementById("controls-key").innerHTML = "'"+CONTROLS+"'";
document.getElementById("drop-key").innerHTML = "'"+DROP+"'";
document.getElementById("attack-key").innerHTML = "'"+ATTACK+"'";
document.getElementById("equipment-key").innerHTML = "'"+EQUIPMENT+"'";
document.getElementById("remove-key").innerHTML = "'"+REMOVE+"'";
document.getElementById("stats-key").innerHTML = "'"+STATS+"'";
document.getElementById("parts-key").innerHTML = "'"+PARTS+"'";
document.getElementById("build-key").innerHTML = "'"+BUILD+"'";
document.getElementById("talk-key").innerHTML = "'"+TALK+"'";
document.getElementById("world-map-key").innerHTML = "'"+WORLD_MAP+"'";
/* Holds the current action so control() will know what to do */
var actionExecuted;
/* Passes the direction pressed to the requesting function */
function passToAction(direction, player) {
switch(actionExecuted) {
case "examine":
player.examine(direction);
break;
case "utilItem":
utilItem(itemHolder, player, direction);
break;
case "attack":
player.attack(direction);
break;
case "pickup":
player.pickup(direction);
break;
case "build":
player.build(direction);
break;
case "talk":
player.talk(direction);
}
}
/* Passes the keydown event to the suitable function */
function control(event, player) {
var key = event.key;
switch (key) {
case MOVE_RIGHT:
if(movement == false) {
passToAction(MOVE_RIGHT, player);
movement = true;
} else {
player.move(event);
}
break;
case MOVE_LEFT:
if(movement == false) {
passToAction(MOVE_LEFT, player);
movement = true;
} else {
player.move(event);
}
break;
case MOVE_UP:
if(movement == false) {
passToAction(MOVE_UP, player);
movement = true;
} else {
player.move(event);
}
break;
case MOVE_DOWN:
if(movement == false) {
passToAction(MOVE_DOWN, player);
movement = true;
} else {
player.move(event);
}
break;
case EXAMINE:
player.examine();
break;
case INVENTORY:
toggleWindow("inventory", player);
break;
case USE:
player.use();
break;
case PICKUP:
player.pickup();
break;
case CONTROLS:
toggleWindow("controls");
break;
case DROP:
player.drop();
break;
case ATTACK:
player.attack();
break;
case EQUIPMENT:
toggleWindow("equipment");
break;
case REMOVE:
player.unequip(true);
break;
case STATS:
toggleWindow("extended-stats");
break;
case PARTS:
toggleWindow("parts");
break;
case BUILD:
player.build();
break;
case TALK:
player.talk();
break;
case WORLD_MAP:
toggleWindow("world-map-wrapper");
break;
}
}