kippy 0.1.0 → 0.2.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/README.md CHANGED
@@ -81,6 +81,8 @@ entity.rotation; // Initialized from the "rotation" param above, 0 if not specif
81
81
 
82
82
  A sprite represents what an entity looks like, the "graphics part", you can create a sprite like this:
83
83
  ```js
84
+ import { Sprite } from "kippy";
85
+
84
86
  const sprite = new Sprite({
85
87
  texture, // Sprite's texture, can be HTMLImageElement, HTMLCanvasElement, OffscreenCanvas, ImageBitmap
86
88
  width, // Sprite's width, type number
@@ -93,38 +95,32 @@ entity.setSprite(sprite);
93
95
 
94
96
  ### Add controls
95
97
 
96
- Game controls like mouse presses, key presses, and mouse cursor traking (in the game canvas, not the web window) can be done with the `Input` class:
98
+ Game controls like mouse presses, key presses, and mouse cursor traking (in the game canvas, not the web window) can be done by using the input handler from your `game` instance:
97
99
  ```js
98
- const input = new Input({
99
- canvas
100
- });
101
-
102
- // You can assign it into a game during initialization
103
- const game = new Game({
104
- // other stuff
105
- input
106
- })
107
- // or after
108
- game.setInput(input);
100
+ const input = game.input;
109
101
  ```
110
102
 
111
103
  Then in a scene's `update` method, you can use these utilities to check for key presses:
112
104
  ```js
113
105
  // Keyboard
114
- input.isKeyDown(/* Character/key here */); // Key hold
115
- input.isKeyPressed(/* Character/key here */); // Key press
116
- input.isKeyReleased(/* Character/key here */); // Key released
106
+ input.isKeyDown(/* Character/key here */); // true if key is held, false otherwise
107
+ input.isKeyPressed(/* Character/key here */); // true if key is pressed, false otherwise
108
+ input.isKeyReleased(/* Character/key here */); // true if key is released, false otherwise
117
109
  // Mouse
118
- input.isMouseDown(/* 0 for left, 1 for right */); // Mouse hold
119
- input.isMousePressed(/* 0 for left, 1 for right */); // Mouse press
120
- input.isMouseReleased(/* 0 for left, 1 for right */); // Mouse released
110
+ input.isMouseDown(/* 0 for left, 1 for right */); // true if mouse is held, false otherwise
111
+ input.isMousePressed(/* 0 for left, 1 for right */); // true if mouse is pressed, false otherwise
112
+ input.isMouseReleased(/* 0 for left, 1 for right */); // true if mouse is released, false otherwise
121
113
  input.mouseX; // Current X position of mouse
122
114
  input.mouseY; // Current Y position of mouse
