-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssTest.html
More file actions
332 lines (287 loc) · 10.5 KB
/
ssTest.html
File metadata and controls
332 lines (287 loc) · 10.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Side Scroller Tester (7x5)</title>
<style>
body {
margin: 0;
background-color: #1a1a1a;
color: white;
font-family: 'Segoe UI', sans-serif;
overflow: hidden;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
/* UI Overlay */
.controls-panel {
position: absolute;
top: 20px;
left: 20px;
background: rgba(0, 0, 0, 0.8);
padding: 20px;
border-radius: 8px;
border: 1px solid #444;
box-shadow: 0 4px 10px rgba(0,0,0,0.5);
}
h1 { margin: 0 0 10px 0; font-size: 18px; color: #ff0055; }
.key-row { display: flex; justify-content: space-between; margin-bottom: 5px; font-size: 13px; color: #ccc; }
.key { font-weight: bold; color: #fff; background: #333; padding: 2px 6px; border-radius: 4px; }
canvas {
border-bottom: 4px solid #444; /* The floor visual */
background: linear-gradient(to bottom, #202020, #111);
box-shadow: 0 0 30px rgba(0,0,0,0.5);
image-rendering: pixelated;
}
.upload-btn {
margin-top: 10px;
display: block;
width: 100%;
}
</style>
</head>
<body>
<div class="controls-panel">
<h1>Side Scroller Tester</h1>
<div class="key-row"><span>Move</span> <span class="key">A / D</span></div>
<div class="key-row"><span>Jump</span> <span class="key">Space</span></div>
<div class="key-row"><span>Run</span> <span class="key">Shift</span></div>
<div style="border-top:1px solid #444; margin:10px 0;"></div>
<div class="key-row"><span>Attack A</span> <span class="key">J</span></div>
<div class="key-row"><span>Attack B</span> <span class="key">K</span></div>
<div class="key-row"><span>Taunt</span> <span class="key">L</span></div>
<div class="key-row"><span>Hurt</span> <span class="key">H</span></div>
<input type="file" id="upload" class="upload-btn" accept="image/*">
</div>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// --- CONFIG ---
// 7 Rows x 5 Columns
const COLS = 5;
const ROWS = 7;
// Row Mapping based on your request
const ANIM = {
IDLE: 0,
WALK: 1,
RUN: 2,
ATTACK_A: 3,
ATTACK_B: 4,
TAUNT: 5,
HURT: 6
};
// Physics Config
const GRAVITY = 0.8;
const WALK_SPEED = 4;
const RUN_SPEED = 8;
const JUMP_FORCE = 15;
const GROUND_Y = 400; // Where the floor is
// --- STATE ---
let spriteSheet = new Image();
let isLoaded = false;
let player = {
x: 200,
y: GROUND_Y,
vx: 0,
vy: 0,
facingRight: true,
state: ANIM.IDLE,
frame: 0,
frameTimer: 0,
isGrounded: true,
isLocked: false // Locks movement during attacks
};
// Frame Dimensions (Calculated on load)
let fw = 64;
let fh = 64;
// Input
const keys = {
left: false, right: false, run: false, jump: false
};
// --- INIT ---
function resize() {
canvas.width = window.innerWidth - 100;
canvas.height = window.innerHeight - 100;
GROUND_Y_ACTUAL = canvas.height - 100;
player.y = GROUND_Y_ACTUAL;
ctx.imageSmoothingEnabled = false;
}
window.addEventListener('resize', resize);
resize();
document.getElementById('upload').addEventListener('change', (e) => {
const f = e.target.files[0];
if(!f) return;
const reader = new FileReader();
reader.onload = (evt) => {
spriteSheet = new Image();
spriteSheet.onload = () => {
isLoaded = true;
// Auto-Calculate Grid
fw = Math.floor(spriteSheet.width / COLS);
fh = Math.floor(spriteSheet.height / ROWS);
};
spriteSheet.src = evt.target.result;
};
reader.readAsDataURL(f);
});
// Input Listeners
window.addEventListener('keydown', e => {
const k = e.key.toLowerCase();
if(k === 'a' || k === 'arrowleft') keys.left = true;
if(k === 'd' || k === 'arrowright') keys.right = true;
if(k === 'shift') keys.run = true;
if(k === ' ' || k === 'arrowup') jump();
// Actions
if(k === 'j') playAction(ANIM.ATTACK_A);
if(k === 'k') playAction(ANIM.ATTACK_B);
if(k === 'l') playAction(ANIM.TAUNT);
if(k === 'h') playAction(ANIM.HURT);
});
window.addEventListener('keyup', e => {
const k = e.key.toLowerCase();
if(k === 'a' || k === 'arrowleft') keys.left = false;
if(k === 'd' || k === 'arrowright') keys.right = false;
if(k === 'shift') keys.run = false;
});
// --- LOGIC ---
function jump() {
if(player.isGrounded && !player.isLocked) {
player.vy = -JUMP_FORCE;
player.isGrounded = false;
}
}
function playAction(animRow) {
// interrupt current action? yes.
player.state = animRow;
player.frame = 0;
player.isLocked = true; // Can't move while attacking
}
function update() {
// 1. Physics
if(!player.isLocked) {
let speed = keys.run ? RUN_SPEED : WALK_SPEED;
if(keys.left) {
player.vx = -speed;
player.facingRight = false;
} else if(keys.right) {
player.vx = speed;
player.facingRight = true;
} else {
player.vx = 0;
}
} else {
// Friction if locked (attacking)
player.vx *= 0.8;
}
// Apply Velocity
player.x += player.vx;
player.y += player.vy;
player.vy += GRAVITY;
// Ground Collision
// We use GROUND_Y_ACTUAL defined in resize
const floor = canvas.height - 100;
if(player.y >= floor) {
player.y = floor;
player.vy = 0;
player.isGrounded = true;
} else {
player.isGrounded = false;
}
// 2. State Machine (Animation Switching)
// Only change state if we aren't locked in an animation (like attacking)
if(!player.isLocked) {
if(!player.isGrounded) {
// Optional: You could add a "Jump" row here if you had one
// For now, we use Run frame for jumping
player.state = ANIM.RUN;
}
else if(Math.abs(player.vx) > 0.1) {
player.state = keys.run ? ANIM.RUN : ANIM.WALK;
}
else {
player.state = ANIM.IDLE;
}
}
// 3. Animation Frame Cycle
player.frameTimer++;
let frameDelay = 8; // Standard speed
if(player.state === ANIM.RUN) frameDelay = 5; // Run faster
if(player.frameTimer > frameDelay) {
player.frame++;
// Loop Logic
if(player.frame >= COLS) {
if(player.isLocked) {
// If action finished, return to idle and unlock
player.isLocked = false;
player.state = ANIM.IDLE;
player.frame = 0;
} else {
// Loop normally
player.frame = 0;
}
}
player.frameTimer = 0;
}
}
// --- DRAW ---
function draw() {
// Background
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw Floor Line
ctx.beginPath();
ctx.moveTo(0, canvas.height - 100 + fh); // Draw slightly below feet
ctx.lineTo(canvas.width, canvas.height - 100 + fh);
ctx.strokeStyle = "#444";
ctx.lineWidth = 4;
ctx.stroke();
// Draw Text instructions if not loaded
if(!isLoaded) {
ctx.fillStyle = "white";
ctx.font = "20px sans-serif";
ctx.textAlign = "center";
ctx.fillText("Upload your 7x5 Sprite Sheet", canvas.width/2, canvas.height/2 - 20);
return;
}
// SPRITE DRAWING
ctx.save();
// Position the context at player's feet
let drawX = player.x;
let drawY = player.y;
// Translate to pivot point (bottom center of sprite)
ctx.translate(drawX, drawY);
// Scale Size
const scale = 3;
// FLIP LOGIC
if(!player.facingRight) {
ctx.scale(-1, 1); // Mirror horizontally
}
// Draw Image
// Source X/Y
let sx = player.frame * fw;
let sy = player.state * fh;
// Draw centered on the pivot
// We draw at -width/2 and -height so the pivot is at bottom center
ctx.drawImage(
spriteSheet,
sx, sy, fw, fh, // Source
-(fw*scale)/2, -(fh*scale), fw*scale, fh*scale // Dest
);
// Debug Box (optional, to see pivot)
// ctx.fillStyle = "red";
// ctx.fillRect(-2, -2, 4, 4);
ctx.restore();
}
// Loop
function loop() {
update();
draw();
requestAnimationFrame(loop);
}
loop();
</script>
</body>
</html>