kippy 0.2.0 → 0.2.2
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/README.md +43 -11
- package/dist/entity.d.ts +3 -4
- package/dist/entity.js +0 -3
- package/dist/game.d.ts +1 -1
- package/dist/game.js +4 -3
- package/dist/input.d.ts +8 -8
- package/dist/input.js +40 -20
- package/dist/physics.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,6 +31,9 @@ const game = new Game({
|
|
|
31
31
|
|
|
32
32
|
// Start the game loop
|
|
33
33
|
game.start();
|
|
34
|
+
|
|
35
|
+
// You can also swap canvas if you want
|
|
36
|
+
// game.setCanvas(someOtherCanvas);
|
|
34
37
|
```
|
|
35
38
|
|
|
36
39
|
### Create a scene
|
|
@@ -63,7 +66,8 @@ const entity = new Entity({
|
|
|
63
66
|
sprite, // Entity's sprite to be rendered, type Sprite
|
|
64
67
|
x, // Entity's x position (centered), type number
|
|
65
68
|
y, // Entity's y position (centered), type number
|
|
66
|
-
rotation // Entity's rotation in radians, type number
|
|
69
|
+
rotation, // Entity's rotation in radians, type number
|
|
70
|
+
body, // Entity's physical body, type EntityBody
|
|
67
71
|
});
|
|
68
72
|
|
|
69
73
|
// Add it to a scene
|
|
@@ -84,18 +88,18 @@ A sprite represents what an entity looks like, the "graphics part", you can crea
|
|
|
84
88
|
import { Sprite } from "kippy";
|
|
85
89
|
|
|
86
90
|
const sprite = new Sprite({
|
|
87
|
-
texture, // Sprite's texture
|
|
91
|
+
texture, // Sprite's texture - HTMLImageElement, HTMLCanvasElement, OffscreenCanvas, ImageBitmap
|
|
88
92
|
width, // Sprite's width, type number
|
|
89
93
|
height // Sprite's height, type number
|
|
90
94
|
});
|
|
91
95
|
|
|
92
96
|
// Set sprite for an entity
|
|
93
|
-
entity.
|
|
97
|
+
entity.sprite = sprite;
|
|
94
98
|
```
|
|
95
99
|
|
|
96
100
|
### Add controls
|
|
97
101
|
|
|
98
|
-
Game controls like mouse presses, key presses, and
|
|
102
|
+
Game controls like mouse presses, key presses, touch, and cursor traking (in the game canvas, not the web window) can be done by using the input handler from your `game` instance:
|
|
99
103
|
```js
|
|
100
104
|
const input = game.input;
|
|
101
105
|
```
|
|
@@ -106,21 +110,49 @@ Then in a scene's `update` method, you can use these utilities to check for key
|
|
|
106
110
|
input.isKeyDown(/* Character/key here */); // true if key is held, false otherwise
|
|
107
111
|
input.isKeyPressed(/* Character/key here */); // true if key is pressed, false otherwise
|
|
108
112
|
input.isKeyReleased(/* Character/key here */); // true if key is released, false otherwise
|
|
109
|
-
// Mouse
|
|
110
|
-
input.
|
|
111
|
-
input.
|
|
112
|
-
input.
|
|
113
|
-
input.
|
|
114
|
-
input.
|
|
113
|
+
// Mouse/touch
|
|
114
|
+
input.isPointerDown(/* 0 for left, 1 for right, 2 for touch */); // true if held, false otherwise
|
|
115
|
+
input.isPointerPressed(/* 0 for left, 1 for right, 2 for touch */); // true if pressed, false otherwise
|
|
116
|
+
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
|
|
115
119
|
```
|
|
116
120
|
|
|
117
121
|
### Physics
|
|
118
122
|
|
|
123
|
+
For movements, currently you can create a `RigidBody`:
|
|
124
|
+
```js
|
|
125
|
+
// Create a rigid body
|
|
126
|
+
const rigidBody = new RigidBody({
|
|
127
|
+
velocityX, // X velocity, type number
|
|
128
|
+
velocityY, // Y velocity, type number
|
|
129
|
+
rotationVelocity, // Angular/rotation velocity, type number
|
|
130
|
+
mass, // Entity's mass, type number
|
|
131
|
+
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
|
|
134
|
+
torque, // Entity's torque/rotational force, type number
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// Attach body to an entity
|
|
138
|
+
entity.body = rigidBody;
|
|
139
|
+
|
|
140
|
+
// 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
|
|
143
|
+
entity.body.rotationVelocity; // Set with the matching parameter above, default is 0
|
|
144
|
+
entity.body.mass; // Set with the matching parameter above, default is 1
|
|
145
|
+
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
|
|
148
|
+
entity.body.torque; // Set with the matching parameter above, default is 0
|
|
149
|
+
```
|
|
119
150
|
|
|
151
|
+
Collisions to be added.
|
|
120
152
|
|
|
121
153
|
### Animation
|
|
122
154
|
|
|
123
|
-
To be added, for now
|
|
155
|
+
To be added, for now mutate `entity.sprite` to swap sprites and create animations manually.
|
|
124
156
|
|
|
125
157
|
### Audio
|
|
126
158
|
|
package/dist/entity.d.ts
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EntityBody } from "./physics.js";
|
|
2
2
|
import { Sprite } from "./sprite.js";
|
|
3
3
|
export interface EntityOptions {
|
|
4
4
|
sprite?: Sprite;
|
|
5
5
|
x?: number;
|
|
6
6
|
y?: number;
|
|
7
7
|
rotation?: number;
|
|
8
|
-
body?:
|
|
8
|
+
body?: EntityBody;
|
|
9
9
|
}
|
|
10
10
|
export declare class Entity {
|
|
11
11
|
sprite?: Sprite;
|
|
12
12
|
x: number;
|
|
13
13
|
y: number;
|
|
14
14
|
rotation: number;
|
|
15
|
-
body?:
|
|
15
|
+
body?: EntityBody;
|
|
16
16
|
constructor(options?: EntityOptions);
|
|
17
|
-
setSprite(sprite: Sprite): void;
|
|
18
17
|
render(ctx: CanvasRenderingContext2D): void;
|
|
19
18
|
}
|
package/dist/entity.js
CHANGED
package/dist/game.d.ts
CHANGED
|
@@ -14,8 +14,8 @@ export declare class Game {
|
|
|
14
14
|
input: Input;
|
|
15
15
|
physics: Physics;
|
|
16
16
|
constructor(options: GameOptions);
|
|
17
|
+
setCanvas(canvas: HTMLCanvasElement): void;
|
|
17
18
|
setScene(scene: Scene): void;
|
|
18
|
-
setInput(input: Input): void;
|
|
19
19
|
start(): void;
|
|
20
20
|
loop(timestamp: number): void;
|
|
21
21
|
}
|
package/dist/game.js
CHANGED
|
@@ -13,14 +13,15 @@ export class Game {
|
|
|
13
13
|
this.input = options.input ?? new Input({ canvas: this.canvas });
|
|
14
14
|
this.physics = options.physics ?? new Physics();
|
|
15
15
|
}
|
|
16
|
+
setCanvas(canvas) {
|
|
17
|
+
this.canvas = canvas;
|
|
18
|
+
this.ctx = this.canvas.getContext("2d");
|
|
19
|
+
}
|
|
16
20
|
setScene(scene) {
|
|
17
21
|
this.scene?.exit();
|
|
18
22
|
this.scene = scene;
|
|
19
23
|
this.scene.init();
|
|
20
24
|
}
|
|
21
|
-
setInput(input) {
|
|
22
|
-
this.input = input;
|
|
23
|
-
}
|
|
24
25
|
start() {
|
|
25
26
|
requestAnimationFrame(this.loop.bind(this));
|
|
26
27
|
}
|
package/dist/input.d.ts
CHANGED
|
@@ -6,17 +6,17 @@ export declare class Input {
|
|
|
6
6
|
keys: Set<string>;
|
|
7
7
|
keysPressed: Set<string>;
|
|
8
8
|
keysReleased: Set<string>;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
pointerX: number;
|
|
10
|
+
pointerY: number;
|
|
11
|
+
pointers: Set<number>;
|
|
12
|
+
pointersPressed: Set<number>;
|
|
13
|
+
pointersReleased: Set<number>;
|
|
14
14
|
constructor(options: InputOptions);
|
|
15
15
|
update(): void;
|
|
16
16
|
isKeyDown(key: string): boolean;
|
|
17
17
|
isKeyPressed(key: string): boolean;
|
|
18
18
|
isKeyReleased(key: string): boolean;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
isPointerDown(button?: number): boolean;
|
|
20
|
+
isPointerPressed(button?: number): boolean;
|
|
21
|
+
isPointerReleased(button?: number): boolean;
|
|
22
22
|
}
|
package/dist/input.js
CHANGED
|
@@ -3,11 +3,11 @@ export class Input {
|
|
|
3
3
|
keys = new Set(); // Key on hold
|
|
4
4
|
keysPressed = new Set(); // Key pressed
|
|
5
5
|
keysReleased = new Set(); // Key released
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
pointerX = 0; // Mouse/touch coord x in canvas
|
|
7
|
+
pointerY = 0; // Mouse/touch coord y in canvas
|
|
8
|
+
pointers = new Set(); // Mouse/touch on hold
|
|
9
|
+
pointersPressed = new Set(); // Mouse/touch pressed
|
|
10
|
+
pointersReleased = new Set(); // Mouse/touch released
|
|
11
11
|
constructor(options) {
|
|
12
12
|
this.canvas = options.canvas;
|
|
13
13
|
// Keyboard
|
|
@@ -21,21 +21,40 @@ export class Input {
|
|
|
21
21
|
this.keys.delete(e.key);
|
|
22
22
|
this.keysReleased.add(e.key);
|
|
23
23
|
});
|
|
24
|
-
// Mouse
|
|
24
|
+
// Mouse and touch
|
|
25
25
|
this.canvas.addEventListener("mousemove", (e) => {
|
|
26
26
|
const rect = this.canvas.getBoundingClientRect();
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
27
|
+
this.pointerX = e.clientX - rect.left;
|
|
28
|
+
this.pointerY = e.clientY - rect.top;
|
|
29
29
|
});
|
|
30
30
|
this.canvas.addEventListener("mousedown", (e) => {
|
|
31
|
-
if (!this.
|
|
32
|
-
this.
|
|
31
|
+
if (!this.pointers.has(e.button)) {
|
|
32
|
+
this.pointersPressed.add(e.button);
|
|
33
33
|
}
|
|
34
|
-
this.
|
|
34
|
+
this.pointers.add(e.button);
|
|
35
35
|
});
|
|
36
36
|
this.canvas.addEventListener("mouseup", (e) => {
|
|
37
|
-
this.
|
|
38
|
-
this.
|
|
37
|
+
this.pointers.delete(e.button);
|
|
38
|
+
this.pointersReleased.add(e.button);
|
|
39
|
+
});
|
|
40
|
+
this.canvas.addEventListener("touchmove", (e) => {
|
|
41
|
+
e.preventDefault();
|
|
42
|
+
const rect = this.canvas.getBoundingClientRect();
|
|
43
|
+
const touch = e.touches[0];
|
|
44
|
+
this.pointerX = touch.clientX - rect.left;
|
|
45
|
+
this.pointerY = touch.clientY - rect.top;
|
|
46
|
+
});
|
|
47
|
+
this.canvas.addEventListener("touchstart", (e) => {
|
|
48
|
+
e.preventDefault();
|
|
49
|
+
if (!this.pointers.has(2)) {
|
|
50
|
+
this.pointersPressed.add(2);
|
|
51
|
+
}
|
|
52
|
+
this.pointers.add(2);
|
|
53
|
+
});
|
|
54
|
+
this.canvas.addEventListener("touchend", (e) => {
|
|
55
|
+
e.preventDefault();
|
|
56
|
+
this.pointers.delete(2);
|
|
57
|
+
this.pointersReleased.add(2);
|
|
39
58
|
});
|
|
40
59
|
// Prevent right-click menu
|
|
41
60
|
this.canvas.addEventListener("contextmenu", (e) => {
|
|
@@ -46,7 +65,8 @@ export class Input {
|
|
|
46
65
|
update() {
|
|
47
66
|
this.keysPressed.clear();
|
|
48
67
|
this.keysReleased.clear();
|
|
49
|
-
this.
|
|
68
|
+
this.pointersPressed.clear();
|
|
69
|
+
this.pointersReleased.clear();
|
|
50
70
|
}
|
|
51
71
|
// Helper methods
|
|
52
72
|
isKeyDown(key) {
|
|
@@ -58,13 +78,13 @@ export class Input {
|
|
|
58
78
|
isKeyReleased(key) {
|
|
59
79
|
return this.keysReleased.has(key);
|
|
60
80
|
}
|
|
61
|
-
|
|
62
|
-
return this.
|
|
81
|
+
isPointerDown(button = 0) {
|
|
82
|
+
return this.pointers.has(button);
|
|
63
83
|
}
|
|
64
|
-
|
|
65
|
-
return this.
|
|
84
|
+
isPointerPressed(button = 0) {
|
|
85
|
+
return this.pointersPressed.has(button);
|
|
66
86
|
}
|
|
67
|
-
|
|
68
|
-
return this.
|
|
87
|
+
isPointerReleased(button = 0) {
|
|
88
|
+
return this.pointersReleased.has(button);
|
|
69
89
|
}
|
|
70
90
|
}
|
package/dist/physics.d.ts
CHANGED