Properly link flecs library
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
#ifndef BASICS_H
|
||||
#define BASICS_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "basics/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 BASICS_BAKE_CONFIG_H
|
||||
#define BASICS_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
11
engine/libs/flecs/examples/cpp/prefabs/basics/project.json
Normal file
11
engine/libs/flecs/examples/cpp/prefabs/basics/project.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "basics",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
56
engine/libs/flecs/examples/cpp/prefabs/basics/src/main.cpp
Normal file
56
engine/libs/flecs/examples/cpp/prefabs/basics/src/main.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <basics.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.
|
||||
//
|
||||
// Prefab instances are entities that have an IsA relationship to the prefab.
|
||||
// The IsA relationship causes instances to inherit the components from the
|
||||
// prefab. By default all instances for a prefab share its components.
|
||||
//
|
||||
// Inherited components save memory as they only need to be stored once for all
|
||||
// prefab instances. They also speed up the creation of prefabs, as inherited
|
||||
// components don't need to be copied to the instances.
|
||||
//
|
||||
// To get a private copy of a component, an instance can add it which is called
|
||||
// an override. Overrides can be manual (by using add) or automatic (see the
|
||||
// auto_override example).
|
||||
//
|
||||
// If a prefab has children, adding the IsA relationship instantiates the prefab
|
||||
// children for the instance (see hierarchy example).
|
||||
|
||||
struct Defense {
|
||||
double value;
|
||||
};
|
||||
|
||||
int main() {
|
||||
flecs::world ecs;
|
||||
|
||||
// Create a SpaceShip prefab with a Defense component.
|
||||
flecs::entity SpaceShip = ecs.prefab("SpaceShip")
|
||||
.set<Defense>({ 50 });
|
||||
|
||||
// Create a prefab instance
|
||||
flecs::entity inst = ecs.entity("my_spaceship").is_a(SpaceShip);
|
||||
|
||||
// Because of the IsA relationship, the instance now shares the Defense
|
||||
// component with the prefab, and can be retrieved as a regular component:
|
||||
const Defense *d_inst = inst.get<Defense>();
|
||||
std::cout << "defense: " << d_inst->value << "\n";
|
||||
|
||||
// Because the component is shared, changing the value on the prefab will
|
||||
// also change the value for the instance:
|
||||
SpaceShip.set<Defense>({ 100 });
|
||||
std::cout << "defense after set: " << d_inst->value << "\n";
|
||||
|
||||
// Prefab components can be iterated like regular components:
|
||||
ecs.each([](flecs::entity e, Defense& d) {
|
||||
std::cout << e.path() << ": " << d.value << "\n";
|
||||
});
|
||||
|
||||
// Output:
|
||||
// defense: 50
|
||||
// defense after set: 100
|
||||
// ::my_spaceship: 100
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef HIERARCHY_H
|
||||
#define HIERARCHY_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "hierarchy/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 HIERARCHY_BAKE_CONFIG_H
|
||||
#define HIERARCHY_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "hierarchy",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <hierarchy.h>
|
||||
#include <iostream>
|
||||
|
||||
// When a prefab has children, they are instantiated for an instance when the
|
||||
// IsA relationship to the prefab is added.
|
||||
|
||||
int main() {
|
||||
flecs::world ecs;
|
||||
|
||||
// Create a prefab hierarchy.
|
||||
flecs::entity SpaceShip = ecs.prefab("SpaceShip");
|
||||
ecs.prefab("Engine").child_of(SpaceShip);
|
||||
ecs.prefab("Cockpit").child_of(SpaceShip);
|
||||
|
||||
// Instantiate the prefab. This also creates an Engine and Cockpit child
|
||||
// for the instance.
|
||||
flecs::entity inst = ecs.entity("my_spaceship").is_a(SpaceShip);
|
||||
flecs::entity inst_engine = inst.lookup("Engine");
|
||||
flecs::entity inst_cockpit = inst.lookup("Cockpit");
|
||||
|
||||
std::cout << "instance engine: " << inst_engine.path() << "\n";
|
||||
std::cout << "instance cockpit: " << inst_cockpit.path() << "\n";
|
||||
|
||||
// Output:
|
||||
// instance engine: ::my_spaceship::Engine
|
||||
// instance cockpit: ::my_spaceship::Cockpit
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef NESTED_PREFABS_H
|
||||
#define NESTED_PREFABS_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "nested_prefabs/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 NESTED_PREFABS_BAKE_CONFIG_H
|
||||
#define NESTED_PREFABS_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "nested_prefabs",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
#include <nested_prefabs.h>
|
||||
#include <iostream>
|
||||
|
||||
// Nested prefabs make it possible to reuse an existing prefab inside another
|
||||
// prefab. An example of where this could be useful is a car with four wheels:
|
||||
// instead of defining four times what a wheel is a Car prefab can reference an
|
||||
// existing Wheel prefab.
|
||||
//
|
||||
// Nested prefabs can be created by adding a child that is a variant (inherits
|
||||
// from) another prefab. For more information on variants, see the variants
|
||||
// example.
|
||||
//
|
||||
// Instantiated children from a nested prefab still inherit from the original
|
||||
// prefab. The reason for this is that an instantiated child is an exact copy
|
||||
// of the prefab child, and the prefab child only has an IsA relationship to the
|
||||
// nested prefab.
|
||||
//
|
||||
// This example shows how auto overriding (see the auto override example) can be
|
||||
// used to give instantiated children from a nested prefab a private copy of an
|
||||
// inherited component.
|
||||
|
||||
struct TirePressure {
|
||||
double value;
|
||||
};
|
||||
|
||||
int main() {
|
||||
flecs::world ecs;
|
||||
|
||||
// Create a Wheel prefab, make sure each instantiated wheel has a private
|
||||
// copy of the TirePressure component.
|
||||
flecs::entity Wheel = ecs.prefab("Wheel")
|
||||
.set_override<TirePressure>({ 32 });
|
||||
|
||||
// Create a Car prefab with four wheels. Note how we're using the scope
|
||||
// method, which has the same effect as adding the (ChildOf, Car) pair.
|
||||
flecs::entity Car = ecs.prefab("Car").scope([&]{
|
||||
ecs.prefab("FrontLeft").is_a(Wheel);
|
||||
ecs.prefab("FrontRight").is_a(Wheel);
|
||||
ecs.prefab("BackLeft").is_a(Wheel);
|
||||
ecs.prefab("BackRight").is_a(Wheel);
|
||||
});
|
||||
|
||||
// Create a prefab instance.
|
||||
flecs::entity inst = ecs.entity("my_car").is_a(Car);
|
||||
|
||||
// Lookup one of the wheels
|
||||
flecs::entity inst_front_left = inst.lookup("FrontLeft");
|
||||
|
||||
// The type shows that the child has a private copy of the TirePressure
|
||||
// component, and an IsA relationship to the Wheel prefab.
|
||||
std::cout << "type: [" << inst_front_left.type().str() << "]\n";
|
||||
|
||||
// Get the TirePressure component & print its value
|
||||
inst_front_left.get([](const TirePressure p) {
|
||||
std::cout << "pressure: " << p.value << "\n";
|
||||
});
|
||||
|
||||
// Output:
|
||||
// type: [TirePressure, (Identifier,Name), (ChildOf,my_car), (IsA,Wheel)]
|
||||
// pressure: 32
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef OVERRIDE_H
|
||||
#define OVERRIDE_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "override/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 OVERRIDE_BAKE_CONFIG_H
|
||||
#define OVERRIDE_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
11
engine/libs/flecs/examples/cpp/prefabs/override/project.json
Normal file
11
engine/libs/flecs/examples/cpp/prefabs/override/project.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "override",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
73
engine/libs/flecs/examples/cpp/prefabs/override/src/main.cpp
Normal file
73
engine/libs/flecs/examples/cpp/prefabs/override/src/main.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#include <override.h>
|
||||
#include <iostream>
|
||||
|
||||
// Overriding makes it possible for a prefab instance to obtain a private copy
|
||||
// of an inherited component. To override a component the regular add operation
|
||||
// is used. The overridden component will be initialized with the value of the
|
||||
// inherited component.
|
||||
//
|
||||
// In some cases a prefab instance should always have a private copy of an
|
||||
// inherited component. This can be achieved with an auto override which can be
|
||||
// added to a prefab. Components with an auto override are automatically
|
||||
// overridden when the prefab is instantiated.
|
||||
|
||||
struct Attack {
|
||||
double value;
|
||||
};
|
||||
|
||||
struct Defense {
|
||||
double value;
|
||||
};
|
||||
|
||||
struct Damage {
|
||||
double value;
|
||||
};
|
||||
|
||||
int main() {
|
||||
flecs::world ecs;
|
||||
|
||||
// Attack and Damage are properties that can be shared across many
|
||||
// spaceships. This saves memory, and speeds up prefab creation as we don't
|
||||
// have to copy the values of Attack and Defense to private components.
|
||||
flecs::entity SpaceShip = ecs.prefab("SpaceShip")
|
||||
.set<Attack>({ 75 })
|
||||
.set<Defense>({ 100 });
|
||||
|
||||
// Damage is a property that is private to a spaceship, so add an auto
|
||||
// override for it. This ensures that each prefab instance will have a
|
||||
// private copy of the component.
|
||||
SpaceShip.set_override<Damage>({ 0 });
|
||||
|
||||
// Create a prefab instance.
|
||||
flecs::entity inst = ecs.entity("my_spaceship").is_a(SpaceShip);
|
||||
|
||||
// The entity will now have a private copy of the Damage component, but not
|
||||
// of the Attack and Defense components. We can see this when we look at the
|
||||
// type of the instance:
|
||||
std::cout << inst.type().str() << "\n";
|
||||
|
||||
// Even though Attack was not automatically overridden, we can always
|
||||
// override it manually afterwards by adding it:
|
||||
inst.add<Attack>();
|
||||
|
||||
// The Attack component now shows up in the entity type:
|
||||
std::cout << inst.type().str() << "\n";
|
||||
|
||||
// We can get all components on the instance, regardless of whether they
|
||||
// are overridden or not. Note that the overridden components (Attack and
|
||||
// Damage) are initialized with the values from the prefab component:
|
||||
const Attack *a = inst.get<Attack>();
|
||||
const Defense *d = inst.get<Defense>();
|
||||
const Damage *dmg = inst.get<Damage>();
|
||||
|
||||
std::cout << "attack: " << a->value << "\n";
|
||||
std::cout << "defense: " << d->value << "\n";
|
||||
std::cout << "damage: " << dmg->value << "\n";
|
||||
|
||||
// Output:
|
||||
// Damage, (Identifier,Name), (IsA,SpaceShip)
|
||||
// Attack, Damage, (Identifier,Name), (IsA,SpaceShip)
|
||||
// attack: 75
|
||||
// defense: 100
|
||||
// damage: 0
|
||||
}
|
||||
16
engine/libs/flecs/examples/cpp/prefabs/slots/include/slots.h
Normal file
16
engine/libs/flecs/examples/cpp/prefabs/slots/include/slots.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef SLOTS_H
|
||||
#define SLOTS_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "slots/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 SLOTS_BAKE_CONFIG_H
|
||||
#define SLOTS_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
11
engine/libs/flecs/examples/cpp/prefabs/slots/project.json
Normal file
11
engine/libs/flecs/examples/cpp/prefabs/slots/project.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "slots",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
65
engine/libs/flecs/examples/cpp/prefabs/slots/src/main.cpp
Normal file
65
engine/libs/flecs/examples/cpp/prefabs/slots/src/main.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include <slots.h>
|
||||
#include <iostream>
|
||||
|
||||
// Slots can be combined with prefab hierarchies to make it easier to access
|
||||
// the child entities created for an instance.
|
||||
//
|
||||
// To create a slot, the SlotOf relationship is added to the child of a prefab,
|
||||
// with as relationship target the prefab for which to register the slot. When
|
||||
// the prefab is instantiated, each slot will be added as a relationship pair
|
||||
// to the instance that looks like this:
|
||||
// (PrefabChild, InstanceChild)
|
||||
//
|
||||
// For a SpaceShip prefab and an Engine child, that pair would look like this:
|
||||
// (SpaceShip.Engine, Instance.Engine)
|
||||
//
|
||||
// To get the entity for a slot, an application can use the regular functions
|
||||
// to inspect relationships and relationship targets (see code).
|
||||
//
|
||||
// Slots can be added to any level of a prefab hierarchy, as long as it is above
|
||||
// (a parent of) the slot itself. When the prefab tree is instantiated, the
|
||||
// slots are added to the entities that correspond with the prefab children.
|
||||
//
|
||||
// Without slots, an application would have to rely on manually looking up
|
||||
// entities by name to get access to the instantiated children, like what the
|
||||
// hierarchy example does.
|
||||
|
||||
int main() {
|
||||
flecs::world ecs;
|
||||
|
||||
// Create the same prefab hierarchy as from the hierarchy example, but now
|
||||
// with the SlotOf relationship.
|
||||
flecs::entity SpaceShip = ecs.prefab("SpaceShip");
|
||||
flecs::entity Engine = ecs.prefab("Engine")
|
||||
.child_of(SpaceShip)
|
||||
.slot_of(SpaceShip);
|
||||
|
||||
flecs::entity Cockpit = ecs.prefab("Cockpit")
|
||||
.child_of(SpaceShip)
|
||||
.slot_of(SpaceShip);
|
||||
|
||||
// Add an additional child to the Cockpit prefab to demonstrate how
|
||||
// slots can be different from the parent. This slot could have been
|
||||
// added to the Cockpit prefab, but instead we register it on the top
|
||||
// level SpaceShip prefab.
|
||||
flecs::entity PilotSeat = ecs.prefab("PilotSeat")
|
||||
.child_of(Cockpit)
|
||||
.slot_of(SpaceShip);
|
||||
|
||||
// Create a prefab instance.
|
||||
flecs::entity inst = ecs.entity("my_spaceship").is_a(SpaceShip);
|
||||
|
||||
// Get the instantiated entities for the prefab slots
|
||||
flecs::entity inst_engine = inst.target(Engine);
|
||||
flecs::entity inst_cockpit = inst.target(Cockpit);
|
||||
flecs::entity inst_seat = inst.target(PilotSeat);
|
||||
|
||||
std::cout << "instance engine: " << inst_engine.path() << "\n";
|
||||
std::cout << "instance cockpit: " << inst_cockpit.path() << "\n";
|
||||
std::cout << "instance seat: " << inst_seat.path() << "\n";
|
||||
|
||||
// Output:
|
||||
// instance engine: ::my_spaceship::Engine
|
||||
// instance cockpit: ::my_spaceship::Cockpit
|
||||
// instance seat: ::my_spaceship::Cockpit::PilotSeat
|
||||
}
|
||||
5
engine/libs/flecs/examples/cpp/prefabs/typed_prefabs/.gitignore
vendored
Normal file
5
engine/libs/flecs/examples/cpp/prefabs/typed_prefabs/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.bake_cache
|
||||
.DS_Store
|
||||
.vscode
|
||||
gcov
|
||||
bin
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef TYPED_PREFABS_H
|
||||
#define TYPED_PREFABS_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "typed_prefabs/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 TYPED_PREFABS_BAKE_CONFIG_H
|
||||
#define TYPED_PREFABS_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"id": "typed_prefabs",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#include <typed_prefabs.h>
|
||||
#include <iostream>
|
||||
|
||||
// Just like how entities can be associated with a type (like components)
|
||||
// prefabs can be associated with types as well. Types can be more convenient to
|
||||
// work with than entity handles, for a few reasons:
|
||||
//
|
||||
// - There is no need to pass around or store entity handles
|
||||
// - Prefabs automatically assume the name of the type
|
||||
// - Nested types can be used to create prefab hierarchies
|
||||
//
|
||||
// While this functionality is not unique to prefabs (the same mechanism is
|
||||
// used to distribute component handles), prefabs are a good fit, especially
|
||||
// when combined with prefab slots (see slots example and code below).
|
||||
|
||||
// Create types that mirror the prefab hierarchy.
|
||||
struct Turret {
|
||||
struct Base {};
|
||||
struct Head {};
|
||||
};
|
||||
|
||||
struct Railgun {
|
||||
struct Beam {};
|
||||
};
|
||||
|
||||
int main() {
|
||||
flecs::world ecs;
|
||||
|
||||
// Associate types with prefabs
|
||||
ecs.prefab<Turret>();
|
||||
ecs.prefab<Turret::Base>().slot_of<Turret>();
|
||||
ecs.prefab<Turret::Head>().slot_of<Turret>();
|
||||
|
||||
ecs.prefab<Railgun>().is_a<Turret>();
|
||||
ecs.prefab<Railgun::Beam>().slot_of<Railgun>();
|
||||
|
||||
// Create prefab instance.
|
||||
flecs::entity inst = ecs.entity("my_railgun").is_a<Railgun>();
|
||||
|
||||
// Get entities for slots
|
||||
flecs::entity inst_base = inst.target<Turret::Base>();
|
||||
flecs::entity inst_head = inst.target<Turret::Head>();
|
||||
flecs::entity inst_beam = inst.target<Railgun::Beam>();
|
||||
|
||||
std::cout << "instance base: " << inst_base.path() << "\n";
|
||||
std::cout << "instance head: " << inst_head.path() << "\n";
|
||||
std::cout << "instance beam: " << inst_beam.path() << "\n";
|
||||
|
||||
// Output:
|
||||
// instance base: ::my_railgun::Base
|
||||
// instance head: ::my_railgun::Head
|
||||
// instance beam: ::my_railgun::Beam
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef VARIANT_H
|
||||
#define VARIANT_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "variant/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 VARIANT_BAKE_CONFIG_H
|
||||
#define VARIANT_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
11
engine/libs/flecs/examples/cpp/prefabs/variant/project.json
Normal file
11
engine/libs/flecs/examples/cpp/prefabs/variant/project.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "variant",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
83
engine/libs/flecs/examples/cpp/prefabs/variant/src/main.cpp
Normal file
83
engine/libs/flecs/examples/cpp/prefabs/variant/src/main.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <variant.h>
|
||||
#include <iostream>
|
||||
|
||||
/* Prefabs can inherit from each other, which creates prefab variants. With
|
||||
* variants applications can reuse a commmon set of components and specialize it
|
||||
* by adding or overriding components on the variant. */
|
||||
|
||||
struct Attack {
|
||||
double value;
|
||||
};
|
||||
|
||||
struct Defense {
|
||||
double value;
|
||||
};
|
||||
|
||||
struct FreightCapacity {
|
||||
double value;
|
||||
};
|
||||
|
||||
struct ImpulseSpeed {
|
||||
double value;
|
||||
};
|
||||
|
||||
struct Position {
|
||||
double x;
|
||||
double y;
|
||||
};
|
||||
|
||||
int main() {
|
||||
flecs::world ecs;
|
||||
|
||||
// Create a base prefab for SpaceShips.
|
||||
flecs::entity SpaceShip = ecs.prefab("SpaceShip")
|
||||
.set<ImpulseSpeed>({ 50 })
|
||||
.set<Defense>({ 25 });
|
||||
|
||||
// Create a Freighter variant which inherits from SpaceShip
|
||||
flecs::entity Freighter = ecs.prefab("Freighter")
|
||||
.is_a(SpaceShip)
|
||||
.set<FreightCapacity>({ 100 })
|
||||
.set<Defense>({ 50 });
|
||||
|
||||
// Create a MammotFreighter variant which inherits from Freighter
|
||||
flecs::entity MammothFreighter = ecs.prefab("MammothFreighter")
|
||||
.is_a(Freighter)
|
||||
.set<FreightCapacity>({ 500 });
|
||||
|
||||
// Create a Frigate variant which inherits from SpaceShip
|
||||
ecs.prefab("Frigate")
|
||||
.is_a(SpaceShip)
|
||||
.set<Attack>({ 100 })
|
||||
.set<Defense>({ 75 })
|
||||
.set<ImpulseSpeed>({ 125 });
|
||||
|
||||
// Create an instance of the MammothFreighter. This entity will inherit the
|
||||
// ImpulseSpeed from SpaceShip, Defense from Freighter and FreightCapacity
|
||||
// from MammothFreighter.
|
||||
flecs::entity inst = ecs.entity("my_freighter").is_a(MammothFreighter);
|
||||
|
||||
// Add a private Position component.
|
||||
inst.set<Position>({ 10, 20});
|
||||
|
||||
// Instances can override inherited components to give them a private copy
|
||||
// of the component. This freighter got an armor upgrade:
|
||||
inst.set<Defense>({ 100 });
|
||||
|
||||
// Queries can match components from multiple levels of inheritance
|
||||
ecs.each([](flecs::entity e,
|
||||
Position& p, ImpulseSpeed& s, Defense& d, FreightCapacity& c) {
|
||||
std::cout << e.name() << ":\n";
|
||||
std::cout << " - position: " << p.x << ", " << p.y << "\n";
|
||||
std::cout << " - impulse speed: " << s.value << "\n";
|
||||
std::cout << " - defense: " << d.value << "\n";
|
||||
std::cout << " - capacity: " << c.value << "\n";
|
||||
});
|
||||
|
||||
// Output:
|
||||
// my_freighter:
|
||||
// - position: 10, 20
|
||||
// - impulse speed: 50
|
||||
// - defense: 100
|
||||
// - capacity: 500
|
||||
}
|
||||
Reference in New Issue
Block a user