Coder Social home page Coder Social logo

Predictive Systems about harmony-ecs HOT 2 CLOSED

3mcd avatar 3mcd commented on June 11, 2024
Predictive Systems

from harmony-ecs.

Comments (2)

3mcd avatar 3mcd commented on June 11, 2024

Leaving some more scratch work here. The general idea is that a system can be invoked within a context, like a predictive context. Most of the Harmony API would be updated to accept an optional context id, changing the final target of that call. Components of a different context could be stored in parallel with the components today. Contexts would just need to track this mapping from the base context schema id to the corresponding schema id within the context.

import * as Rapier from "@dimforge/rapier3d-compat"
import { Command, Context, Effect, Entity, Format, Schema, World } from "harmony-ecs"

const GRAVITY = { x: 0, y: -9.81, z: 0 }

const RigidBody = Schema.define()
const PlayerInput = Schema.defineBinary(Format.uint8)
const Players = [RigidBody] as const

enum PlayerInputIntent {
  Jump = 1 << 0
}

enum MyCommand {
  Spawn = Command.define({ entity: Format.entity }),
  Input = Command.define({ entity: Format.entity, input: Format.uint8 })
}

enum MyEffect {
  Physics = Effect.defineRef(() => new Rapier.World(GRAVITY))
}

enum MyContext {
  Predicted
}

function simulate(ecs: World.World, ctx: Context.Id) {
  const physics = Effect.call(ecs, MyEffect.Physics, ctx)
  const players = Effect.call(ecs, Effect.Query, ctx)(Players)

  for (const spawn of Command.drain(ecs, MyCommand.Spawn, ctx)) {
    const actor = makePlayerActor()
    Entity.set(ecs, input.entity, [RigidBody], [actor], ctx)
    simulation.add(actor)
  }

  for (const input of Command.drain(ecs, MyCommand.Input, ctx)) {
    Entity.set(ecs, input.entity, [PlayerInput], [input], ctx)
  }

  for (const [e, [b, i]] of players) {
    for (let _ = 0; _ < e.length; _++) {
      const body = b[_] as Rapier.RigidBody
      const input = i[_]
      if ((input | PlayerInputIntent.Jump) === PlayerInputIntent.Jump) {
        body.applyForce(/* ... */)
      }
    }
  }
}

const ecs = World.make(1e5)
const pipeline = Pipeline.make(
  System.make(simulate),
  System.make(simulate, MyContext.Predicted)
)

We could then target one system vs the other with a server snapshot etc.

from harmony-ecs.

3mcd avatar 3mcd commented on June 11, 2024

Better yet, systems could be passed a World wrapped with the present context id, i.e. a tuple of (World, Context.Id), removing the need for the API to accept the optional context parameter:

// `ecs` is actually (World.World, Context.Id)
function simulate(ecs: World.World) {
  for (const spawn of Command.drain(ecs, Commands.Spawn)) {
    // ...
  }
}

from harmony-ecs.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.