kippy 0.2.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -64,8 +64,7 @@ import { Entity } from "kippy";
64
64
 
65
65
  const entity = new Entity({
66
66
  sprite, // Entity's sprite to be rendered, type Sprite
67
- x, // Entity's x position (centered), type number
68
- y, // Entity's y position (centered), type number
67
+ position, // Entity's position (centered), type Vector2
69
68
  rotation, // Entity's rotation in radians, type number
70
69
  body, // Entity's physical body, type EntityBody
71
70
  });
@@ -76,8 +75,10 @@ scene.addEntity(entity);
76
75
  scene.removeEntity(entity);
77
76
 
78
77
  // These props contain movement info and you can mutate them to edit its position
79
- entity.x; // Initialized from the "x" param above, 0 if not specified
80
- entity.y; // Initialized from the "y" param above, 0 if not specified
78
+ entity.position; // Initialized from the "position" param above, Vector2(0, 0) if not specified
79
+ // You can mutate these directly:
80
+ entity.position.x;
81
+ entity.position.y;
81
82
  entity.rotation; // Initialized from the "rotation" param above, 0 if not specified
82
83
  ```
83
84
 
@@ -114,8 +115,48 @@ input.isKeyReleased(/* Character/key here */); // true if key is released, false
114
115
  input.isPointerDown(/* 0 for left, 1 for right, 2 for touch */); // true if held, false otherwise
115
116
  input.isPointerPressed(/* 0 for left, 1 for right, 2 for touch */); // true if pressed, false otherwise
116
117
  input.isPointerReleased(/* 0 for left, 1 for right, 2 for touch */); // true if released, false otherwise
117
- input.pointerX; // Current X position of mouse/touch
118
- input.pointerY; // Current Y position of mouse/touch
118
+ // Mouse/touch position
119
+ input.pointer; // Pointer's position vector
120
+ input.pointer.x; // Current X position of mouse/touch
121
+ input.pointer.y; // Current Y position of mouse/touch
122
+ ```
123
+
124
+ ### Vectors
125
+
126
+ To work with positions and movements in Kippy, it is best to know about `Vector2` first. Positions, velocities, forces, etc are all represented as vectors in Kippy. And here is how you can create a 2D vector and some vector math utilities that comes along with it:
127
+ ```js
128
+ import { Vector2 } from "kippy";
129
+
130
+ const vect = new Vector2(/* x coordinate, number */, /*y coordinate, number */);
131
+
132
+ // Props
133
+ vect.x; // X coordinate
134
+ vect.y; // Y coordinate
135
+
136
+ // Utilities
137
+ vect.add(otherVect); // Add another vector and return the result vector
138
+ vect.sub(otherVect); // Subtract another vector and return the result vector
139
+ vect.scale(scale); // Multiply with scale and return the result vector
140
+ vect.magnitude(); // Return the magnitude/length of vector
141
+ vect.normalize(); // Return the normalized vector by magnitude
142
+ vect.dot(otherVect); // Return dot product with another vector
143
+ vect.distance(otherVect); // Return distance to another vector
144
+ vect.copy(); // Return a copy (same coordinates, different reference)
145
+ vect.lerp(otherVect, scale); // Apply linear interpolation and return
146
+ vect.clamp(maxLength); // Clamp vector to have length below maxLength
147
+ vect.rotate(angle); // Return rotated vector by provided angle
148
+ vect.angle(); // Return angle of vector.
149
+ vect.angleTo(otherVec); // Return angle between this and another vector
150
+ vect.reflect(otherVect); // Return reflection/bounce back vector
151
+ vect.equals(otherVect); // Check if two vectors are equal
152
+
153
+ // Useful constants
154
+ Vector2.ZERO; // Vector2(0, 0)
155
+ Vector2.ONE; // Vector2(1, 1);
156
+ Vector2.UP; // Vector2(0, -1);
157
+ Vector2.DOWN; // Vector2(0, 1);
158
+ Vector2.LEFT; // Vector2(-1, 0);
159
+ Vector2.RIGHT; // Vector2(1, 0);
119
160
  ```
120
161
 
121
162
  ### Physics