123
115
  ```
124
116
 
125
117
  ### Physics
126
118
 
127
- To be added, will have rigidBody for entities with collision and movement-related stuff.
119
+
120
+
121
+ ### Animation
122
+
123
+ To be added, for now call `setSprite` to swap sprites for animations.
128
124
 
129
125
  ### Audio
130
126
 
package/dist/entity.d.ts CHANGED
@@ -1,16 +1,19 @@
1
+ import { RigidBody } from "./physics.js";
1
2
  import { Sprite } from "./sprite.js";
2
3
  export interface EntityOptions {
3
4
  sprite?: Sprite;
4
5
  x?: number;
5
6
  y?: number;
6
7
  rotation?: number;
8
+ body?: RigidBody;
7
9
  }
8
10
  export declare class Entity {
9
11
  sprite?: Sprite;
10
12
  x: number;
11
13
  y: number;
12
14
  rotation: number;
13
- constructor(options: EntityOptions);
15
+ body?: RigidBody;
16
+ constructor(options?: EntityOptions);
14
17
  setSprite(sprite: Sprite): void;
15
18
  render(ctx: CanvasRenderingContext2D): void;
16
19
  }
package/dist/entity.js CHANGED
@@ -3,11 +3,13 @@ export class Entity {
3
3
  x;
4
4
  y;
5
5
  rotation;
6
- constructor(options) {
6
+ body;
7
+ constructor(options = {}) {
7
8
  this.sprite = options.sprite;
8
- this.x = options.x || 0;
9
- this.y = options.y || 0;
10
- this.rotation = options.rotation || 0;
9
+ this.x = options.x ?? 0;
10
+ this.y = options.y ?? 0;
11
+ this.rotation = options.rotation ?? 0;
12
+ this.body = options.body;
11
13
  }
12
14
  setSprite(sprite) {
13
15
  this.sprite = sprite;
package/dist/game.d.ts CHANGED
@@ -1,15 +1,18 @@
1
1
  import { Input } from "./input.js";
2
+ import { Physics } from "./physics.js";
2
3
  import { Scene } from "./scene.js";
3
4
  export interface GameOptions {
4
5
  canvas: HTMLCanvasElement;
5
6
  input?: Input;
7
+ physics?: Physics;
6
8
  }
7
9
  export declare class Game {
8
10
  canvas: HTMLCanvasElement;
9
11
  ctx: CanvasRenderingContext2D;
10
12
  scene?: Scene;
11
13
  lastTime: number;
12
- input?: Input;
14
+ input: Input;
15
+ physics: Physics;
13
16
  constructor(options: GameOptions);
14
17
  setScene(scene: Scene): void;
15
18
  setInput(input: Input): void;
package/dist/game.js CHANGED
@@ -1,13 +1,17 @@
1
+ import { Input } from "./input.js";
2
+ import { Physics } from "./physics.js";
1
3
  export class Game {
2
4
  canvas;
3
5
  ctx;
4
6
  scene;
5
7
  lastTime = 0;
6
8
  input;
9
+ physics;
7
10
  constructor(options) {
8
11
  this.canvas = options.canvas;
9
12
  this.ctx = this.canvas.getContext("2d");
10
- this.input = options.input;
13
+ this.input = options.input ?? new Input({ canvas: this.canvas });
14
+ this.physics = options.physics ?? new Physics();
11
15
  }
12
16
  setScene(scene) {
13
17
  this.scene?.exit();
@@ -24,12 +28,20 @@ export class Game {
24
28
  loop(timestamp) {
25
29
  const dt = (timestamp - this.lastTime) / 1000;
26
30
  this.lastTime = timestamp;
27
- // Update state, clear canvas, re-render
28
- this.scene?.update(dt);
29
- this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
30
- this.scene?.render(this.ctx);
31
- // Update input info
32
- this.input?.update();
31
+ if (this.scene) {
32
+ // Update input info
33
+ this.input.update();
34
+ // Update game logic
35
+ this.scene.update(dt);
36
+ // Update physics info
37
+ this.physics.update(this.scene.entities);
38
+ // Render
39
+ this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
40
+ this.scene.render(this.ctx);
41
+ }
42
+ else {
43
+ throw new Error("Can not run game loop without a scene");
44
+ }
33
45
  requestAnimationFrame(this.loop.bind(this));
34
46
  }
35
47
  }
@@ -0,0 +1,25 @@
1
+ import { Entity } from "./entity";
2
+ export interface RigidBodyOptions {
3
+ velocityX?: number;
4
+ velocityY?: number;
5
+ rotationVelocity?: number;
6
+ mass?: number;
7
+ inertia?: number;
8
+ forceX?: number;
9
+ forceY?: number;
10
+ torque?: number;
11
+ }
12
+ export declare class RigidBody {
13
+ velocityX: number;
14
+ velocityY: number;
15
+ rotationVelocity: number;
16
+ mass: number;
17
+ inertia: number;
18
+ forceX: number;
19
+ forceY: number;
20
+ torque: number;
21
+ constructor(options?: RigidBodyOptions);
22
+ }
23
+ export declare class Physics {
24
+ update(entities: Entity[]): void;
25
+ }
@@ -0,0 +1,36 @@
1
+ export class RigidBody {
2
+ velocityX;
3
+ velocityY;
4
+ rotationVelocity;
5
+ mass;
6
+ inertia;
7
+ forceX;
8
+ forceY;
9
+ torque;
10
+ constructor(options = {}) {
11
+ this.velocityX = options.velocityX ?? 0;
12
+ this.velocityY = options.velocityY ?? 0;
13
+ this.rotationVelocity = options.rotationVelocity ?? 0;
14
+ this.mass = options.mass ?? 1;
15
+ this.inertia = options.inertia ?? 1;
16
+ this.forceX = options.forceX ?? 0;
17
+ this.forceY = options.forceY ?? 0;
18
+ this.torque = options.torque ?? 0;
19
+ }
20
+ }
21
+ export class Physics {
22
+ update(entities) {
23
+ for (const entity of entities) {
24
+ if (entity.body instanceof RigidBody) {
25
+ // Acceleration/apply force
26
+ entity.body.velocityX += entity.body.forceX / entity.body.mass;
27
+ entity.body.velocityY += entity.body.forceY / entity.body.mass;
28
+ entity.body.rotationVelocity += entity.body.torque / entity.body.inertia;
29
+ // Positional update
30
+ entity.x += entity.body.velocityX;
31
+ entity.y += entity.body.velocityY;
32
+ entity.rotation += entity.body.rotationVelocity;
33
+ }
34
+ }
35
+ }
36
+ }
package/dist/sprite.js CHANGED
@@ -4,7 +4,7 @@ export class Sprite {
4
4
  height;
5
5
  constructor(options) {
6
6
  this.texture = options.texture;
7
- this.width = options.width || this.texture.width;
8
- this.height = options.height || this.texture.height;
7
+ this.width = options.width ?? this.texture.width;
8
+ this.height = options.height ?? this.texture.height;
9
9
  }
10
10
  }
package/index.d.ts CHANGED
@@ -3,3 +3,4 @@ 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";
package/index.js CHANGED
@@ -3,3 +3,4 @@ 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";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kippy",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Kippy 2D web game engine for JS",
5
5
  "keywords": [
6
6
  "kippy",