-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedux.js
1235 lines (1136 loc) · 40 KB
/
Redux.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @class ReduxEngine
* @description Base class for ReduxEngine
* @param {String} containerId - The id of the container element
* @param {Number} width - The width of the container element
* @param {Number} height - The height of the container element
* @param {Number} frameRate - The frame rate of the game
* @property {HTMLElement} container - The container element
* @property {Array} entities - The entities in the game
* @property {Boolean} isRunning - The running state of the game
* @property {Function} updateCallback - The update callback function
* @property {Number} frameRate - The frame rate of the game
* @property {Number} frameInterval - The frame interval of the game
* @property {Number} lastFrameTime - The last frame time of the game
* @property {Object} keyStates - The key states of the game
* @property {HTMLElement} fpsElement - The FPS element of the game
* @property {Scene} currentScene - The current scene of the game
* @property {Object} scenes - The scenes of the game
* @property {Camera} camera - The camera of the game
* @method setUpdateCallback - Set the update callback function
* @object {Object} inputState - The engine input state
* @method setUpdateCallback - Set the update callback function
* @method addEntity - Add an entity to the game
* @method removeEntity - Remove an entity from the game
* @method clearEntities - Clear all entities from the game
* @method addScene - Add a scene to the game
* @method startScene - Start a scene in the game
* @method handleInput - Handle input for the game
* @method removeInputListeners - Remove input listeners from the game
* @method logFps - Log the FPS of the game
* @method loop - The game loop
* @method start - Start the game
* @method setupInputListeners - Setup input listeners for the game
* @example
* const engine = new ReduxEngine('game-container', 800, 600);
* engine.setUpdateCallback((entities) => {
* // Update logic here
* });
* engine.addEntity(entity);
* engine.removeEntity(entity);
* engine.clearEntities();
* engine.addScene('StartMenu', startMenuScene);
* engine.addScene('Game', gameScene);
* engine.startScene('StartMenu');
* engine.handleInput('ArrowRight', () => {
* // ArrowRight pressed
* }, () => {
* // ArrowRight released
* });
* engine.removeInputListeners('ArrowRight');
* engine.logFps(fps);
* engine.start();
* engine.setupInputListeners();
*/
class ReduxEngine {
constructor(containerId, width, height, frameRate = 60) {
this.container = document.getElementById(containerId);
this.container.style.position = "relative";
this.container.style.width = width + "px";
this.container.style.height = height + "px";
this.container.style.overflow = "hidden";
this.container.style.justifyContent = "center";
this.container.style.alignItems = "center";
this.container.style.display = "flex";
this.entities = [];
this.isRunning = false;
this.updateCallback = null;
this.frameRate = frameRate;
this.frameInterval = 1000 / this.frameRate;
this.lastFrameTime = 0;
let inputState = {
keyboard: {},
mouse: {},
touch: {},
gamepad: [
{
input: null,
axis: [],
buttons: [],
},
],
};
/**
* @property {Object} inputState - The engine input state
* @property {Object} inputState.keyboard - The keyboard input state
* @property {Object} inputState.mouse - The mouse input state
* @property {Object} inputState.touch - The touch input state
* @property {Object} inputState.gamepad - The gamepad input state
* @property {Object} inputState.gamepad[0] - The first gamepad input state
* @property {Object} inputState.gamepad[1] - The second gamepad input state
* @property {Object} inputState.gamepad[index].input - The gamepad input
* @property {Object} inputState.gamepad[index].axis - The gamepad axis
* @readonly
*/
this.inputState = inputState;
this.gamepadStates = {};
this.keyStates = {};
this.fpsElement = null; // For displaying FPS
this.currentScene = null;
this.scenes = {};
this.camera = null;
}
/**
* @method setUpdateCallback
* @param {*} callback
* @description Set the update callback function
*/
setUpdateCallback(callback) {
this.updateCallback = callback;
}
/**
* @method addEntity
* @param {*} element
* @description Add an entity to the game
* @example
* ReduxEngine.addEntity(entity);
*/
addEntity(element) {
this.entities.push(element);
this.container.appendChild(element.element);
}
/**
* @method removeEntity
* @param {*} element
* @description Remove an entity from the game
* @example
* ReduxEngine.removeEntity(entity);
*/
removeEntity(element) {
this.entities = this.entities.filter((entity) => entity !== element);
element.element.remove();
}
/**
* @method clearEntities
* @description Clear all entities from the game
* @example
* ReduxEngine.clearEntities();
*/
clearEntities() {
this.entities.forEach((entity) => {
this.removeEntity(entity);
});
}
/**
* @method addScene
* @param {*} sceneName
* @param {*} scene
* @description Allows you to add scenes to the game
* @example
* ReduxEngine.addScene('StartMenu', startMenuScene);
*/
addScene(sceneName, scene) {
this.scenes[sceneName] = scene;
}
/**
* @method startScene
* @param {*} sceneName
* @description Start the given scene
* @example
* ReduxEngine.startScene('StartMenu');
*/
startScene(sceneName) {
if (this.currentScene) {
this.currentScene.stop();
}
this.currentScene = this.scenes[sceneName];
this.currentScene.start();
}
setCamera(camera) {
this.camera = camera;
}
/***
* @method handleInput
* @param {*} key
* @param {*} startCallback
* @param {*} stopCallback
* @description Handle input for the game
* @example
* ReduxEngine.handleInput('ArrowRight', () => {
* // ArrowRight pressed
* }, () => {
* // ArrowRight released
* });
*/
/**
* @method removeInputListeners
* @param {*} key
* @description Remove input listeners from the game
* @example
* ReduxEngine.removeInputListeners('ArrowRight');
*/
removeInputListeners(key) {
delete this.keyStates[key];
}
/**
* @method inputHandler
* @param {*} key
* @param {Function} startCallback
* @param {Fnction} stopCallback
* @description Implement custom input setup in derived scenes
* @example
* this.inputHandler('ArrowRight', () => {
* // ArrowRight pressed
* }, () => {
* // ArrowRight released
* });
*
* this.inputHandler('ArrowLeft', () => {
* // ArrowLeft pressed
*
* }, () => {
* // ArrowLeft released
* });
* this.inputHandler('mouse1', () => {
* // mouse1 pressed
* }, () => {});
*
*/
inputHandler(key, startCallback, stopCallback) {
this.keyStates[key] = {
isPressed: false,
startCallback: startCallback,
stopCallback: stopCallback,
};
}
/**
* @method logFps
* @param {*} fps
* @param {*} color
* @description Displays a fps counter on the screen
* @example
* ReduxEngine.logFps(fps);
*/
logFps(fps, color = "black") {
if (!this.fpsElement) {
this.fpsElement = document.createElement("div");
this.fpsElement.style.position = "absolute";
this.fpsElement.style.top = "10px";
this.fpsElement.style.color = color;
this.fpsElement.style.left = "10px";
this.container.appendChild(this.fpsElement);
document.title = `FPS: ${fps.toFixed(2)}`;
}
this.fpsElement.innerText = `FPS: ${fps.toFixed(2)}`;
}
loop() {
if (!this.isRunning) {
return;
}
const currentTime = Date.now();
const elapsed = currentTime - this.lastFrameTime;
if (elapsed >= this.frameInterval) {
this.lastFrameTime = currentTime;
if (this.updateCallback) {
this.updateCallback(this.entities);
}
// Check for key state changes
for (const key in this.keyStates) {
if (this.keyStates.hasOwnProperty(key)) {
const keyState = this.keyStates[key];
if (keyState.isPressed && keyState.startCallback) {
keyState.startCallback();
} else if (!keyState.isPressed && keyState.stopCallback) {
keyState.stopCallback();
}
}
}
if (this.camera) {
this.camera.update();
}
// Calculate and log FPS
const fps = 1000 / elapsed;
this.logFps(fps);
}
requestAnimationFrame(() => this.loop());
}
/**
* @method start
* @description Initiate the game loop
* @example
* ReduxEngine.start();
*/
start() {
if (!this.isRunning) {
this.isRunning = true;
this.loop();
this.setupInputListeners(); // Add input event listeners once when starting
} else {
throw new Error(
"Too many instances of ReduxEngine start should only be called once"
);
}
}
handleMouseInput(event) {
// get button etc
let { type } = event;
event.preventDefault();
switch (type) {
case "mousedown":
this.inputState.mouse = {
input: event,
x: event.clientX,
y: event.clientY,
};
if (this.keyStates["mouse" + event.button]) {
this.keyStates["mouse" + event.button].isPressed = true;
}
break;
case "mouseup":
this.inputState.mouse = {
input: event,
x: event.clientX,
y: event.clientY,
};
if (this.keyStates["mouse" + event.button]) {
this.keyStates["mouse" + event.button].isPressed = false;
}
break;
case "mousemove":
this.inputState.mouse = {
input: event,
x: event.clientX,
y: event.clientY,
};
if (this.keyStates["getMousePosition"]) {
this.keyStates["getMousePosition"].isPressed = true;
}
break;
default:
break;
}
}
handleGamepadInput(event) {
const { type, gamepad } = event;
if (type === "gamepadconnected") {
this.gamepadStates[gamepad.index] = gamepad;
const updateGamepad = () => {
const updatedGamepad = navigator.getGamepads()[gamepad.index];
if (updatedGamepad) {
// Check button presses
for (let i = 0; i < updatedGamepad.buttons.length; i++) {
const button = updatedGamepad.buttons[i];
let inputKey = `gamepad${gamepad.index}_button${i}`;
if (this.keyStates[inputKey]) {
if (button.pressed) {
this.keyStates[inputKey].isPressed = true;
} else {
this.keyStates[inputKey].isPressed = false;
}
}
}
// Check axis values
for (let i = 0; i < updatedGamepad.axes.length; i++) {
const axisValue = updatedGamepad.axes[i];
const inputKey = `gamepad${gamepad.index}_axis`;
if (this.keyStates[inputKey]) {
this.inputState.gamepad[gamepad.index] = {
input: updatedGamepad,
axis: updatedGamepad.axes,
buttons: updatedGamepad.buttons,
};
this.keyStates[inputKey].isPressed = true;
}
}
}
requestAnimationFrame(updateGamepad);
};
updateGamepad();
} else if (type === "gamepaddisconnected") {
delete this.gamepadStates[gamepad.index];
}
}
handleTouchInput(event) {
const { type } = event;
switch (type) {
case "touchstart":
// Handle touch start if needed
break;
case "touchend":
// Handle touch end if needed
break;
case "touchmove":
// Handle touch move if needed
break;
default:
break;
}
}
handleKeyboardInput(event) {
const { key, type } = event;
switch (type) {
case "keydown":
this.inputState.keyboard[key] = true;
if (this.keyStates[key]) {
this.keyStates[key].isPressed = true;
}
break;
case "keyup":
this.inputState.keyboard[key] = false;
if (this.keyStates[key]) {
this.keyStates[key].isPressed = false;
}
break;
default:
break;
}
}
/**
* @method setupInputListeners
* @description Setup input listeners for the game
* @example
* ReduxEngine.setupInputListeners();
* @private
*/
setupInputListeners() {
let isPressed = false;
window.addEventListener("keydown", (event) => {
this.handleKeyboardInput(event);
});
window.addEventListener("keyup", (event) => {
this.handleKeyboardInput(event);
});
document.addEventListener("contextmenu", (event) => event.preventDefault());
// constantly check mouse input
this.container.addEventListener("mousedown", (event) => {
event.preventDefault();
this.handleMouseInput(event);
});
this.container.addEventListener("mouseup", (event) => {
event.preventDefault();
this.handleMouseInput(event);
});
this.container.addEventListener("mousemove", (event) => {
event.preventDefault();
this.handleMouseInput(event);
});
this.container.addEventListener("touchstart", (event) => {
this.handleTouchInput(event);
});
this.container.addEventListener("touchend", (event) => {
this.handleTouchInput(event);
});
this.container.addEventListener("touchmove", (event) => {
this.handleTouchInput(event);
});
window.addEventListener("gamepadconnected", (event) => {
this.handleGamepadInput(event);
});
window.addEventListener("gamepaddisconnected", (event) => {
this.handleGamepadInput(event);
});
}
/**
* @method Object
* @returns @type {Object} - The window dimensions
* @description Get the window dimensionss
*/
window() {
return this.container;
}
}
class Camera {
constructor(container, voidColor) {
this.container = container;
this.targetEntity = null;
this.voidColor = voidColor || "black";
this.container.style.overflow = "hidden";
this.offsetX = 0;
this.offsetY = 0;
}
follow(entity) {
this.targetEntity = entity;
}
update() {
if (this.targetEntity) {
const targetX = this.targetEntity.x;
const targetY = this.targetEntity.y;
// follow entitity closely
const newOffsetX =
targetX - this.container.clientWidth / 2 + this.targetEntity.width / 2;
const newOffsetY =
targetY -
this.container.clientHeight / 2 +
this.targetEntity.height / 2;
// Apply the new position to the container's scroll position
if (this.container.scrollLeft !== newOffsetX) {
this.container.style.transform = `translate(${
this.offsetX - newOffsetX
}px, ${this.offsetY - newOffsetY}px)`;
}
this.container.style.backgroundColor = this.voidColor;
// Update the camera's offset values
this.offsetX = newOffsetX / 5;
this.offsetY = newOffsetY / 2;
return {
x: this.offsetX,
y: this.offsetY,
};
} else {
throw new Error("Camera must have a target entity");
}
}
}
/**
* @function isColliding
* @param {*} elementA
* @param {*} elementB
* @param {*} sensitivity
* @returns @type {String} - The direction of collision
* @description Check if two elements are colliding
* @example
* // entities are instances of the Entity class
* this.player = new Entity(100, 100, 50, 50, 'blue');
* this.dummy = new Entity(100, 100, 50, 50, 'red');
* if(isColliding(this.player.element, this.dummy.element, 200) === 'left'){
* console.log('colliding')
* }
*/
const isColliding = (elementA, elementB, sensitivity) => {
// Get the bounding rectangles of the two elements
const rectA = elementA.getBoundingClientRect();
const rectB = elementB.getBoundingClientRect();
console.log(rectA, rectB);
// Calculate the vectors to check the direction of collision
const dx = (rectA.right + rectA.left) / 2 - (rectB.right + rectB.left) / 2;
const dy = (rectA.bottom + rectA.top) / 2 - (rectB.bottom + rectB.top) / 2;
const widthA = rectA.right - rectA.left;
const widthB = rectB.right - rectB.left;
const heightA = rectA.bottom - rectA.top;
const heightB = rectB.bottom - rectB.top;
// Calculate the minimum distance for collision to occur
const minDistance = (widthA + widthB) / 2 / 2;
// Adjust the minimum distance with sensitivity
const adjustedMinDistance = minDistance * sensitivity;
// Check for collision and determine the direction
if (
Math.abs(dx) <= (widthA + widthB) / 2 + adjustedMinDistance &&
Math.abs(dy) <= (heightA + heightB) / 2 + adjustedMinDistance
) {
const overlapX = (widthA + widthB) / 2 + adjustedMinDistance - Math.abs(dx);
const overlapY =
(heightA + heightB) / 2 + adjustedMinDistance - Math.abs(dy);
if (overlapX >= overlapY) {
if (dy > 0) {
return "top";
}
return "bottom";
}
if (dx > 0) {
return "left";
}
return "right";
}
// No collision detected
return null;
};
/***
* @function parseTextureScript
* @param {*} textureScript
* @returns @type {Object} - The styles for the texture script
* @description Parse Redux Texture Scripts to CSS Browser Styles
* @example
* let textureScript = `
* w(100)
* h(100)
* bgColor(FF0000)
* `
* const styles = parseTextureScript(textureScript);
*
* for (const key in styles) {
* if (styles.hasOwnProperty(key)) {
* this.element.style.cssText += `${styles[key]};`;
* }
* }
*
*/
function parseTextureScript(textureScript) {
const styles = {};
const lines = textureScript.split("\n");
const functions = {
vector: (value, inset) => `border-radius: ${value}; padding: ${inset};`, // Set border radius
min: (value) => Math.min(value), // Set border radius
color: (value) => `#${value}`,
screenW: () => window.innerWidth,
screenH: () => window.innerHeight,
gradient: (direction, color1, color2) =>
`linear-gradient(${direction}, #${color1}, #${color2})`,
boxShadow: (x, y, blur, color) => `${x}px ${y}px ${blur}px #${color}`,
opacity: (value) => value,
rotate: (angle) => `rotate(${angle}deg)`,
scale: (value) => `scale(${value})`,
translate: (x, y) => `translate(${x}px, ${y}px)`,
font: (family, size) => `'${family}', ${size}`,
textShadow: (x, y, blur, color) => `${x}px ${y}px ${blur}px #${color}`,
w: (value) => `width: ${value};`,
tile: (value) => `background-repeat: repeat; background-size: ${value}px;`,
texture: (url) => `background-image: url(${url}); background-size: cover;`,
bgColor: (color) => `background-color: #${color};`,
bgGradient: (direction, color1, color2) =>
`background-image: linear-gradient(${direction}, #${color1}, #${color2});`,
bgGradientRadial: (color1, color2) =>
`background-image: radial-gradient(#${color1}, #${color2});`,
bgGradientRadialCircle: (color1, color2) =>
`background-image: radial-gradient(circle, #${color1}, #${color2});`,
bgGradientRadialEllipse: (color1, color2) =>
`background-image: radial-gradient(ellipse, #${color1}, #${color2});`,
bgGradientRadialClosestSide: (color1, color2) =>
`background-image: radial-gradient(closest-side, #${color1}, #${color2});`,
};
lines.forEach((line) => {
const [key, value] = line
.split("(")
.map((item) => item.trim().replace(")", ""));
if (window[value]) {
value = window[value]();
console.log(value);
}
if (functions[key]) {
styles[key] = functions[key](...value.split(","));
}
});
return styles;
}
class Entity {
constructor(x, y, width, height, color, initialState, textureScript) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color || "black";
this.element = document.createElement("div");
this.element.style.position = "absolute";
this.element.style.width = this.width + "px";
this.element.style.height = this.height + "px";
this.state = initialState || {};
this.animationStates = {};
this.animationState = null;
this.applyTextureScript(textureScript);
this.element.style.backgroundColor = this.color;
this.element.style.left = this.x + "px";
this.element.style.top = this.y + "px";
this.element.style.pointerEvents = "none";
this.element.style.zIndex = "1"; // Ensure entities are displayed above UI elements
this.parseTextureScript = parseTextureScript;
}
/**
* @method applyTextureScript
* @param {*} textureScript
* @returns @type {Object} - The styles for the texture script
* @description Apply Redux Texture Scripts to the entity
* @example
* let textureScript = `
* w(100)
* h(100)
* bgColor(FF0000)
* `
* entity.applyTextureScript(textureScript);
*/
applyTextureScript(textureScript) {
if (!textureScript) return;
const styles = this.parseTextureScript(textureScript);
for (const key in styles) {
if (styles.hasOwnProperty(key)) {
this.element.cssText = " ";
this.element.style.cssText += `${styles[key]};`;
}
}
}
/**
* @method index
* @description Set the z-index of the entity - higher z-index means the entity is displayed above other entities
* @example
* entity.index(1);
* @param {*} I
*/
index(I) {
this.element.style.zIndex = index;
}
/**
* @method addAnimationState
* @description Add an animation state to the entity and apply a texture script
* @param {*} stateName
* @param {*} textureScript
* @example
* entity.addAnimationState('inPlace', `texture('https://picsum.photos/200/300')`);
* entity.changeState('inPlace');
*/
addAnimationState(stateName, textureScript) {
this.animationStates[stateName] = textureScript;
}
/**
* @method changeState
* @description Change the animation state of the entity
* @param {*} stateName
* @returns
*/
changeState(stateName) {
if (this.animationState === stateName) return;
this.animationState = stateName;
this.applyTextureScript(this.animationStates[stateName]);
this.render();
}
/**
* @method setAnimationState
* @param {*} state
* @description Set the animation state of the entity
*/
static setAnimationState(state) {
this.animationState = state;
}
getAnimationState() {
return this.animationState;
}
/**
* @method render
* @description Render the entity
* @example
* entity.render();
*/
render() {
this.element.style.left = this.x + "px";
this.element.style.top = this.y + "px";
}
getState() {
return this.state;
}
setState(newState) {
this.state = { ...this.state, ...newState };
}
bind(key, callback) {
if (typeof callback === "function") {
this.state[key] = callback;
}
}
/**
* @method Object
* @returns @type {HTMLElement} - The entity element
*/
Object() {
return this.element;
}
/**
* @method setPosition
* @param {*} x
* @param {*} y
* @description Set the position of the entity
* @example
* entity.setPosition(100, 100);
* entity.setPosition(entity.x + 10, entity.y);
*/
setPosition(x, y) {
if (x && y) {
this.x = x;
this.y = y;
this.render();
}
}
}
/**
* @class UI
* @description Base class for UI elements
* @param {Number} x - The x position of the UI element
* @param {Number} y - The y position of the UI element
* @param {Number} width - The width of the UI element
* @param {Number} height - The height of the UI element
* @param {String|HTMLElement} customContent - Custom content for the UI element
* @param {String} tag - The tag for the UI element
* @property {HTMLElement} element - The UI element
* @method on - Add an event listener to the UI element
* @example
* const uiElement = new UI(100, 100, 200, 50, div, 'startui');
* uiElement.on('click', () => {
* console.log('UI element clicked.');
* });
**/
class UI {
constructor(x, y, width, height, customContent, tag) {
this.element = document.createElement("div");
this.element.style.position = "absolute";
this.element.style.width = width + "px";
this.element.style.height = height + "px";
this.element.style.left = x + "px";
this.element.style.top = y + "px";
this.element.style.zIndex = "1"; // Ensure UI elements are displayed above entities
if (tag === undefined) {
throw new Error("UI element must have a tag");
}
this.element.id = tag;
this.element.style.pointerEvents = "none"; // Disable pointer events for UI elements
// Append custom content to the UI element
if (customContent) {
if (typeof customContent === "string") {
// If customContent is a string, treat it as HTML content
this.element.innerHTML = customContent;
} else if (customContent instanceof HTMLElement) {
// If customContent is an HTMLElement, append it directly
this.element.appendChild(customContent);
}
}
}
// Method to load external HTML content
async require(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to load external HTML from ${url}`);
}
const htmlContent = await response.text();
this.element.innerHTML = htmlContent;
} catch (error) {
console.error(error);
}
}
/**
* @method on
* @param {Identifier} target - the target element id - useful for nested views and imported ui elements
* @param {Event} event - The event to listen for
* @param {Function} callback - The callback function
* @description Add an event listener to the UI element
*/
on(target, event, callback) {
// wait for the element to be rendered
setTimeout(() => {
this.element.style.pointerEvents = "auto"; // Enable pointer events for UI elements
this.element.addEventListener(event, (e) => {
if (e.target.id === target) {
callback(e);
}
});
}, 0);
}
/**
* @method updateContent
* @param {*} content
* @description Update the content of the UI element
*/
updateContent(content) {
this.element.innerHTML = content;
}
/**
* @method Object
* @description Use to explicitly return the UI element
* @returns @type {HTMLElement} - The UI element
*/
Object() {
return this.element;
}
}
/***
* @class Scene
* @description Base class for scenes
* @param {ReduxEngine} engine - The ReduxEngine instance
* @property {ReduxEngine} engine - The ReduxEngine instance
* @property {Array} entities - The entities in the scene
* @property {Object} customInputs - Custom input bindings for the scene
* @property {Array} uiElements - The UI elements in the scene
* @method addUIElement - Add a UI element to the scene
* @method removeUIElements - Remove all UI elements from the scene
* @method removeUIElement - Remove a UI element from the scene
* @method uiLogic - Implement UI logic in derived scenes
* @method start - Start the scene
* @method stop - Stop the scene
* @method startLogic - Implement custom start logic in derived scenes
* @method inputHandler - Implement custom input setup in derived scenes
* @method addCustomInput - Add a custom input binding to the scene
* @method cleanupInputs - Remove custom input bindings from the scene
* @example
* class StartMenuScene extends Scene {
* constructor(engine) {
* super(engine);
* }
* startLogic() {
* console.log('StartMenuScene: Scene-specific logic here.');
* }
* uiLogic() {
* console.log('StartMenuScene: UI logic here.');
* }
* inputHandler() {
* this.addCustomInput('ArrowRight', () => {
* console.log('StartMenuScene: ArrowRight pressed.');
* }, () => {
* console.log('StartMenuScene: ArrowRight released.');
* });
* }
* }
*
*/
class Scene {
constructor(engine) {
/**
* @property {ReduxEngine} engine - The ReduxEngine instance
* @param {String} containerId - The id of the container element
* @param {Number} width - The width of the container element
* @param {Number} height - The height of the container element
* @param {Number} frameRate - The frame rate of the game
* @property {HTMLElement} container - The container element
* @property {Array} entities - The entities in the game
* @property {Boolean} isRunning - The running state of the game
* @property {Function} updateCallback - The update callback function
* @property {Number} frameRate - The frame rate of the game
* @property {Number} frameInterval - The frame interval of the game
* @property {Number} lastFrameTime - The last frame time of the game
* @property {Object} keyStates - The key states of the game
* @property {HTMLElement} fpsElement - The FPS element of the game
* @property {Scene} currentScene - The current scene of the game
* @property {Object} scenes - The scenes of the game
* @property {Camera} camera - The camera of the game
* @method setUpdateCallback - Set the update callback function
* @object {Object} inputState - The engine input state
* @method setUpdateCallback - Set the update callback function
* @method addEntity - Add an entity to the game
* @method removeEntity - Remove an entity from the game
* @method clearEntities - Clear all entities from the game
* @method addScene - Add a scene to the game
* @method startScene - Start a scene in the game
* @method handleInput - Handle input for the game
* @method removeInputListeners - Remove input listeners from the game