@@ -124,13 +165,11 @@ For movements, currently you can create a `RigidBody`:
124
165
  ```js
125
166
  // Create a rigid body
126
167
  const rigidBody = new RigidBody({
127
- velocityX, // X velocity, type number
128
- velocityY, // Y velocity, type number
129
- rotationVelocity, // Angular/rotation velocity, type number
168
+ velocity, // Entity's velocity vector, type Vector2
169
+ rotationVelocity, // Entity's ngular/rotation velocity, type number
130
170
  mass, // Entity's mass, type number
131
171
  inertia, // Entity's inertia, type number
132
- forceX, // Entity's force on X axis, type number
133
- forceY, // Entity's force on Y axis, type number
172
+ force, // Entity's force vector, type Vector2
134
173
  torque, // Entity's torque/rotational force, type number
135
174
  });
136
175
 
@@ -138,18 +177,35 @@ const rigidBody = new RigidBody({
138
177
  entity.body = rigidBody;
139
178
 
140
179
  // And you can mutate these props to update movement every frame
141
- entity.body.velocityX; // Set with the matching parameter above, default is 0
142
- entity.body.velocityY; // Set with the matching parameter above, default is 0
180
+ entity.body.velocity; // Set with the matching parameter above, default is Vector2(0, 0)
143
181
  entity.body.rotationVelocity; // Set with the matching parameter above, default is 0
144
182
  entity.body.mass; // Set with the matching parameter above, default is 1
145
183
  entity.body.inertia; // Set with the matching parameter above, default is 1
146
- entity.body.forceX; // Set with the matching parameter above, default is 0
147
- entity.body.forceY; // Set with the matching parameter above, default is 0
184
+ // Note that forces are reset after every frame
185
+ entity.body.force; // Set with the matching parameter above, default is Vector2(0, 0)
148
186
  entity.body.torque; // Set with the matching parameter above, default is 0
149
187
  ```
150
188
 
151
189
  Collisions to be added.
152
190
 
191
+ ## Camera
192
+
193
+ The camera decides what part of your game world gets rendered. Note that unlike most camera implementations of which positions are centered, Kippy's camera position is at the top-left of the camera. For example, camera at (0,0) and entity at (0,0) in Godot would show the entity at the center, while the same setup in Kippy would show the entity at the top-left. This is to be more aligned with how web and canvas positioning works.
194
+
195
+ The camera is available for use through every scene object:
196
+ ```js
197
+ // Get camera
198
+ const camera = scene.camera;
199
+
200
+ // The camera has these props to manage it:
201
+ camera.position; // The position vector of the camera, default is Vector2(0, 0)
202
+ camera.rotation; // Rotation of the camera, default is 0
203
+ camera.zoom // Camera zoom level, default is 1
204
+
205
+ // Convert a screen-based vector (mouse position for example) to world-based vector
206
+ camera.screenToWorld(input.pointer); // Return new vector
207
+ ```
208
+
153
209
  ### Animation
154
210
 
155
211
  To be added, for now mutate `entity.sprite` to swap sprites and create animations manually.
@@ -158,6 +214,10 @@ To be added, for now mutate `entity.sprite` to swap sprites and create animation
158
214
 
159
215
  To be added, for now use web's built-in `Audio` class.
160
216
 
217
+ ### Asset management
218
+
219
+ To be added.
220
+
161
221
  ## Copyrights and License
162
222
 
163
223
  Copyrights © 2026 Nguyen Phu Minh.
