Skip to content

Commit

Permalink
fix: cli + better docs
Browse files Browse the repository at this point in the history
  • Loading branch information
load1n9 committed Mar 10, 2023
1 parent bd2fab3 commit 3fcbc2f
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 27 deletions.
12 changes: 6 additions & 6 deletions cli/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
### Caviar CLI
# Caviar CLI

#### cli to generate caviar projects, plugins and scenes
## Command-line interface to generate caviar projects, plugins and scenes

## install:
### install

```
```sh
deno install -Af --unstable -n caviar https://deno.land/x/caviar/cli/caviar.ts
```

## Usage:
## Usage

generate project:

```shell
```sh
caviar generate project
```
2 changes: 1 addition & 1 deletion cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
Confirm,
Input,
Number,
} from "https://deno.land/x/cliffy@v0.20.1/prompt/mod.ts";
} from "https://deno.land/x/cliffy@v0.25.7/prompt/mod.ts";
import { createScene } from "./structs/Scene.ts";
import { createWorld } from "./structs/World.ts";
import { HTML } from "./structs/html.ts";
Expand Down
11 changes: 11 additions & 0 deletions cli/example/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { World } from "https://deno.land/x/caviar@2.6.4/mod.ts";
import { Boot } from "./src/scenes/Boot.ts";

const example = new World({
title: "example",
width: 800,
height: 600,
resizable: true,
}, [Boot]);

await example.start();
9 changes: 9 additions & 0 deletions cli/example/src/scenes/Boot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Scene } from "https://deno.land/x/caviar@2.6.4/mod.ts";

export class Boot extends Scene {
setup() {
}

update() {
}
}
10 changes: 6 additions & 4 deletions cli/structs/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ export const createScene = (name: string, web = false) =>
? `"https://deno.land/x/caviar@${VERSION}/web/dist/mod.js"`
: `"https://deno.land/x/caviar@${VERSION}/mod.ts"`
};
export class ${name} extends Scene {
setup() {
}
update() {
}
setup() {
}
update() {
}
}`;
10 changes: 6 additions & 4 deletions cli/structs/World.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ export const createWorld = (
: `"https://deno.land/x/caviar@${VERSION}/mod.ts"`
};
import { Boot } from "./src/scenes/Boot.${web ? "js" : "ts"}";
const ${name} = new World({
title: "${title}",
width: ${width},
height: ${height},
resizable: ${resizable},
title: "${title}",
width: ${width},
height: ${height},
resizable: ${resizable},
}, [Boot]);
await ${name}.start();`;
2 changes: 1 addition & 1 deletion cli/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = "2.6.4";
export const VERSION = "2.6.5";
16 changes: 16 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,21 @@
"example_sprite": "deno run -A --unstable examples/sprite.ts",
"example_texturesprite": "deno run -A --unstable examples/texturesprite.ts",
"example_transparent_window": "deno run -A --unstable examples/transparent_window.ts"
},
"lint": {
"files": {
"exclude": [
"./web",
"./docs"
]
}
},
"check": {
"files": {
"exclude": [
"./web",
"./docs"
]
}
}
}
3 changes: 2 additions & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { fromFileUrl } from "https://deno.land/std@0.179.0/path/mod.ts";
export * from "https://deno.land/x/gluten@0.1.5/mod.ts";
export * from "https://deno.land/x/dwm@0.3.2/mod.ts";
export * from "https://deno.land/x/dwm@0.3.2/mod.ts";
4 changes: 2 additions & 2 deletions docs/docs/deno/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ await test.start();

run this with the unstable flag

