Debian Codegraph @

tests/resources/userdiff/after/file.javascript

raw
1 define(function(require, exports, module) {
2 module.exports = Player;
3
4 var Key = require("./key")
5 , Direction = require("./direction");
6
7 function Player(game) {
8 this.game = game;
9
10 this.image = new Image("./assets/fighter.png");
11 this.game.resources.add(this.image);
12
13 this.x = 0;
14 this.y = 0;
15
16 this.pixelX = 10;
17 this.pixelY = 10;
18
19 this.animationStep = 0;
20 }
21
22 Player.prototype.update = function() {
23 if (!this.isWalking()) {
24 this.handleInput();
25 }
26
27 if (this.isWalking()) {
28 // Increase the animation step.
29 this.animationStep = ++this.animationStep % 60;
30
31 if (this.x * 32 > this.pixelX) {
32 this.pixelX++;
33 } else if (this.x * 32 < this.pixelX) {
34 this.pixelX--;
35 }
36
37 if (this.y * 32 > this.pixelY) {
38 this.pixelY++;
39 } else if (this.y * 32 < this.pixelY) {
40 this.pixelY--;
41 }
42 } else {
43 // Reset the animation step.
44 this.animationStep = 0;
45 }
46 };
47
48 Player.prototype.handleInput = function() {
49 var keyboard = this.game.keyboard, finalAction, action, inputs = {
50 'moveDown': keyboard.isDown(Key.DOWN),
51 'moveUp': keyboard.isDown(Key.UP),
52 'moveLeft': keyboard.isDown(Key.LEFT),
53 'moveRight': keyboard.isDown(Key.RIGHT)
54 };
55
56 for (action in inputs) {
57 if (inputs[action]) {
58 if (!finalAction || inputs[finalAction] < inputs[action]) {
59 finalAction = action;
60 }
61 }
62 }
63
64 this[finalAction] && this[finalAction]();
65 };
66
67 Player.prototype.isWalking = function() {
68 return this.x * 32 != this.pixelX || this.y * 32 != this.pixelY;
69 };
70
71 Player.prototype.moveDown = function() {
72 this.y += 1;
73 this.direction = Direction.DOWN;
74 };
75
76 Player.prototype.moveUp = function() {
77 this.y -= 1;
78 this.direction = Direction.UP;
79 };
80
81 Player.prototype.moveLeft = function() {
82 this.x -= 5;
83 this.direction = Direction.LEFT;
84 };
85
86 Player.prototype.moveRight = function() {
87 this.x += 1;
88 this.direction = Direction.RIGHT;
89 };
90
91 Player.prototype.draw = function(context) {
92 var offsetX = Math.floor(this.animationStep / 15) * 32, offsetY = 0;
93
94 switch(this.direction) {
95 case Direction.UP:
96 offsetY = 48 * 3;
97 break;
98 case Direction.RIGHT:
99 offsetY = 48 * 2;
100 break;
101 case Direction.LEFT:
102 offsetY = 48;
103 break;
104 }
105
106 context.drawImage(this.image.data, offsetX, offsetY, 32, 48, this.pixelX, this.pixelY, 32, 48);
107 };
108 });