@@ -0,0 +1,17 @@
1
+ import { Scene } from "./scene";
2
+ import { Vector2 } from "./vector";
3
+ export interface CameraOptions {
4
+ position?: Vector2;
5
+ rotation?: number;
6
+ zoom?: number;
7
+ scene: Scene;
8
+ }
9
+ export declare class Camera {
10
+ position: Vector2;
11
+ rotation: number;
12
+ zoom: number;
13
+ scene: Scene;
14
+ constructor(options: CameraOptions);
15
+ apply(): void;
16
+ screenToWorld(screenPos: Vector2): Vector2;
17
+ }
package/dist/camera.js ADDED
@@ -0,0 +1,48 @@
1
+ import { Vector2 } from "./vector";
2
+ export class Camera {
3
+ position;
4
+ rotation;
5
+ zoom;
6
+ scene;
7
+ constructor(options) {
8
+ this.position = options.position ?? new Vector2(0, 0);
9
+ this.rotation = options.rotation ?? 0;
10
+ this.zoom = options.zoom ?? 1;
11
+ this.scene = options.scene;
12
+ }
13
+ apply() {
14
+ const ctx = this.scene.ctx;
15
+ if (ctx) {
16
+ const cx = ctx.canvas.width / 2;
17
+ const cy = ctx.canvas.height / 2;
18
+ // Move to center for zoom/rotation
19
+ ctx.translate(cx, cy);
20
+ // Zoom and rotate around center
21
+ ctx.scale(this.zoom, this.zoom);
22
+ ctx.rotate(this.rotation);
23
+ // Offset so position appears at top-left
24
+ ctx.translate(-this.position.x - cx / this.zoom, -this.position.y - cy / this.zoom);
25
+ }
26
+ }
27
+ screenToWorld(screenPos) {
28
+ const ctx = this.scene.ctx;
29
+ if (ctx) {
30
+ const cx = ctx.canvas.width / 2;
31
+ const cy = ctx.canvas.height / 2;
32
+ // Offset from center
33
+ let x = screenPos.x - cx;
34
+ let y = screenPos.y - cy;
35
+ // Undo rotation
36
+ const cos = Math.cos(-this.rotation);
37
+ const sin = Math.sin(-this.rotation);
38
+ const rotatedX = x * cos - y * sin;
39
+ const rotatedY = x * sin + y * cos;
40
+ // Undo zoom
41
+ const worldX = rotatedX / this.zoom;
42
+ const worldY = rotatedY / this.zoom;
43
+ // Add camera offset
44
+ return new Vector2(worldX + this.position.x + cx / this.zoom, worldY + this.position.y + cy / this.zoom);
45
+ }
46
+ return new Vector2(0, 0);
47
+ }
48
+ }
package/dist/entity.d.ts CHANGED
@@ -1,16 +1,15 @@
1
1
  import { EntityBody } from "./physics.js";
2
2
  import { Sprite } from "./sprite.js";
3
+ import { Vector2 } from "./vector.js";
3
4
  export interface EntityOptions {
4
5
  sprite?: Sprite;
5
- x?: number;
6
- y?: number;
6
+ position?: Vector2;
7
7
  rotation?: number;
8
8
  body?: EntityBody;
9
9
  }
