-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.js
72 lines (69 loc) · 2.24 KB
/
input.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
window.Input = (function() {
var maxSpeed = 12,
acceleration = 1,
friction = 1,
leftKey = Keyboard.Keys.LEFT,
rightKey = Keyboard.Keys.RIGHT,
jumpKey = Keyboard.Keys.Z,
grabKey = Keyboard.Keys.X,
doorKey = Keyboard.Keys.C;
return {
runSystem: function(model) {
model.getEntities().filter(function(entity) {
return entity.player;
}).forEach(function(playerEntity) {
if(Keyboard.isKeyPressed(leftKey)) {
playerEntity.dx = Math.max(playerEntity.dx - acceleration, -maxSpeed);
}
if(Keyboard.isKeyPressed(rightKey)) {
playerEntity.dx = Math.min(playerEntity.dx + acceleration, maxSpeed);
}
if(Keyboard.isKeyPressed(rightKey) === Keyboard.isKeyPressed(leftKey)) {
if(playerEntity.dx > 0) {
playerEntity.dx = Math.max(0, playerEntity.dx - friction);
} else {
playerEntity.dx = Math.min(0, playerEntity.dx + friction);
}
}
if(Keyboard.isKeyPressedSinceStartOfLastFrame(jumpKey)) {
if(playerEntity.dy === 0 && playerEntity.landed) {
playerEntity.dy = 20;
playerEntity.landed = false;
}
}
// pick up item
if(Keyboard.isKeyPressedSinceStartOfLastFrame(grabKey)) {
if(playerEntity.heldEntity === undefined) {
// grab
model.getEntities().filter(function(entity) {
return entity.grabbable;
}).some(function(grabbableEntity) {
if(Entity.overlapsEntity(playerEntity, grabbableEntity)) {
playerEntity.heldEntity = grabbableEntity;
playerEntity.heldEntity.collisionType = "held";
playerEntity.heldEntity.x = playerEntity.x;
playerEntity.heldEntity.y = playerEntity.y;
}
// stops trying to grab others once it grabs something
return playerEntity.heldEntity !== undefined;
});
} else {
// drop
playerEntity.heldEntity.collisionType = "actor";
playerEntity.heldEntity.dy = 0;
playerEntity.heldEntity = undefined;
}
}
if(Keyboard.isKeyPressedSinceStartOfLastFrame(doorKey)) {
model.getEntities().filter(function(entity) {
return entity.door;
}).forEach(function(doorEntity) {
if(Entity.overlapsEntity(playerEntity, doorEntity)) {
model.loadRoom(doorEntity.doorDestination);
}
})
}
});
}
};
}());