swordfish

Crates.ioswordfish
lib.rsswordfish
version0.1.9
created_at2025-07-20 05:01:52.998505+00
updated_at2025-07-26 10:23:56.806723+00
descriptionSwordfish is a Data-Oriented Design (DOD) Logic Layer for large scale social based simulations / games. It's designed for fast query / discrete graphs processing performance and the ease of use.
homepage
repositoryhttps://github.com/dangkaz1109/SwordfishDOD
max_upload_size
id1760805
size20,905
Sollie20 (dangkaz1109)

documentation

README

Swordfish

Swordfish is a Data-Oriented Design (DOD) Logic Layer for large scale social based simulations / games. It's designed for fast query / discrete graphs processing performance and the ease of use.


Features

  • Macro-driven Table Creation: Define your data tables with a simple macro.
  • Efficient Data Storage: Data is stored in parallel vectors, allowing for cache-friendly iteration and operations.
  • Fast Insertions: Reuses IDs from deleted entries to minimize memory reallocations and maintain contiguous data.
  • Easily Querying: Iterate over your table data easily with query! and query_mut! macros, providing direct access to fields.

Installation

Add the following to your Cargo.toml file:

[dependencies]
swordfish = "0.9.0" # Or the latest version

Usage

Use the create_table! macro to define your archetype.

use swordfish::{create_table, query, query_mut};

create_table!(
    /// Represents a game entity with position and health.
    EntityTable, //Define archetype name
    position_x: f32,
    position_y: f32,
    health: i32
);

fn main() {
    // ...
}

This will generate a struct named EntityTable with fields for position_x, position_y, and health, along with management methods.

Creating a Table Instance

use swordfish::{create_table, query, query_mut};

create_table!(
      /// Represents a game entity with position and health.
      EntityTable,
      position_x: f32,
      position_y: f32,
      health: i32
);

fn main() {
    let mut entities = EntityTable::new();
    // ...
}

Inserting Data

Use the insert method to add new entries to your table. It returns the id of the newly inserted entry.

use swordfish::{create_table, query, query_mut};

create_table!(
      /// Represents a game entity with position and health.
      EntityTable,
      position_x: f32,
      position_y: f32,
      health: i32
);
 
fn main() {
    let mut entities = EntityTable::new();

    let entity_id_1 = entities.insert(10.0, 20.0, 100);
    let entity_id_2 = entities.insert(5.0, 15.0, 75);

    println!("Entity 1 ID: {}", entity_id_1); // Output: Entity 1 ID: 0
    println!("Entity 2 ID: {}", entity_id_2); // Output: Entity 2 ID: 1
}

Querying Data

The query! macro allows you to iterate over your table's data, providing immutable references to the fields.

query!(entities, id, pos_x: position_x, pos_y: position_y, hp: health, 
    {
    println!("Entity ID: {}, Pos: ({}, {}), Health: {}", id, pos_x, pos_y, hp);
    }
);

Mutating Data

The query_mut! macro is similar to query!, but it provides mutable references to the fields, allowing you to modify them within the loop.

query_mut!(entities, id, hp: health, 
    {
        *hp -= 10;     
    }
);

Deleting Data

You can mark entries for deletion using delete_by_id. Work safe ONLY in query_mut (or query) statement.

query_mut!(entities, id1, hp: health, {
    entities.delete_by_id(id1);
});
Commit count: 0

cargo fmt