```
```sh
deno run -A --unstable main.ts
```

![](https://i.ibb.co/RzSZfBH/Screenshot-2021-12-10-112659.png)
![preview](https://i.ibb.co/RzSZfBH/Screenshot-2021-12-10-112659.png)
79 changes: 76 additions & 3 deletions src/World.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,51 @@ import { hexToRGBA, printBanner, sleepSync } from "./utils/mod.ts";
import { VERSION } from "./version.ts";

export class World extends WebGLCanvas {
/**
* Frames displayed per second
*/
FPS = 500;

/**
* Window parameters
*/
params: CreateWindowOptions;

/**
* Collection of scenes in the world
*/
scenes: Array<typeof Scene>;

/**
* Current scene displayed
*/
currentScene: Scene;

/**
* The WebGl renderer that powers the engine
*/
renderer: WebGLRenderer2D;

/**
* Manages Key Events
*/
keyManager: KeyManager;

/**
* List of available plugins
*/
// deno-lint-ignore no-explicit-any
plugins: any = {};

/**
* All plugins currently loaded
*/
// deno-lint-ignore no-explicit-any
loadedPlugins: any = [];

/**
* Whether or not to rerender
*/
reRender = false;

#showBanner = true;
Expand All @@ -35,6 +70,9 @@ export class World extends WebGLCanvas {
this.renderer = new WebGLRenderer2D(this);
}

/**
* Launches the World
*/
async start(): Promise<void> {
if (this.#showBanner) printBanner(VERSION);
this.setup();
Expand Down Expand Up @@ -65,8 +103,6 @@ export class World extends WebGLCanvas {

_draw(): void {
this._fps()();
// if (this.shouldClose()) return;
// this.updateEvents();
this.updateProgramLifeCycle();
if (this.reRender) {
this.renderer.start(this.currentScene.entities);
Expand All @@ -81,6 +117,9 @@ export class World extends WebGLCanvas {
requestAnimationFrame(this._draw.bind(this));
}

/**
* Sets the amount of Frames to render per second
*/
setFPS(fps: number): void {
this.FPS = fps;
}
Expand All @@ -98,10 +137,20 @@ export class World extends WebGLCanvas {
};
}

/**
* Checks if a key is currently down
*/
keyDown(e: WindowKeyboardEvent): boolean {
return this.currentScene.keyDown(e.key);
}

/**
* Sets the current scene to the specified scene index
*/
setScene(id: number): void;
/**
* Sets the current scene to the specified scene with the given name
*/
setScene(name: string): void;
setScene(scene: number | string): void {
if (typeof scene === "string") {
for (const s of this.scenes) {
Expand All @@ -116,11 +165,17 @@ export class World extends WebGLCanvas {
this.setup();
}

/**
* Adds a plugin to the game
*/
// deno-lint-ignore no-explicit-any
loadPlugin(name: string, plugin: any): void {
this.plugins[name] = plugin;
}

/**
* Uses an loaded plugin to the game
*/
// deno-lint-ignore no-explicit-any
usePlugin(name: string): any {
const plug = new this.plugins[name](this);
Expand All @@ -139,21 +194,39 @@ export class World extends WebGLCanvas {
this.currentScene._mouseMotion(e);
}

/**
* Sets up the current scene
*/
setup(): void {
this.currentScene.setup();
}

/**
* Updates the program's life cycle
*/
updateProgramLifeCycle(): void {
this.currentScene.tick();
this.currentScene.update();
}

/**
* Sets the background to the given hex code
*/
setBackground(color: string): void;

/**
* Sets the background to the given color
*/
setBackground(color: RGBA): void;
setBackground(color: string | RGBA): void {
this.renderer.setBackground(
typeof color === "string" ? hexToRGBA(color) : color,
);
}

/**
* Disables the caviar launch banner
*/
disableBanner() {
this.#showBanner = false;
}
Expand Down
46 changes: 45 additions & 1 deletion src/entities/Entity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
/**
* An Entity represents any object that can be used in a scene
*/
export abstract class Entity {
id: string;
/**
* Identifier for the entity
*/
readonly id: string;
#x: number;
#y: number;
#z: number;

/**
* Width of the entity
*/
width = 0;

/**
* Height of the entity
*/
height = 0;

#interactive = false;
Expand All @@ -14,43 +28,73 @@ export abstract class Entity {
this.#y = y;
this.#z = 1;
}

set x(x: number) {
this.#x = x;
}

/**
* X coordinate of the entity
*/
get x(): number {
return this.#x;
}

set y(y: number) {
this.#y = y;
}

/**
* Y coordinate of the entity
*/
get y() {
return this.#y;
}

set z(z: number) {
this.#z = z;
}

/**
* Z coordinate of the entity
*/
get z(): number {
return this.#z;
}

/**
* Whether or not the entity is interactive
*/
get interactive() {
return this.#interactive;
}

/**
* Sets the interactivity to a boolean
*/
setInteractive(interactive = true) {
this.#interactive = interactive;
}

/**
* Sets the entities position
*/
setPosition(x: number, y: number, z?: number) {
this.#x = x;
this.#y = y;
if (z) this.#z = z;
}

/**
* Logic for determining mouse collision
*/
collides(_x: number, _y: number) {
return false;
}

/**
* Runs when the entity is clicked
*/
onClick() {
}
}
Loading

0 comments on commit 3fcbc2f

Please sign in to comment.