1     /**
2         A class to represent the player on the screen
3         @author <a href="mailto:matthewcasperson@gmail.com">Matthew Casperson</a>
4         @class
5     */
6     function Player()
7 {
8 /** The speed that the player moves at 9 @type Number 10 */ 11 this.speed = 75;
12 /** True if the player is moving left, false otherwise 13 @type Boolean 14 */ 15 this.left = false;
16 /** True if the player is moving right, false otherwise 17 @type Boolean 18 */ 19 this.right = false;
20 /** A reference to the level object 21 @type Level 22 */ 23 this.level = null;
24 25 /** 26 Initialises this object 27 */ 28 this.startupPlayer = function(level)
29 {
30 this.startupAnimatedGameObject(g_idle_left, 300, 400 - 48 - 48, 4, 6, 20);
31 this.level = level;
32 return this;
33 }
34 35 /** 36 Called when a key is pressed 37 @param event Event Object 38 */ 39 this.keyDown = function(event)
40 {
41 var updateRequired = false;
42 43 // left 44 if (event.keyCode == 37 && !this.left)
45 {
46 this.left = true;
47 updateRequired = true;
48 }
49 // right 50 if (event.keyCode == 39 && !this.right)
51 {
52 this.right = true;
53 updateRequired = true;
54 }
55 56 if (updateRequired)
57 this.updateAnimation();
58 59 }
60 61 /** 62 Called when a key is pressed 63 @param event Event Object 64 */ 65 this.keyUp = function(event)
66 {
67 // left 68 if (event.keyCode == 37)
69 {
70 this.left = false;
71 this.setAnimation(g_idle_left, 6, 20);
72 }
73 // right 74 if (event.keyCode == 39)
75 {
76 this.right = false;
77 this.setAnimation(g_idle_right, 6, 20);
78 }
79 80 this.updateAnimation();
81 }
82 83 /** 84 Updates the current animation depending on the movement 85 of the player. This accounts for the fact that both 86 the left and right arrow keys can be pressed at the 87 same time. 88 */ 89 this.updateAnimation = function()
90 {
91 if (this.right && this.left)
92 this.setAnimation(g_idle_left, 6, 20);
93 else if (this.right)
94 this.setAnimation(g_run_right, 12, 20);
95 else if (this.left)
96 this.setAnimation(g_run_left, 12, 20);
97 }
98 99 /** 100 Updates the object 101 @param dt The time since the last frame in seconds 102 @param context The drawing context 103 @param xScroll The global scrolling value of the x axis 104 @param yScroll The global scrolling value of the y axis 105 */ 106 this.update = function (/**Number*/ dt, /**CanvasRenderingContext2D*/context, /**Number*/ xScroll, /**Number*/ yScroll)
107 {
108 if (this.left)
109 this.x -= this.speed * dt;
110 if (this.right)
111 this.x += this.speed * dt;
112 113 // XOR operation 114 // only test for a collision if the player is moving left or right (and not trying to do both at 115 // the same time) 116 if ((this.right || this.left) && !(this.left && this.right))
117 {
118 // this will be true until the player is no longer colliding 119 var collision = false;
120 // the player may have to be pushed back through several block stacks (especially if the 121 // frame rate is very slow) 122 do 123 {
124 // the current position of the player (test the left side if running left 125 // and the right side if running right) 126 var xPos = this.left ? this.x : this.x + this.frameWidth;
127 // the index of stack of blocks that the player is standing on/in 128 var currentBlock = this.level.currentBlock(xPos);
129 // the height of the stack of blocks that the player is standing on/in 130 var groundHeight = this.level.groundHeight(currentBlock);
131 // the height of the player (we need the height from the ground up, 132 // whereas the this.y value reprents the position of the player 133 // from the "sky" down). 134 var playerHeight = context.canvas.height - (this.y + this.image.height);
135 // if the player is not higher than the stack of blocks, it must be colliding 136 if (playerHeight < groundHeight)
137 {
138 collision = true;
139 // we are moving right, so push the player left 140 if (this.right)
141 this.x = this.level.blockWidth * currentBlock - this.frameWidth - 1;
142 // we are moving left, push the player right 143 else 144 this.x = this.level.blockWidth * (currentBlock + 1);
145 }
146 else 147 {
148 collision = false;
149 }
150 } while (collision)
151 }
152 153 // keep the player bound to the level 154 if (this.x > this.level.blocks.length * this.level.blockWidth - this.frameWidth - 1)
155 this.x = this.level.blocks.length * this.level.blockWidth - this.frameWidth - 1;
156 if (this.x > context.canvas.width - this.frameWidth + xScroll)
157 g_GameObjectManager.xScroll = this.x - (context.canvas.width - this.frameWidth);
158 // modify the xScroll value to keep the player on the screen 159 if (this.x < 0)
160 this.x = 0;
161 if (this.x < xScroll)
162 g_GameObjectManager.xScroll = this.x;
163 164 // modify the xScroll value to keep the player on the screen 165 if (this.x > context.canvas.width - this.frameWidth + xScroll)
166 g_GameObjectManager.xScroll = this.x - (context.canvas.width - this.frameWidth);
167 if (this.x < xScroll)
168 g_GameObjectManager.xScroll = this.x;
169 }
170 }
171 172 Player.prototype = new AnimatedGameObject;
Top