Skip to content

Commit

Permalink
0.1.1 (#3)
Browse files Browse the repository at this point in the history
* Remove hide props

* Update default format version

* Fix format version type

* Add animations

* Add animation controllers

* Add animations accessor
  • Loading branch information
ink0rr authored Sep 28, 2023
1 parent c153a5b commit 4af8e7d
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 9 deletions.
59 changes: 59 additions & 0 deletions lib/bedrock/animation_controllers/AnimationControllers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Project } from "../../core/Project.ts";
import { AddonFile } from "../AddonFile.ts";
import { StringOrRecord } from "../common/StringOrRecord.ts";

export class AnimationControllers extends AddonFile {
#data: AnimationControllersSchema;
constructor(fileName: string) {
super(fileName);
this.#data = {
format_version: "1.10.0",
animation_controllers: {},
};

Project.onSave(({ writeBP }) => {
writeBP(`animation_controllers/${this.fileName}.json`, this.#data);
});
}

/**
* @param id The id of the animation controller, without the `controller.animation.` prefix
*/
add(id: string, controller?: AnimationController) {
controller ??= {
states: {},
};
return this.#data.animation_controllers[`controller.animation.${id}`] = controller;
}

/**
* @param id The id of the animation controller, without the `controller.animation.` prefix
*/
get(id: string) {
return this.#data.animation_controllers[`controller.animation.${id}`];
}

/**
* @param id The id of the animation controller, without the `controller.animation.` prefix
*/
delete(id: string) {
delete this.#data.animation_controllers[`controller.animation.${id}`];
}
}

export interface AnimationControllersSchema {
format_version: string;
animation_controllers: Record<string, AnimationController>;
}

export interface AnimationController {
initial_state?: string;
states: Record<string, AnimationControllerState>;
}

export interface AnimationControllerState {
animations?: StringOrRecord[];
on_entry?: string[];
on_exit?: string[];
transitions?: Record<string, string>[];
}
58 changes: 58 additions & 0 deletions lib/bedrock/animations/Animations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Project } from "../../core/Project.ts";
import { AddonFile } from "../AddonFile.ts";

export class Animations extends AddonFile {
#data: AnimationsSchema;
/**
* @param fileName File name without extension
*/
constructor(fileName: string) {
super(fileName);
this.#data = {
format_version: "1.10.0",
animations: {},
};

Project.onSave(({ writeBP }) => {
writeBP(`animations/${this.fileName}.json`, this.#data);
});
}

/**
* @param id The id of the animation, without the `animation.` prefix
*/
add(id: string, animation?: Animation) {
animation ??= {
timeline: {},
};
return this.#data.animations[`animation.${id}`] = animation;
}

/**
* @param id The id of the animation, without the `animation.` prefix
*/
get(id: string) {
return this.#data.animations[`animation.${id}`];
}

/**
* @param id The id of the animation, without the `animation.` prefix
*/
delete(id: string) {
delete this.#data.animations[`animation.${id}`];
}
}

export interface AnimationsSchema {
format_version: string;
animations: Record<string, Animation>;
}

export interface Animation {
anim_time_update?: string | number;
animation_length?: number;
loop?: boolean;
loop_delay?: string | number;
start_delay?: string | number;
timeline: Record<number, string[]>;
}
12 changes: 10 additions & 2 deletions lib/bedrock/attachable/Attachable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ export class Attachable extends IdentifierAddonFile {
this.#attachable.scripts = value;
}

get animations() {
return this.#attachable.animations;
}

set animations(value) {
this.#attachable.animations = value;
}

addAnimation(key: string, animation: string, condition?: string | true) {
addAnimation(this.#attachable, key, animation, condition);
}
Expand Down Expand Up @@ -127,22 +135,22 @@ export class Attachable extends IdentifierAddonFile {
}

export interface AttachableSchema {
format_version: "1.10.0";
format_version: string;
"minecraft:attachable": {
description: {
identifier: string;
item?: ItemIdentifier;
materials?: Record<string, string>;
textures?: Record<string, string>;
geometry?: Record<string, string>;
animations?: Record<string, string>;
scripts?: {
animate?: StringOrRecord[];
parent_setup?: string;
pre_animation?: string[];
should_update_bones_and_effects_offscreen?: string | number | boolean;
should_update_effects_offscreen?: string | number | boolean;
};
animations?: Record<string, string>;
render_controllers?: StringOrRecord[];
queryable_geometry?: string;
};
Expand Down
6 changes: 2 additions & 4 deletions lib/bedrock/entity/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { IdentifierAddonFile } from "../AddonFile.ts";
import { EntityBehavior } from "./EntityBehavior.ts";
import { EntityResource } from "./EntityResource.ts";

type HideProps = "identifier" | "fileName";

export class Entity extends IdentifierAddonFile {
#behavior: EntityBehavior;
#resource: EntityResource;
Expand Down Expand Up @@ -40,11 +38,11 @@ export class Entity extends IdentifierAddonFile {
this.#resource.fileName = value;
}

get behavior(): Omit<EntityBehavior, HideProps> {
get behavior(): EntityBehavior {
return this.#behavior;
}

get resource(): Omit<EntityResource, HideProps> {
get resource(): EntityResource {
return this.#resource;
}
}
2 changes: 1 addition & 1 deletion lib/bedrock/entity/EntityBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class EntityBehavior extends IdentifierAddonFile {
constructor(identifier: string, dir?: string) {
super(identifier, dir);
this.#data = {
format_version: "1.16.0",
format_version: "1.19.80",
"minecraft:entity": {
description: {
identifier,
Expand Down
12 changes: 10 additions & 2 deletions lib/bedrock/entity/EntityResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ export class EntityResource extends IdentifierAddonFile {
this.#entity.scripts = value;
}

get animations() {
return this.#entity.animations;
}

set animations(value) {
this.#entity.animations = value;
}

addAnimation(key: string, animation: string, condition?: string | true) {
addAnimation(this.#entity, key, animation, condition);
}
Expand Down Expand Up @@ -178,14 +186,13 @@ export class EntityResource extends IdentifierAddonFile {
}

export interface EntityResourceSchema {
format_version: "1.10.0";
format_version: string;
"minecraft:client_entity": {
description: {
identifier: string;
materials?: Record<string, string>;
textures?: Record<string, string>;
geometry?: Record<string, string>;
animations?: Record<string, string>;
scripts?: {
animate?: StringOrRecord[];
initialize?: string[];
Expand All @@ -195,6 +202,7 @@ export interface EntityResourceSchema {
should_update_effects_offscreen?: string | number | boolean;
variables?: Record<string, "public">;
};
animations?: Record<string, string>;
particle_effects?: Record<string, string>;
particle_emitters?: Record<string, string>;
sound_effects?: Record<string, string>;
Expand Down
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export * from "./lib/bedrock/common/RawText.ts";
export * from "./lib/bedrock/common/SpellEffects.ts";
export * from "./lib/bedrock/common/query.ts";

export * from "./lib/bedrock/animation_controllers/AnimationControllers.ts";
export * from "./lib/bedrock/animations/Animations.ts";
export * from "./lib/bedrock/attachable/Attachable.ts";
export * from "./lib/bedrock/entity/Entity.ts";
export * from "./lib/bedrock/entity/EntityBehavior.ts";
Expand Down

0 comments on commit 4af8e7d

Please sign in to comment.