10
10
  export declare class Entity {
11
11
  sprite?: Sprite;
12
- x: number;
13
- y: number;
12
+ position: Vector2;
14
13
  rotation: number;
15
14
  body?: EntityBody;
16
15
  constructor(options?: EntityOptions);
package/dist/entity.js CHANGED
@@ -1,20 +1,19 @@
1
+ import { Vector2 } from "./vector.js";
1
2
  export class Entity {
2
3
  sprite;
3
- x;
4
- y;
4
+ position;
5
5
  rotation;
6
6
  body;
7
7
  constructor(options = {}) {
8
8
  this.sprite = options.sprite;
9
- this.x = options.x ?? 0;
10
- this.y = options.y ?? 0;
9
+ this.position = options.position ?? new Vector2(0, 0);
11
10
  this.rotation = options.rotation ?? 0;
12
11
  this.body = options.body;
13
12
  }
14
13
  render(ctx) {
15
14
  if (this.sprite) {
16
15
  ctx.save();
17
- ctx.translate(this.x, this.y);
16
+ ctx.translate(this.position.x, this.position.y);
18
17
  ctx.rotate(this.rotation);
19
18
  ctx.drawImage(this.sprite.texture, -this.sprite.width / 2, -this.sprite.height / 2);
20
19
  ctx.restore();
package/dist/game.js CHANGED
@@ -19,6 +19,7 @@ export class Game {
19
19
  }
20
20
  setScene(scene) {
21
21
  this.scene?.exit();
22
+ scene.ctx = this.ctx;
22
23
  this.scene = scene;
23
24
  this.scene.init();
24
25
  }
@@ -38,7 +39,7 @@ export class Game {
38
39
  this.physics.update(this.scene.entities);
39
40
  // Render
40
41
  this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
41
- this.scene.render(this.ctx);
42
+ this.scene.render();
42
43
  }
43
44
  else {
44
45
  throw new Error("Can not run game loop without a scene");
package/dist/input.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Vector2 } from "./vector";
1
2
  export interface InputOptions {
2
3
  canvas: HTMLCanvasElement;
3
4
  }
@@ -6,8 +7,7 @@ export declare class Input {
6
7
  keys: Set<string>;
7
8
  keysPressed: Set<string>;
8
9
  keysReleased: Set<string>;
9
- pointerX: number;
10
- pointerY: number;
10
+ pointer: Vector2;
11
11
  pointers: Set<number>;
12
12
  pointersPressed: Set<number>;
13
13
  pointersReleased: Set<number>;
package/dist/input.js CHANGED
@@ -1,10 +1,10 @@
1
+ import { Vector2 } from "./vector";
1
2
  export class Input {
2
3
  canvas;
3
4
  keys = new Set(); // Key on hold
4
5
  keysPressed = new Set(); // Key pressed
5
6
  keysReleased = new Set(); // Key released
6
- pointerX = 0; // Mouse/touch coord x in canvas
7
- pointerY = 0; // Mouse/touch coord y in canvas
7
+ pointer = new Vector2(0, 0);
8
8
  pointers = new Set(); // Mouse/touch on hold
9
9
  pointersPressed = new Set(); // Mouse/touch pressed
10
10
  pointersReleased = new Set(); // Mouse/touch released
@@ -24,8 +24,8 @@ export class Input {
24
24
  // Mouse and touch
25
25
  this.canvas.addEventListener("mousemove", (e) => {
26
26
  const rect = this.canvas.getBoundingClientRect();
27
- this.pointerX = e.clientX - rect.left;
28
- this.pointerY = e.clientY - rect.top;
27
+ this.pointer.x = e.clientX - rect.left;
28
+ this.pointer.y = e.clientY - rect.top;
29
29
  });
30
30
  this.canvas.addEventListener("mousedown", (e) => {
31
31
  if (!this.pointers.has(e.button)) {
@@ -41,8 +41,8 @@ export class Input {
41
41
  e.preventDefault();
42
42
  const rect = this.canvas.getBoundingClientRect();
43
43
  const touch = e.touches[0];
44
- this.pointerX = touch.clientX - rect.left;
45
- this.pointerY = touch.clientY - rect.top;
44
+ this.pointer.x = touch.clientX - rect.left;
45
+ this.pointer.y = touch.clientY - rect.top;
46
46
  });
47
47
  this.canvas.addEventListener("touchstart", (e) => {
48
48
  e.preventDefault();
package/dist/physics.d.ts CHANGED
@@ -1,22 +1,19 @@
1
1
  import { Entity } from "./entity";
2
+ import { Vector2 } from "./vector";
2
3
  export interface RigidBodyOptions {
3
- velocityX?: number;
4
- velocityY?: number;
4
+ velocity?: Vector2;
5
5
  rotationVelocity?: number;
6
6
  mass?: number;
7
7
  inertia?: number;
8
- forceX?: number;
9
- forceY?: number;
8
+ force?: Vector2;
10
9
  torque?: number;
11
10
  }
12
11
  export declare class RigidBody {
13
- velocityX: number;
14
- velocityY: number;
12
+ velocity: Vector2;
15
13
  rotationVelocity: number;
16
14
  mass: number;
17
15
  inertia: number;
18
- forceX: number;
19
- forceY: number;
16
+ force: Vector2;
20
17
  torque: number;
21
18
  constructor(options?: RigidBodyOptions);
22
19
  }
package/dist/physics.js CHANGED
@@ -1,20 +1,17 @@
1
+ import { Vector2 } from "./vector";
1
2
  export class RigidBody {
2
- velocityX;
3
- velocityY;
3
+ velocity;
4
4
  rotationVelocity;
5
5
  mass;
6
6
  inertia;
7
- forceX;
8
- forceY;
7
+ force;
9
8
  torque;
10
9
  constructor(options = {}) {
11
- this.velocityX = options.velocityX ?? 0;
12
- this.velocityY = options.velocityY ?? 0;
10
+ this.velocity = options.velocity ?? new Vector2(0, 0);
13
11
  this.rotationVelocity = options.rotationVelocity ?? 0;
14
12
  this.mass = options.mass ?? 1;
15
13
  this.inertia = options.inertia ?? 1;
16
- this.forceX = options.forceX ?? 0;
17
- this.forceY = options.forceY ?? 0;
14
+ this.force = options.force ?? new Vector2(0, 0);
18
15
  this.torque = options.torque ?? 0;
19
16
  }
20
17
  }
@@ -23,13 +20,17 @@ export class Physics {
23
20
  for (const entity of entities) {
24
21
  if (entity.body instanceof RigidBody) {
25
22
  // Acceleration/apply force
26
- entity.body.velocityX += entity.body.forceX / entity.body.mass;
27
- entity.body.velocityY += entity.body.forceY / entity.body.mass;
23
+ entity.body.velocity.x += entity.body.force.x / entity.body.mass;
24
+ entity.body.velocity.y += entity.body.force.y / entity.body.mass;
28
25
  entity.body.rotationVelocity += entity.body.torque / entity.body.inertia;
29
26
  // Positional update
30
- entity.x += entity.body.velocityX;
31
- entity.y += entity.body.velocityY;
27
+ entity.position.x += entity.body.velocity.x;
28
+ entity.position.y += entity.body.velocity.y;
32
29
  entity.rotation += entity.body.rotationVelocity;
30
+ // Clear force
31
+ entity.body.force.x = 0;
32
+ entity.body.force.y = 0;
33
+ entity.body.torque = 0;
33
34
  }
34
35
  }
35
36
  }
package/dist/scene.d.ts CHANGED
@@ -1,10 +1,13 @@
1
+ import { Camera } from "./camera.js";
1
2
  import { Entity } from "./entity.js";
2
3
  export declare abstract class Scene {
4
+ ctx?: CanvasRenderingContext2D;
5
+ camera: Camera;
3
6
  entities: Entity[];
4
7
  init(): void;
5
8
  update(deltaTime: number): void;
6
9
  exit(): void;
7
10
  addEntity(entity: Entity): void;
8
11
  removeEntity(entity: Entity): void;
9
- render(ctx: CanvasRenderingContext2D): void;
12
+ render(): void;
10
13
  }
package/dist/scene.js CHANGED
@@ -1,4 +1,7 @@
1
+ import { Camera } from "./camera.js";
1
2
  export class Scene {
3
+ ctx;
4
+ camera = new Camera({ scene: this });
2
5
  entities = [];
3
6
  init() { }
4
7
  update(deltaTime) { }
@@ -9,9 +12,18 @@ export class Scene {
9
12
  removeEntity(entity) {
10
13
  this.entities = this.entities.filter(childEntities => childEntities !== entity);
11
14
  }
12
- render(ctx) {
13
- for (const entity of this.entities) {
14
- entity.render(ctx);
15
+ render() {
16
+ const ctx = this.ctx;
17
+ if (ctx) {
18
+ // Preserve canvas context
19
+ ctx.save();
20
+ // Apply camera config (position, zoom, rotate)
21
+ this.camera.apply();
22
+ for (const entity of this.entities) {
23
+ entity.render(ctx);
24
+ }
25
+ // Restore so camera/entity stuff does not affect original context
26
+ ctx.restore();
15
27
  }
16
28
  }
17
29
  }
@@ -0,0 +1,26 @@
1
+ export declare class Vector2 {
2
+ x: number;
3
+ y: number;
4
+ static ZERO: Vector2;
5
+ static ONE: Vector2;
6
+ static UP: Vector2;
7
+ static DOWN: Vector2;
8
+ static LEFT: Vector2;
9
+ static RIGHT: Vector2;
10
+ constructor(x: number, y: number);
11
+ add(other: Vector2): Vector2;
12
+ sub(other: Vector2): Vector2;
13
+ scale(scale: number): Vector2;
14
+ magnitude(): number;
15
+ normalize(): Vector2;
16
+ dot(other: Vector2): number;
17
+ distance(other: Vector2): number;
18
+ copy(): Vector2;
19
+ lerp(other: Vector2, scale: number): Vector2;
20
+ clamp(maxLength: number): Vector2;
21
+ rotate(angle: number): Vector2;
22
+ angle(): number;
23
+ angleTo(other: Vector2): number;
24
+ reflect(normal: Vector2): Vector2;
25
+ equals(other: Vector2): boolean;
26
+ }
package/dist/vector.js ADDED
@@ -0,0 +1,64 @@
1
+ export class Vector2 {
2
+ x;
3
+ y;
4
+ static ZERO = new Vector2(0, 0);
5
+ static ONE = new Vector2(1, 1);
6
+ static UP = new Vector2(0, -1);
7
+ static DOWN = new Vector2(0, 1);
8
+ static LEFT = new Vector2(-1, 0);
9
+ static RIGHT = new Vector2(1, 0);
10
+ constructor(x, y) {
11
+ this.x = x;
12
+ this.y = y;
13
+ }
14
+ add(other) {
15
+ return new Vector2(this.x + other.x, this.y + other.y);
16
+ }
17
+ sub(other) {
18
+ return new Vector2(this.x - other.x, this.y - other.y);
19
+ }
20
+ scale(scale) {
21
+ return new Vector2(this.x * scale, this.y * scale);
22
+ }
23
+ magnitude() {
24
+ return Math.sqrt(this.x * this.x + this.y * this.y);
25
+ }
26
+ normalize() {
27
+ const mag = this.magnitude();
28
+ return mag > 0 ? new Vector2(this.x / mag, this.y / mag) : new Vector2(0, 0);
29
+ }
30
+ dot(other) {
31
+ return this.x * other.x + this.y * other.y;
32
+ }
33
+ distance(other) {
34
+ return Math.sqrt((this.x - other.x) ** 2 + (this.y - other.y) ** 2);
35
+ }
36
+ copy() {
37
+ return new Vector2(this.x, this.y);
38
+ }
39
+ lerp(other, scale) {
40
+ return this.add(other.sub(this).scale(scale));
41
+ }
42
+ clamp(maxLength) {
43
+ const mag = this.magnitude();
44
+ return mag > maxLength ? this.scale(maxLength / mag) : this.copy();
45
+ }
46
+ rotate(angle) {
47
+ const cos = Math.cos(angle);
48
+ const sin = Math.sin(angle);
49
+ return new Vector2(this.x * cos - this.y * sin, this.x * sin + this.y * cos);
50
+ }
51
+ angle() {
52
+ return Math.atan2(this.y, this.x);
53
+ }
54
+ angleTo(other) {
55
+ return Math.atan2(other.y - this.y, other.x - this.x);
56
+ }
57
+ reflect(normal) {
58
+ const d = this.dot(normal);
59
+ return this.sub(normal.scale(2 * d));
60
+ }
61
+ equals(other) {
62
+ return this.x === other.x && this.y === other.y;
63
+ }
64
+ }
package/index.d.ts CHANGED
@@ -3,4 +3,6 @@ export * from "./dist/scene.js";
3
3
  export * from "./dist/entity.js";
4
4
  export * from "./dist/sprite.js";
5
5
  export * from "./dist/input.js";
6
- export * from "./dist/physics.js";
6
+ export * from "./dist/physics.js";
7
+ export * from "./dist/vector.js";
8
+ export * from "./dist/camera.js";
package/index.js CHANGED
@@ -4,3 +4,5 @@ export * from "./dist/entity.js";
4
4
  export * from "./dist/sprite.js";
5
5
  export * from "./dist/input.js";
6
6
  export * from "./dist/physics.js";
7
+ export * from "./dist/vector.js";
8
+ export * from "./dist/camera.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kippy",
3
- "version": "0.2.2",
3
+ "version": "0.4.0",
4
4
  "description": "Kippy 2D web game engine for JS",
5
5
  "keywords": [
6
6
  "kippy",