123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- import {
- web3,
- AddEntity,
- ApplySystem,
- InitializeComponent,
- } from "../../clients/bolt-sdk/lib";
- import { Direction, Framework } from "../framework";
- import { expect } from "chai";
- export function ecs(framework: Framework) {
- describe("ECS", () => {
- it("Add entity 1", async () => {
- const addEntity = await AddEntity({
- payer: framework.provider.wallet.publicKey,
- world: framework.worldPda,
- connection: framework.provider.connection,
- });
- await framework.provider.sendAndConfirm(addEntity.transaction);
- framework.entity1Pda = addEntity.entityPda; // Saved for later
- });
- it("Add entity 2", async () => {
- const addEntity = await AddEntity({
- payer: framework.provider.wallet.publicKey,
- world: framework.worldPda,
- connection: framework.provider.connection,
- });
- await framework.provider.sendAndConfirm(addEntity.transaction);
- framework.entity2Pda = addEntity.entityPda; // Saved for later
- });
- it("Add entity 3", async () => {
- const addEntity = await AddEntity({
- payer: framework.provider.wallet.publicKey,
- world: framework.worldPda,
- connection: framework.provider.connection,
- });
- await framework.provider.sendAndConfirm(addEntity.transaction);
- });
- it("Add entity 4 (with seed)", async () => {
- const addEntity = await AddEntity({
- payer: framework.provider.wallet.publicKey,
- world: framework.worldPda,
- seed: Buffer.from("custom-seed"),
- connection: framework.provider.connection,
- });
- await framework.provider.sendAndConfirm(addEntity.transaction);
- framework.entity4Pda = addEntity.entityPda;
- });
- it("Initialize Velocity Component on Entity 1 (with seed)", async () => {
- const initializeComponent = await InitializeComponent({
- payer: framework.provider.wallet.publicKey,
- entity: framework.entity1Pda,
- componentId: framework.exampleComponentVelocity.programId,
- seed: "component-velocity",
- });
- await framework.provider.sendAndConfirm(initializeComponent.transaction);
- framework.componentVelocityEntity1Pda = initializeComponent.componentPda; // Saved for later
- });
- it("Initialize Position Component on Entity 1", async () => {
- const initializeComponent = await InitializeComponent({
- payer: framework.provider.wallet.publicKey,
- entity: framework.entity1Pda,
- componentId: framework.exampleComponentPosition.programId,
- });
- await framework.provider.sendAndConfirm(initializeComponent.transaction);
- framework.componentPositionEntity1Pda = initializeComponent.componentPda; // Saved for later
- });
- it("Initialize Position Component on Entity 2", async () => {
- const initializeComponent = await InitializeComponent({
- payer: framework.provider.wallet.publicKey,
- entity: framework.entity2Pda,
- componentId: framework.exampleComponentPosition.programId,
- });
- await framework.provider.sendAndConfirm(initializeComponent.transaction);
- });
- it("Initialize Position Component on Entity 4", async () => {
- const initializeComponent = await InitializeComponent({
- payer: framework.provider.wallet.publicKey,
- entity: framework.entity4Pda,
- componentId: framework.exampleComponentPosition.programId,
- });
- await framework.provider.sendAndConfirm(initializeComponent.transaction);
- framework.componentPositionEntity4Pda = initializeComponent.componentPda; // Saved for later
- });
- it("Check Position on Entity 1 is default", async () => {
- const position =
- await framework.exampleComponentPosition.account.position.fetch(
- framework.componentPositionEntity1Pda,
- );
- expect(position.x.toNumber()).to.equal(0);
- expect(position.y.toNumber()).to.equal(0);
- expect(position.z.toNumber()).to.equal(0);
- });
- it("Apply Simple Movement System (Up) on Entity 1", async () => {
- const applySystem = await ApplySystem({
- authority: framework.provider.wallet.publicKey,
- systemId: framework.systemSimpleMovement.programId,
- world: framework.worldPda,
- entities: [
- {
- entity: framework.entity1Pda,
- components: [
- { componentId: framework.exampleComponentPosition.programId },
- ],
- },
- ],
- args: {
- direction: Direction.Up,
- },
- });
- await framework.provider.sendAndConfirm(applySystem.transaction, [], {
- skipPreflight: true,
- });
- const position =
- await framework.exampleComponentPosition.account.position.fetch(
- framework.componentPositionEntity1Pda,
- );
- expect(position.x.toNumber()).to.equal(0);
- expect(position.y.toNumber()).to.equal(1);
- expect(position.z.toNumber()).to.equal(0);
- });
- it("Apply Simple Movement System (Right) on Entity 1", async () => {
- const applySystem = await ApplySystem({
- authority: framework.provider.wallet.publicKey,
- systemId: framework.systemSimpleMovement.programId,
- world: framework.worldPda,
- entities: [
- {
- entity: framework.entity1Pda,
- components: [
- { componentId: framework.exampleComponentPosition.programId },
- ],
- },
- ],
- args: {
- direction: Direction.Right,
- },
- });
- await framework.provider.sendAndConfirm(applySystem.transaction);
- const position =
- await framework.exampleComponentPosition.account.position.fetch(
- framework.componentPositionEntity1Pda,
- );
- expect(position.x.toNumber()).to.equal(1);
- expect(position.y.toNumber()).to.equal(1);
- expect(position.z.toNumber()).to.equal(0);
- });
- it("Apply Fly System on Entity 1", async () => {
- const applySystem = await ApplySystem({
- authority: framework.provider.wallet.publicKey,
- systemId: framework.systemFly.programId,
- world: framework.worldPda,
- entities: [
- {
- entity: framework.entity1Pda,
- components: [
- { componentId: framework.exampleComponentPosition.programId },
- ],
- },
- ],
- });
- await framework.provider.sendAndConfirm(applySystem.transaction);
- const position =
- await framework.exampleComponentPosition.account.position.fetch(
- framework.componentPositionEntity1Pda,
- );
- expect(position.x.toNumber()).to.equal(1);
- expect(position.y.toNumber()).to.equal(1);
- expect(position.z.toNumber()).to.equal(1);
- });
- it("Apply System Velocity on Entity 1", async () => {
- const applySystem = await ApplySystem({
- authority: framework.provider.wallet.publicKey,
- systemId: framework.systemApplyVelocity.programId,
- world: framework.worldPda,
- entities: [
- {
- entity: framework.entity1Pda,
- components: [
- {
- componentId: framework.exampleComponentVelocity.programId,
- seed: "component-velocity",
- },
- { componentId: framework.exampleComponentPosition.programId },
- ],
- },
- ],
- });
- await framework.provider.sendAndConfirm(applySystem.transaction);
- const velocity =
- await framework.exampleComponentVelocity.account.velocity.fetch(
- framework.componentVelocityEntity1Pda,
- );
- expect(velocity.x.toNumber()).to.equal(10);
- expect(velocity.y.toNumber()).to.equal(0);
- expect(velocity.z.toNumber()).to.equal(0);
- expect(velocity.lastApplied.toNumber()).to.not.equal(0);
- const position =
- await framework.exampleComponentPosition.account.position.fetch(
- framework.componentPositionEntity1Pda,
- );
- expect(position.x.toNumber()).to.greaterThan(1);
- expect(position.y.toNumber()).to.equal(1);
- expect(position.z.toNumber()).to.equal(1);
- });
- it("Apply System Velocity on Entity 1, with Clock external account", async () => {
- const applySystem = await ApplySystem({
- authority: framework.provider.wallet.publicKey,
- systemId: framework.systemApplyVelocity.programId,
- world: framework.worldPda,
- entities: [
- {
- entity: framework.entity1Pda,
- components: [
- {
- componentId: framework.exampleComponentVelocity.programId,
- seed: "component-velocity",
- },
- { componentId: framework.exampleComponentPosition.programId },
- ],
- },
- ],
- extraAccounts: [
- {
- pubkey: new web3.PublicKey(
- "SysvarC1ock11111111111111111111111111111111",
- ),
- isWritable: false,
- isSigner: false,
- },
- ],
- });
- await framework.provider.sendAndConfirm(applySystem.transaction);
- const position =
- await framework.exampleComponentPosition.account.position.fetch(
- framework.componentPositionEntity1Pda,
- );
- expect(position.x.toNumber()).to.greaterThan(1);
- expect(position.y.toNumber()).to.equal(1);
- expect(position.z.toNumber()).to.equal(300);
- });
- it("Apply Fly System on Entity 4", async () => {
- const applySystem = await ApplySystem({
- authority: framework.provider.wallet.publicKey,
- systemId: framework.systemFly.programId,
- world: framework.worldPda,
- entities: [
- {
- entity: framework.entity4Pda,
- components: [
- { componentId: framework.exampleComponentPosition.programId },
- ],
- },
- ],
- });
- await framework.provider.sendAndConfirm(applySystem.transaction);
- const position =
- await framework.exampleComponentPosition.account.position.fetch(
- framework.componentPositionEntity4Pda,
- );
- expect(position.x.toNumber()).to.equal(0);
- expect(position.y.toNumber()).to.equal(0);
- expect(position.z.toNumber()).to.equal(1);
- });
- });
- }
|