bubel-ecs/demos/source/demos/simple.d
Dawid Masiukiewicz 024356df9b Common update:
-added multiple new function to allocate template and add entity
-updated README.md (complete initial version)
-empty components now don't take memory
-fixedd small bug with TestRunner
-added many new tests (HashMap, Vector, EntityMeta, ...)
-added default hashing function to HashMap
-fixed critical bug with adding entities
-fixed small bug with adding entity with remplacement components
-added asserts into code to better bug detection
-small performance improvement for events
-added ComponentRef structure which contain data pointer and componentID
-remove EntityID from Event structure
-now events are handled before removing entiteis
-fixed GDC compilation
-fixed rendering of rotated sprites
-added weapons as separate entities to space ship and others
-added Tower enemy to SpaceInvaders demo
-added Boss to SpaceInvaders demo (boss has four tower attached to it)
-Boss towers shoot multiple bullets upon death
-fixed critical bug with demos switching
-fixed critical bug related to adding/removing entities form inside onAdd/onRemove entity callback
-added animation support
-added particles sypport and particles for firing and explostions, and more
-multithreaded rendering now has same rendering order as singlethreaded
-application automaticallu detect host CPU threads count
-added upgrades to SPaceInvaders demo
-fixed texture memory freeing
-improved documentation
-improved multithreaded performance
-improve shader code
-fixed registration issue
-some additional performance improvements
-added depth and colors to rendering parameters
-jobs now has names corresponding to their systems
-change execute() -> willExecute()
-added EntityMeta structure to speedup getting fetching components form entity
-improved multithreading rendering
-added possibility tio change number of threads runtime
-added bullets collision detection in SpaceInvaders demo
-some CI changes
-added VBO batch rendering (current default, no render mode switch yet)
-fixed camera positioning calculation
-fixed buffer issue with WebGL
-added viewport scalling (at least 300 pixels height). Pixels are scalled if screen is bigger.
-center demos gameplay area
-added fullpage html template for Emscripten build
-added many new sprites to atlas
-fixed critical bug with CPU usage in multithreaded mode
-snake render tile coresponding to body part
-snake is destroyed after collision and emit some particles
-added some functionality to vectors
-fixed documentation issue in Manager.d
-more minor code changes and cleanup
2020-05-28 16:48:42 +00:00

208 lines
No EOL
5.3 KiB
D

module demos.simple;
import app;
import bindbc.sdl;
import cimgui.cimgui;
import bubel.ecs.attributes;
import bubel.ecs.core;
import bubel.ecs.entity;
import bubel.ecs.manager;
import bubel.ecs.std;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
extern(C):
struct Simple
{
__gshared const (char)* tips = "Use \"space\" to spwan entities.\n\nSystems can be enabled/disabled from \"Simple\" window.";
EntityTemplate* tmpl;
Texture texture;
bool move_system = true;
bool draw_system = true;
}
struct CLocation
{
mixin ECS.Component;
alias location this;
vec2 location;
}
struct CTexture
{
mixin ECS.Component;
Texture tex;
}
struct DrawSystem
{
mixin ECS.System!32;
struct EntitiesData
{
uint length;
//uint thread_id;
uint job_id;
@readonly CTexture[] textures;
@readonly CLocation[] locations;
}
void onUpdate(EntitiesData data)
{
if(launcher.renderer.prepared_items >= launcher.renderer.MaxObjects)return;//simple leave loop if max visible objects count was reached
foreach(i; 0..data.length)
{
launcher.renderer.draw(data.textures[i].tex, data.locations[i].location, vec2(16,16), vec4(0,0,1,1), cast(ushort)(data.locations[i].y), 0x80808080, 0, 0, 0, data.job_id);
// launcher.renderer.draw(data.textures[i].tex, data.locations[i].location, vec2(16,16), vec4(0,0,1,1), 0, 0x80808080, 0, 0, 0, data.job_id);
//draw(renderer, data.textures[i].tex, data.locations[i], vec2(32,32), vec4(0,0,1,1));
}
//if(data.thread_id == 0)launcher.renderer.pushData();
}
}
struct MoveSystem
{
mixin ECS.System!64;
struct EntitiesData
{
uint length;
CLocation[] locations;
}
void onUpdate(EntitiesData data)
{
foreach(i; 0..data.length)
{
data.locations[i].location.y = data.locations[i].location.y + 1;
if(data.locations[i].location.y > 300)data.locations[i].location.y = 0;
}
}
}
__gshared Simple* simple;
void simpleStart()
{
simple = Mallocator.make!Simple;
simple.texture.create();
simple.texture.load("assets/textures/buckler.png");
launcher.manager.beginRegister();
launcher.manager.registerComponent!CLocation;
launcher.manager.registerComponent!CTexture;
launcher.manager.registerSystem!MoveSystem(0);
launcher.manager.registerSystem!DrawSystem(1);
launcher.manager.endRegister();
launcher.gui_manager.addSystem(MoveSystem.system_id,"Move System");
launcher.gui_manager.addSystem(DrawSystem.system_id,"Draw System");
ushort[2] components = [CLocation.component_id, CTexture.component_id];
simple.tmpl = launcher.manager.allocateTemplate(components);
CTexture* tex_comp = simple.tmpl.getComponent!CTexture;
tex_comp.tex = simple.texture;
CLocation* loc_comp = simple.tmpl.getComponent!CLocation;
launcher.gui_manager.addTemplate(simple.tmpl, "Basic");
foreach(i; 0..10)
foreach(j; 0..10)
{
loc_comp.location = vec2(i*16+64,j*16+64);
launcher.manager.addEntity(simple.tmpl);
}
}
void simpleEnd()
{
launcher.manager.getSystem(MoveSystem.system_id).disable();
launcher.manager.getSystem(DrawSystem.system_id).disable();
simple.texture.destroy();
//launcher.manager.freeTemplate(simple.tmpl);
Mallocator.dispose(simple);
}
void simpleTool(vec2 position, Tool tool, int size)
{
switch(tool)
{
case Tool.entity_spawner:
{
EntityTemplate* tmpl = launcher.gui_manager.getSelectedTemplate();
CLocation* location = tmpl.getComponent!CLocation;
if(location)
{
position.x += (randomf - 0.5) * size;
position.y += (randomf - 0.5) * size;
if(position.x > 400)position.x -= 400;
else if(position.x < 0)position.x += 400;
if(position.y > 300)position.y -= 300;
else if(position.y < 0)position.y += 300;
*location = position;
}
launcher.manager.addEntity(tmpl);
}
break;
default:
break;
}
}
void simpleEvent(SDL_Event* event)
{
/*if(event.type == event.button)
{
vec2 position = vec2(event.button.x, event.button.y);
}*/
}
void spawnEntity()
{
CLocation* loc_comp = simple.tmpl.getComponent!CLocation;
loc_comp.location = vec2(randomf() * 400,0);
launcher.manager.addEntity(simple.tmpl);
}
bool simpleLoop()
{
launcher.render_position = (vec2(launcher.window_size.x,launcher.window_size.y)*launcher.scalling - vec2(400,300)) * 0.5;
if(launcher.getKeyState(SDL_SCANCODE_SPACE))
{
foreach(i;0..1)spawnEntity();
}
launcher.manager.begin();
if(launcher.multithreading)
{
launcher.job_updater.begin();
launcher.manager.updateMT();
launcher.job_updater.call();
}
else
{
launcher.manager.update();
}
launcher.manager.end();
return true;
}