about summary refs log tree commit diff stats
path: root/html/side-scrolling-rogue-thing/js/camera.js
diff options
context:
space:
mode:
Diffstat (limited to 'html/side-scrolling-rogue-thing/js/camera.js')
-rw-r--r--html/side-scrolling-rogue-thing/js/camera.js56
1 files changed, 56 insertions, 0 deletions
diff --git a/html/side-scrolling-rogue-thing/js/camera.js b/html/side-scrolling-rogue-thing/js/camera.js
new file mode 100644
index 0000000..e5d5d14
--- /dev/null
+++ b/html/side-scrolling-rogue-thing/js/camera.js
@@ -0,0 +1,56 @@
+const createCamera = (x, y) => ({
+    x,
+    y,
+    width: window.innerWidth,
+    height: window.innerHeight,
+    // Define the dead zone (the area where camera won't move)
+    deadZone: {
+        x: window.innerWidth * 0.3, // 30% of screen width
+        y: window.innerHeight * 0.3, // 30% of screen height
+    }
+});
+
+const updateCamera = (camera, target) => {
+    // Calculate the center point of the screen
+    const screenCenterX = camera.x + camera.width / 2;
+    const screenCenterY = camera.y + camera.height / 2;
+
+    // Calculate the distance from the target to the screen center
+    const distanceX = target.x - screenCenterX;
+    const distanceY = target.y - screenCenterY;
+
+    // Calculate the dead zone boundaries
+    const deadZoneLeft = -camera.deadZone.x / 2;
+    const deadZoneRight = camera.deadZone.x / 2;
+    const deadZoneTop = -camera.deadZone.y / 2;
+    const deadZoneBottom = camera.deadZone.y / 2;
+
+    // Calculate new camera position with smooth following
+    let newX = camera.x;
+    let newY = camera.y;
+
+    // Horizontal camera movement
+    if (distanceX < deadZoneLeft) {
+        newX += distanceX - deadZoneLeft;
+    } else if (distanceX > deadZoneRight) {
+        newX += distanceX - deadZoneRight;
+    }
+
+    // Vertical camera movement
+    if (distanceY < deadZoneTop) {
+        newY += distanceY - deadZoneTop;
+    } else if (distanceY > deadZoneBottom) {
+        newY += distanceY - deadZoneBottom;
+    }
+
+    // Add subtle smoothing to camera movement
+    const smoothing = 0.1;
+    newX = camera.x + (newX - camera.x) * smoothing;
+    newY = camera.y + (newY - camera.y) * smoothing;
+
+    return {
+        ...camera,
+        x: newX,
+        y: newY
+    };
+};
26027 ^
ca01193d ^
e2240eb4 ^
69e14325 ^
e2240eb4 ^


ec926027 ^

7284d503 ^


8eff7919 ^

f1b71a44 ^
ec926027 ^
7c8493b3 ^

b75e94b3 ^
ec926027 ^
b75e94b3 ^


ec926027 ^
dcfca05e ^

f1b71a44 ^
dcfca05e ^
f89378d5 ^
f1a6f323 ^
9cf71627 ^
2199940a ^
dcfca05e ^

9cf71627 ^

dcfca05e ^
a767dbd3 ^
9cf71627 ^

7284d503 ^
64cf0a59 ^
69e14325 ^


df8bb4c3 ^
f6d47435 ^
f1e953d0 ^
ec926027 ^

8eff7919 ^
b75e94b3 ^

ec926027 ^
8eff7919 ^
f6d47435 ^
f278a15d ^



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