-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.js
More file actions
341 lines (300 loc) · 12.5 KB
/
Copy pathplayer.js
File metadata and controls
341 lines (300 loc) · 12.5 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
* Player Class for AI Alchemist's Lair
* Based on the entity class, adds player-specific functionality
*/
import { Entity } from './entity.js';
import assetLoader from './assetLoader.js';
import { debug } from './utils.js';
class Player extends Entity {
constructor(x, y, width = 0.7, height = 0.75) {
// When calling super, explicitly set isPlayer=true and isStatic=false
super(x, y, width, height, {
isPlayer: true, // Critical flag to identify this as the player
isStatic: false, // Ensure player is never static
zHeight: 0.5, // Standard height
collidable: true // Enable collisions
});
// Player specific properties
this.speed = 7; // Increased by 30% from 3.0 for faster portal navigation
this.jumpStrength = 1.6; // Jump strength (reduced from 2.0 for gameplay balance)
this.jumpProgress = 0; // Track jump animation progress
this.isJumping = false; // Track if player is currently jumping
this.canJump = true; // Prevent double jumps
// Track last movement direction for sprite rendering
this.lastDirection = 'south'; // Default facing south
this.isMoving = false; // Track if player is currently moving
}
/**
* Moves the player in a specific direction
* @param {string} direction - Direction to move ('up', 'down', 'left', 'right', 'none', or diagonal combinations)
* @param {number} deltaTime - Time step in seconds
*/
move(direction, deltaTime) {
// Reset velocity if no movement or 'none' is passed
if (!direction || direction === 'none') {
this.velocityX = 0;
this.velocityY = 0;
return;
}
// Calculate the normalized time step (prevent extreme speeds on lag)
const normalizedDelta = deltaTime > 0 ? deltaTime : 1/60;
// Reset velocities
this.velocityX = 0;
this.velocityY = 0;
// Handle cardinal directions
if (direction === 'left' || direction === 'west') {
this.velocityX = -this.speed;
}
else if (direction === 'right' || direction === 'east') {
this.velocityX = this.speed;
}
if (direction === 'up' || direction === 'north') {
this.velocityY = -this.speed;
}
else if (direction === 'down' || direction === 'south') {
this.velocityY = this.speed;
}
// Handle diagonal directions
if (direction === 'northwest' || direction === 'northleft') {
this.velocityX = -this.speed;
this.velocityY = -this.speed;
}
else if (direction === 'northeast' || direction === 'northright') {
this.velocityX = this.speed;
this.velocityY = -this.speed;
}
else if (direction === 'southwest' || direction === 'southleft') {
this.velocityX = -this.speed;
this.velocityY = this.speed;
}
else if (direction === 'southeast' || direction === 'southright') {
this.velocityX = this.speed;
this.velocityY = this.speed;
}
// Normalize diagonal speed to prevent moving faster diagonally
if (this.velocityX !== 0 && this.velocityY !== 0) {
const normalizeFactor = 1 / Math.sqrt(2);
this.velocityX *= normalizeFactor;
this.velocityY *= normalizeFactor;
}
// Update position based on velocity
this.x += this.velocityX * normalizedDelta;
this.y += this.velocityY * normalizedDelta;
// Update the direction immediately if we're moving
if (this.velocityX !== 0 || this.velocityY !== 0) {
this.updateDirection();
}
}
/**
* Updates the player's direction based on current velocity
*/
updateDirection() {
// Only update direction if the player is actually moving
if (this.velocityX === 0 && this.velocityY === 0) {
console.log(`Not updating direction - velocities are zero [${this.velocityX}, ${this.velocityY}]`);
return; // Keep current direction if not moving
}
// Save previous direction for comparison
const prevDirection = this.lastDirection;
// Debug current velocities
console.log(`Updating direction: vX=${this.velocityX.toFixed(2)}, vY=${this.velocityY.toFixed(2)}`);
// Determine basic direction based on velocity
if (this.velocityX > 0 && this.velocityY < 0) {
this.lastDirection = 'northeast';
console.log('Direction check: velocityX > 0 && velocityY < 0 => northeast');
} else if (this.velocityX > 0 && this.velocityY > 0) {
this.lastDirection = 'southeast';
console.log('Direction check: velocityX > 0 && velocityY > 0 => southeast');
} else if (this.velocityX < 0 && this.velocityY > 0) {
this.lastDirection = 'southwest';
console.log('Direction check: velocityX < 0 && velocityY > 0 => southwest');
} else if (this.velocityX < 0 && this.velocityY < 0) {
this.lastDirection = 'northwest';
console.log('Direction check: velocityX < 0 && velocityY < 0 => northwest');
} else if (this.velocityX > 0) {
this.lastDirection = 'east';
console.log('Direction check: velocityX > 0 => east');
} else if (this.velocityX < 0) {
this.lastDirection = 'west';
console.log('Direction check: velocityX < 0 => west');
} else if (this.velocityY < 0) {
this.lastDirection = 'north';
console.log('Direction check: velocityY < 0 => north');
} else if (this.velocityY > 0) {
this.lastDirection = 'south';
console.log('Direction check: velocityY > 0 => south');
}
console.log(`Direction changed: ${prevDirection} -> ${this.lastDirection} [vX=${this.velocityX.toFixed(2)}, vY=${this.velocityY.toFixed(2)}]`);
}
/**
* Stops player movement
*/
stop() {
this.velocityX = 0;
this.velocityY = 0;
this.isMoving = false;
}
/**
* Initiates a jump if the player is grounded
*/
jump() {
if (this.isGrounded && this.canJump) {
this.isJumping = true;
this.isGrounded = false;
this.canJump = false;
this.jumpProgress = 0;
debug('Player jumped');
}
}
/**
* Handles the jump animation
* @param {number} deltaTime - Time step in seconds
*/
updateJump(deltaTime) {
// Update jump progress
this.jumpProgress += 0.05;
// Parabolic jump curve: z = jumpStrength * (t - t²)
// This creates a smooth up and down motion
this.z = this.jumpStrength * (this.jumpProgress - (this.jumpProgress * this.jumpProgress));
// End jump when animation completes
if (this.jumpProgress >= 1) {
this.isJumping = false;
this.z = 0;
// Allow jumping again when grounded
if (this.isGrounded) {
this.canJump = true;
}
}
}
/**
* Updates player state based on input and physics
* @param {number} deltaTime - Time step in seconds
*/
update(deltaTime) {
// Apply velocity to position
super.update(deltaTime);
// Update jump animation if currently jumping
if (this.isJumping) {
this.updateJump(deltaTime);
}
// Check if player is moving and update direction accordingly
if (Math.abs(this.velocityX) > 0.1 || Math.abs(this.velocityY) > 0.1) {
this.isMoving = true;
this.updateDirection();
} else {
this.isMoving = false;
// We've stopped - set velocities to exactly zero to avoid floating point issues
this.velocityX = 0;
this.velocityY = 0;
}
}
/**
* Gets the current facing direction of the player
* @returns {string} Direction ('north', 'northeast', 'east', etc.)
*/
getDirection() {
// Return the player's facing direction
return this.lastDirection;
}
/**
* Gets the appropriate sprite name based on current direction
* @returns {string} Sprite name (e.g., 'wizardN', 'wizardS', etc.)
*/
getSpriteName() {
// Map direction to sprite key
const directionMap = {
'north': 'wizardN',
'northeast': 'wizardNE',
'east': 'wizardE',
'southeast': 'wizardSE',
'south': 'wizardS',
'southwest': 'wizardSW',
'west': 'wizardW',
'northwest': 'wizardNW'
};
return directionMap[this.lastDirection] || 'wizardS'; // Default to south
}
/**
* Check if the player is currently moving
* @returns {boolean} True if the player is moving
*/
isPlayerMoving() {
return this.isMoving;
}
/**
* Draw the player sprite directly on the canvas
* @param {CanvasRenderingContext2D} ctx - Canvas context
* @param {number} screenX - Isometric X position
* @param {number} screenY - Isometric Y position
* @param {number} width - Render width
* @param {number} height - Render height
* @param {number} zOffset - Height offset for jumping
*/
draw(ctx, screenX, screenY, width, height, zOffset) {
try {
// Get the sprite for the current direction
const spriteName = this.getSpriteName();
const sprite = assetLoader.getAsset(spriteName);
// Check if all direction sprites are loaded
const directions = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'];
const loadedSprites = directions.map(dir => {
const key = `wizard${dir}`;
return { dir, key, loaded: !!assetLoader.getAsset(key) };
});
// Log detailed sprite info
console.log(`Player direction: ${this.lastDirection}`);
console.log(`Using sprite: ${spriteName}, loaded: ${!!sprite}`);
console.log('Available sprites:', loadedSprites.filter(s => s.loaded).map(s => s.key).join(', '));
console.log('Missing sprites:', loadedSprites.filter(s => !s.loaded).map(s => s.key).join(', '));
if (sprite) {
// Draw shadow under player
ctx.fillStyle = 'rgba(0, 0, 0, 0.3)';
ctx.beginPath();
ctx.ellipse(
screenX,
screenY,
width / 2,
width / 4,
0, 0, Math.PI * 2
);
ctx.fill();
// Calculate sprite dimensions (making character 2x taller)
const spriteWidth = width * 1.5;
const spriteHeight = height * 3.5; // Doubled from 2.2 to 4.4
// Position sprite so feet are at entity position
// Adjust Y position to account for increased height
const spriteX = screenX - spriteWidth / 2;
const spriteY = screenY - spriteHeight + height / 1.5 - zOffset;
// Draw sprite
ctx.drawImage(
sprite,
spriteX,
spriteY,
spriteWidth,
spriteHeight
);
} else {
// Fallback: draw colored rectangle if sprite not available
ctx.fillStyle = '#00BFFF'; // Light blue placeholder
ctx.fillRect(
screenX - width / 2,
screenY - height - zOffset,
width,
height
);
console.warn('Player sprite not available, using fallback rendering');
}
} catch (err) {
console.error('Error drawing player sprite:', err);
// Emergency fallback
ctx.fillStyle = 'red';
ctx.fillRect(
screenX - width / 2,
screenY - height - zOffset,
width,
height
);
}
}
}
export { Player };