123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- import { expect } from "chai";
- import {
- anchor,
- web3,
- FindComponentPda,
- FindEntityPda,
- SerializeArgs,
- } from "../../clients/bolt-sdk/lib";
- import { Direction } from "../framework";
- export function ecs(framework) {
- describe("ECS", () => {
- it("Add entity 1", async () => {
- const world = await framework.worldProgram.account.world.fetch(
- framework.worldPda,
- );
- framework.entity1Pda = FindEntityPda({
- worldId: world.id,
- entityId: world.entities,
- });
- const instruction = await framework.worldProgram.methods
- .addEntity(null)
- .accounts({
- payer: framework.provider.wallet.publicKey,
- world: framework.worldPda,
- entity: framework.entity1Pda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(transaction);
- });
- it("Add entity 2", async () => {
- const world = await framework.worldProgram.account.world.fetch(
- framework.worldPda,
- );
- framework.entity2Pda = FindEntityPda({
- worldId: world.id,
- entityId: world.entities,
- });
- const instruction = await framework.worldProgram.methods
- .addEntity(null)
- .accounts({
- payer: framework.provider.wallet.publicKey,
- world: framework.worldPda,
- entity: framework.entity2Pda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(transaction);
- });
- it("Add entity 3", async () => {
- const world = await framework.worldProgram.account.world.fetch(
- framework.worldPda,
- );
- const entity3Pda = FindEntityPda({
- worldId: world.id,
- entityId: world.entities,
- });
- const instruction = await framework.worldProgram.methods
- .addEntity(null)
- .accounts({
- payer: framework.provider.wallet.publicKey,
- world: framework.worldPda,
- entity: entity3Pda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(transaction);
- });
- it("Add entity 4 (with seed)", async () => {
- const world = await framework.worldProgram.account.world.fetch(
- framework.worldPda,
- );
- const seed = Buffer.from("custom-seed");
- framework.entity4Pda = FindEntityPda({ worldId: world.id, seed });
- const instruction = await framework.worldProgram.methods
- .addEntity(seed)
- .accounts({
- payer: framework.provider.wallet.publicKey,
- world: framework.worldPda,
- entity: framework.entity4Pda,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(transaction);
- });
- it("Initialize Velocity Component on Entity 1 (with seed)", async () => {
- const componentId = framework.exampleComponentVelocity.programId;
- framework.componentVelocityEntity1Pda = FindComponentPda({
- componentId,
- entity: framework.entity1Pda,
- seed: "component-velocity",
- });
- const instruction = await framework.worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: framework.provider.wallet.publicKey,
- entity: framework.entity1Pda,
- data: framework.componentVelocityEntity1Pda,
- componentProgram: componentId,
- authority: framework.worldProgram.programId,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(transaction);
- });
- it("Initialize Position Component on Entity 1", async () => {
- const componentId = framework.exampleComponentPosition.programId;
- framework.componentPositionEntity1Pda = FindComponentPda({
- componentId,
- entity: framework.entity1Pda,
- });
- const instruction = await framework.worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: framework.provider.wallet.publicKey,
- entity: framework.entity1Pda,
- data: framework.componentPositionEntity1Pda,
- componentProgram: componentId,
- authority: framework.worldProgram.programId,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(transaction);
- });
- it("Initialize Position Component on Entity 2", async () => {
- const componentId = framework.exampleComponentPosition.programId;
- const componentPda = FindComponentPda({
- componentId,
- entity: framework.entity2Pda,
- });
- const instruction = await framework.worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: framework.provider.wallet.publicKey,
- entity: framework.entity2Pda,
- data: componentPda,
- componentProgram: componentId,
- authority: framework.worldProgram.programId,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(transaction);
- });
- it("Initialize Position Component on Entity 4", async () => {
- const componentId = framework.exampleComponentPosition.programId;
- framework.componentPositionEntity4Pda = FindComponentPda({
- componentId,
- entity: framework.entity4Pda,
- });
- const instruction = await framework.worldProgram.methods
- .initializeComponent()
- .accounts({
- payer: framework.provider.wallet.publicKey,
- entity: framework.entity4Pda,
- data: framework.componentPositionEntity4Pda,
- componentProgram: componentId,
- authority: framework.worldProgram.programId,
- })
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(transaction);
- });
- 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 instruction = await framework.worldProgram.methods
- .apply(SerializeArgs({ direction: Direction.Up }))
- .accounts({
- authority: framework.provider.wallet.publicKey,
- boltSystem: framework.systemSimpleMovement.programId,
- world: framework.worldPda,
- sessionToken: null,
- })
- .remainingAccounts([
- {
- pubkey: framework.exampleComponentPosition.programId,
- isSigner: false,
- isWritable: false,
- },
- {
- pubkey: framework.componentPositionEntity1Pda,
- isSigner: false,
- isWritable: true,
- },
- ])
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(transaction);
- 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 instruction = await framework.worldProgram.methods
- .apply(SerializeArgs({ direction: Direction.Right }))
- .accounts({
- authority: framework.provider.wallet.publicKey,
- boltSystem: framework.systemSimpleMovement.programId,
- world: framework.worldPda,
- sessionToken: null,
- })
- .remainingAccounts([
- {
- pubkey: framework.exampleComponentPosition.programId,
- isSigner: false,
- isWritable: false,
- },
- {
- pubkey: framework.componentPositionEntity1Pda,
- isSigner: false,
- isWritable: true,
- },
- ])
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(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 instruction = await framework.worldProgram.methods
- .apply(SerializeArgs())
- .accounts({
- authority: framework.provider.wallet.publicKey,
- boltSystem: framework.systemFly.programId,
- world: framework.worldPda,
- sessionToken: null,
- })
- .remainingAccounts([
- {
- pubkey: framework.exampleComponentPosition.programId,
- isSigner: false,
- isWritable: false,
- },
- {
- pubkey: framework.componentPositionEntity1Pda,
- isSigner: false,
- isWritable: true,
- },
- ])
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(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 instruction = await framework.worldProgram.methods
- .apply(SerializeArgs())
- .accounts({
- authority: framework.provider.wallet.publicKey,
- boltSystem: framework.systemApplyVelocity.programId,
- world: framework.worldPda,
- sessionToken: null,
- })
- .remainingAccounts([
- {
- pubkey: framework.exampleComponentVelocity.programId,
- isSigner: false,
- isWritable: false,
- },
- {
- pubkey: framework.componentVelocityEntity1Pda,
- isSigner: false,
- isWritable: true,
- },
- {
- pubkey: framework.exampleComponentPosition.programId,
- isSigner: false,
- isWritable: false,
- },
- {
- pubkey: framework.componentPositionEntity1Pda,
- isSigner: false,
- isWritable: true,
- },
- ])
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(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 instruction = await framework.worldProgram.methods
- .apply(SerializeArgs())
- .accounts({
- authority: framework.provider.wallet.publicKey,
- boltSystem: framework.systemApplyVelocity.programId,
- world: framework.worldPda,
- sessionToken: null,
- })
- .remainingAccounts([
- {
- pubkey: framework.exampleComponentVelocity.programId,
- isSigner: false,
- isWritable: false,
- },
- {
- pubkey: framework.componentVelocityEntity1Pda,
- isSigner: false,
- isWritable: true,
- },
- {
- pubkey: framework.exampleComponentPosition.programId,
- isSigner: false,
- isWritable: false,
- },
- {
- pubkey: framework.componentPositionEntity1Pda,
- isSigner: false,
- isWritable: true,
- },
- {
- pubkey: framework.worldProgram.programId, // world program ID is the end of components delimiter
- isSigner: false,
- isWritable: false,
- },
- {
- pubkey: new web3.PublicKey(
- "SysvarC1ock11111111111111111111111111111111",
- ),
- isWritable: false,
- isSigner: false,
- },
- ])
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(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 instruction = await framework.worldProgram.methods
- .apply(SerializeArgs())
- .accounts({
- authority: framework.provider.wallet.publicKey,
- boltSystem: framework.systemFly.programId,
- world: framework.worldPda,
- sessionToken: null,
- })
- .remainingAccounts([
- {
- pubkey: framework.exampleComponentPosition.programId,
- isSigner: false,
- isWritable: false,
- },
- {
- pubkey: framework.componentPositionEntity4Pda,
- isSigner: false,
- isWritable: true,
- },
- ])
- .instruction();
- const transaction = new anchor.web3.Transaction().add(instruction);
- await framework.provider.sendAndConfirm(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);
- });
- });
- }
|