Properly link flecs library
This commit is contained in:
5
engine/libs/flecs/examples/cpp/entities/prefab/.gitignore
vendored
Normal file
5
engine/libs/flecs/examples/cpp/entities/prefab/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.bake_cache
|
||||
.DS_Store
|
||||
.vscode
|
||||
gcov
|
||||
bin
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef PREFAB_H
|
||||
#define PREFAB_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "prefab/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* This file is generated by bake.lang.c for your convenience. Headers of
|
||||
* dependencies will automatically show up in this file. Include bake_config.h
|
||||
* in your main project file. Do not edit! */
|
||||
|
||||
#ifndef PREFAB_BAKE_CONFIG_H
|
||||
#define PREFAB_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
12
engine/libs/flecs/examples/cpp/entities/prefab/project.json
Normal file
12
engine/libs/flecs/examples/cpp/entities/prefab/project.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"id": "prefab",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"author": "Jane Doe",
|
||||
"description": "A simple hello world flecs application",
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++"
|
||||
}
|
||||
}
|
||||
70
engine/libs/flecs/examples/cpp/entities/prefab/src/main.cpp
Normal file
70
engine/libs/flecs/examples/cpp/entities/prefab/src/main.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#include <prefab.h>
|
||||
#include <iostream>
|
||||
|
||||
// Prefabs are entities that can be used as templates for other entities. They
|
||||
// are created with a builtin Prefab tag, which by default excludes them from
|
||||
// queries and systems.
|
||||
|
||||
struct Attack { double value; };
|
||||
struct Defense { double value; };
|
||||
struct FreightCapacity { double value; };
|
||||
struct ImpulseSpeed { double value; };
|
||||
struct HasFTL { };
|
||||
|
||||
struct Position { double x = 0; double y = 0; };
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Create a prefab hierarchy.
|
||||
flecs::entity spaceship = ecs.prefab("Spaceship")
|
||||
// Add components to prefab entity as usual
|
||||
.set<ImpulseSpeed>({50})
|
||||
.set<Defense>({50})
|
||||
|
||||
// By default components in an inheritance hierarchy are shared between
|
||||
// entities. The override function ensures that instances have a private
|
||||
// copy of the component.
|
||||
.override<Position>();
|
||||
|
||||
flecs::entity freighter = ecs.prefab("Freighter")
|
||||
// Short for .add(flecs::IsA, spaceship). This ensures the entity
|
||||
// inherits all components from spaceship.
|
||||
.is_a(spaceship)
|
||||
.add<HasFTL>()
|
||||
.set<FreightCapacity>({100})
|
||||
.set<Defense>({100});
|
||||
|
||||
flecs::entity mammoth_freighter = ecs.prefab("MammothFreighter")
|
||||
.is_a(freighter)
|
||||
.set<FreightCapacity>({500})
|
||||
.set<Defense>({300});
|
||||
|
||||
ecs.prefab("Frigate")
|
||||
.is_a(spaceship)
|
||||
.add<HasFTL>()
|
||||
.set<Attack>({100})
|
||||
.set<Defense>({75})
|
||||
.set<ImpulseSpeed>({125});
|
||||
|
||||
// Create a regular entity from a prefab.
|
||||
// The instance will have a private copy of the Position component, because
|
||||
// of the override in the spaceship entity. All other components are shared.
|
||||
flecs::entity inst = ecs.entity("my_mammoth_freighter")
|
||||
.is_a(mammoth_freighter);
|
||||
|
||||
// Inspect the type of the entity. This outputs:
|
||||
// Position,(Identifier,Name),(IsA,MammothFreighter)
|
||||
std::cout << "Instance type: [" << inst.type().str() << "]\n";
|
||||
|
||||
// Even though the instance doesn't have a private copy of ImpulseSpeed, we
|
||||
// can still get it using the regular API (outputs 50)
|
||||
const ImpulseSpeed *ptr = inst.get<ImpulseSpeed>();
|
||||
std::cout << "Impulse speed: " << ptr->value << "\n";
|
||||
|
||||
// Prefab components can be iterated just like regular components:
|
||||
ecs.each([](flecs::entity e, ImpulseSpeed& is, Position& p) {
|
||||
p.x += is.value;
|
||||
std::cout << e.name() << ": {" << p.x << ", " << p.y << "}\n";
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user