Huge demos update

-moved C stdlib function definitions to ecs_utils.utils
-added function to calculate mix(linear interpolation) and rsqrt(fast inverse sqrt)
-added some math to vec2 (length, normalize...)
-improved renderer with possibility to use multiple materials (one per block, not perfect solution for parallel compute, but works with some requirements)
-added blending support for material (opaque, additive, mixed)
-added Android support
-added gprahical representation for mouse tools (tool_circle.d)
-added initial support for editing template components variables
-better Component and Templates listing
-added possibility to add/removes components using mouse
-move CLocation to game_core.basic and reuse in every test
-moved tools code from demos to App (now code is fully separated from demos!)
-some improvement and fixes in Snake demo, with additional systems to handle adding and removing entities
-added new demo: Particles. By now demo has several particles to spawn and support for attractors and vortexes (calculation is made as every attractor with every entity)
-fixed bug with window hover and tools
-improved tool behaviour
-added new material
-now window is always opened as maximized windowed mode
-some minor fixes and improvements
This commit is contained in:
Mergul 2020-06-06 22:46:29 +02:00
parent 13e6ed8fd5
commit e76c5ccdb2
20 changed files with 1804 additions and 288 deletions

View file

@ -0,0 +1,545 @@
module demos.particles;
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;
import game_core.basic;
import gui.attributes;
extern(C):
private enum float px = 1.0/512.0;
/*#######################################################################################################################
------------------------------------------------ Components ------------------------------------------------------------------
#######################################################################################################################*/
/*struct CLocation
{
mixin ECS.Component;
alias location this;
vec2 location;
}*/
struct CTexCoords
{
mixin ECS.Component;
vec4 value;
}
struct CColor
{
mixin ECS.Component;
alias value this;
@GUIColor uint value = uint.max;
}
struct CVelocity
{
mixin ECS.Component;
alias value this;
vec2 value = vec2(0);
}
struct CForceRange
{
mixin ECS.Component;
vec2 range = vec2(20,200);
}
struct CAttractor
{
mixin ECS.Component;
//alias value this;
float strength = 0.2;
}
struct CVortex
{
mixin ECS.Component;
float strength = 0.6;
}
struct CDamping
{
mixin ECS.Component;
alias power this;
@GUIRange(0,9) ubyte power = 0;
}
struct CGravity
{
mixin ECS.Component;
}
struct CParticleLife
{
mixin ECS.Component;
this(float life_in_secs)
{
life = cast(int)(life_in_secs * 1000_000);
}
alias life this;
int life = 1000000;
}
/*#######################################################################################################################
------------------------------------------------ Systems ------------------------------------------------------------------
#######################################################################################################################*/
struct DrawSystem
{
mixin ECS.System!32;
struct EntitiesData
{
uint length;
//uint thread_id;
uint job_id;
//@readonly CTexCoords[] coords;
@readonly CLocation[] locations;
@optional @readonly CColor[] color;
}
void onUpdate(EntitiesData data)
{
if(launcher.renderer.prepared_items >= launcher.renderer.MaxObjects)return;//simple leave loop if max visible objects count was reached
if(!data.color)
{
foreach(i; 0..data.length)
{
launcher.renderer.draw(particles_demo.texture, data.locations[i], vec2(2,2), vec4(246,64,2,2)*px, 0, 0x80808080, 0, 2, 0, data.job_id);
}
}
else
{
foreach(i; 0..data.length)
{
launcher.renderer.draw(particles_demo.texture, data.locations[i], vec2(2,2), vec4(246,64,2,2)*px, 0, data.color[i].value, 0, 2, 0, data.job_id);
}
}
}
}
struct MoveSystem
{
mixin ECS.System!64;
struct EntitiesData
{
uint length;
CLocation[] locations;
@readonly CVelocity[] velocity;
}
void onUpdate(EntitiesData data)
{
foreach(i; 0..data.length)
{
data.locations[i] += data.velocity[i] * launcher.delta_time;
}
}
}
struct MouseAttractSystem
{
mixin ECS.System!64;
struct EntitiesData
{
uint length;
@readonly CLocation[] locations;
CVelocity[] velocity;
}
vec2 mouse_pos;
bool onBegin()
{
if(!launcher.getKeyState(SDL_SCANCODE_SPACE))return false;
mouse_pos = launcher.mouse.position;
mouse_pos = vec2(mouse_pos.x, mouse_pos.y) * launcher.scalling - launcher.render_position;
return true;
}
void onUpdate(EntitiesData data)
{
float speed = launcher.delta_time * 0.01;
foreach(i;0..data.length)
{
vec2 rel_pos = mouse_pos - data.locations[i];
float len2 = rel_pos.x * rel_pos.x + rel_pos.y * rel_pos.y;
if(len2 < 0.1)len2 = 0.1;
data.velocity[i] = data.velocity[i] + rel_pos / len2 * speed;
}
}
}
struct AttractSystem
{
mixin ECS.System!64;
struct EntitiesData
{
uint length;
@readonly CLocation[] locations;
CVelocity[] velocity;
}
struct Updater
{
AttractSystem.EntitiesData data;
void onUpdate(AttractorIterator.EntitiesData adata)
{
float speed = launcher.delta_time * 0.00004;
if(adata.vortex)
{
foreach(i;0..data.length)
{
foreach(j;0..adata.length)
{
vec2 rel_pos = data.locations[i] - adata.locations[j];
float len2 = rel_pos.length2();
float inv_len = rsqrt(len2);
if(1 < adata.force_range[j].range.y*inv_len)
{
float dist = (adata.force_range[j].range.y - 0.4)*inv_len - 1;
vec2 vec = rel_pos * inv_len;
vec2 cvec = vec2(-vec.y,vec.x);
float sign = -1;
if(1 < adata.force_range[j].range.x*inv_len)sign = 1;
float str = adata.attractor[j].strength * sign;
float vortex_str = adata.vortex[j].strength;
data.velocity[i] = data.velocity[i] + (rel_pos * str + cvec * vortex_str) * speed * dist;
}
}
}
}
else
{
foreach(i;0..data.length)
{
foreach(j;0..adata.length)
{
vec2 rel_pos = data.locations[i] - adata.locations[j];
float len2 = rel_pos.length2();
float inv_len = rsqrt(len2);
if(1 < adata.force_range[j].range.y*inv_len)
{
float dist = (adata.force_range[j].range.y - 0.4)*inv_len - 1;
vec2 vec = rel_pos;
float sign = -1;
if(1 < adata.force_range[j].range.x*inv_len)sign = 1;
float str = adata.attractor[j].strength * speed * dist * sign;
data.velocity[i] = data.velocity[i] + vec * str;
}
}
}
}
}
}
void onUpdate(EntitiesData data)
{
Updater updater;
updater.data = data;
launcher.manager.callEntitiesFunction!AttractorIterator(&updater.onUpdate);
}
}
struct AttractorIterator
{
mixin ECS.System!1;
struct EntitiesData
{
uint length;
@readonly CLocation[] locations;
@readonly CAttractor[] attractor;
@readonly CForceRange[] force_range;
@optional @readonly CVortex[] vortex;
}
bool onBegin()
{
return false;
}
void onUpdate(EntitiesData data)
{
}
}
struct PlayAreaSystem
{
mixin ECS.System!32;
struct EntitiesData
{
uint length;
Entity[] entity;
@readonly CLocation[] locations;
}
void onUpdate(EntitiesData data)
{
foreach(i; 0..data.length)
{
if(data.locations[i].x > 400)launcher.manager.removeEntity(data.entity[i].id);
else if(data.locations[i].x < 0)launcher.manager.removeEntity(data.entity[i].id);
if(data.locations[i].y > 300)launcher.manager.removeEntity(data.entity[i].id);
else if(data.locations[i].y < 0)launcher.manager.removeEntity(data.entity[i].id);
}
}
}
struct DampingSystem
{
mixin ECS.System!32;
struct EntitiesData
{
uint length;
const (Entity)[] entity;
@readonly CDamping[] damping;
CVelocity[] velocity;
}
float[10] damp = 0;
bool onBegin()
{
foreach(i;0..10)
{
damp[i] = powf((0.99 - cast(float)i * 0.01),launcher.delta_time*0.1);
}
return true;
}
void onUpdate(EntitiesData data)
{
foreach(i; 0..data.length)
{
data.velocity[i] = data.velocity[i] * damp[data.damping[i]];
}
}
}
struct ParticleLifeSystem
{
mixin ECS.System!32;
struct EntitiesData
{
uint length;
const (Entity)[] entity;
CParticleLife[] life;
}
int delta_time;
bool onBegin()
{
delta_time = cast(int)(launcher.delta_time * 1000);
return true;
}
void onUpdate(EntitiesData data)
{
foreach(i; 0..data.length)
{
data.life[i] -= delta_time;
if(data.life[i] < 0)launcher.manager.removeEntity(data.entity[i].id);
}
}
}
struct GravitySystem
{
mixin ECS.System!32;
struct EntitiesData
{
uint length;
const (Entity)[] entity;
@readonly CGravity[] gravity;
CVelocity[] velocity;
}
void onUpdate(EntitiesData data)
{
float delta_time = launcher.delta_time * 0.00_092;
foreach(i; 0..data.length)
{
data.velocity[i].y -= delta_time;
}
}
}
/*#######################################################################################################################
------------------------------------------------ Functions ------------------------------------------------------------------
#######################################################################################################################*/
struct ParticlesDemo
{
__gshared const (char)* tips = "Use \"space\" to spwan entities.\n\nSystems can be enabled/disabled from \"Simple\" window.";
Texture texture;
}
__gshared ParticlesDemo* particles_demo;
void particlesStart()
{
particles_demo = Mallocator.make!ParticlesDemo;
particles_demo.texture.create();
particles_demo.texture.load("assets/textures/atlas.png");
launcher.manager.beginRegister();
launcher.manager.registerComponent!CLocation;
launcher.manager.registerComponent!CTexCoords;
launcher.manager.registerComponent!CColor;
launcher.manager.registerComponent!CVelocity;
launcher.manager.registerComponent!CAttractor;
launcher.manager.registerComponent!CDamping;
launcher.manager.registerComponent!CGravity;
launcher.manager.registerComponent!CVortex;
launcher.manager.registerComponent!CParticleLife;
launcher.manager.registerComponent!CForceRange;
launcher.manager.registerSystem!MoveSystem(0);
launcher.manager.registerSystem!DrawSystem(100);
launcher.manager.registerSystem!PlayAreaSystem(102);
launcher.manager.registerSystem!AttractSystem(-1);
launcher.manager.registerSystem!MouseAttractSystem(1);
launcher.manager.registerSystem!DampingSystem(101);
launcher.manager.registerSystem!ParticleLifeSystem(-10);
launcher.manager.registerSystem!GravitySystem(-2);
launcher.manager.registerSystem!AttractorIterator(-1);
launcher.manager.endRegister();
launcher.gui_manager.addSystem(MoveSystem.system_id,"Move System");
launcher.gui_manager.addSystem(DrawSystem.system_id,"Draw System");
launcher.gui_manager.addSystem(PlayAreaSystem.system_id,"Play Area System");
launcher.gui_manager.addSystem(AttractSystem.system_id,"Attract System");
launcher.gui_manager.addSystem(MouseAttractSystem.system_id,"Mouse Attract System");
launcher.gui_manager.addSystem(DampingSystem.system_id,"Damping System");
launcher.gui_manager.addSystem(ParticleLifeSystem.system_id,"Particle Life System");
launcher.gui_manager.addComponent(CColor(),"Color (white)");
launcher.gui_manager.addComponent(CColor(0xFF101540),"Color (red)");
launcher.gui_manager.addComponent(CColor(0xFF251010),"Color (blue)");
launcher.gui_manager.addComponent(CColor(0xFF102010),"Color (green)");
launcher.gui_manager.addComponent(CAttractor(0.1),"Attractor (str 0.1)");
launcher.gui_manager.addComponent(CForceRange(vec2(5,40)),"ForceRange (5,40)");
launcher.gui_manager.addComponent(CVelocity(),"Velocity");
launcher.gui_manager.addComponent(CDamping(),"Damping");
launcher.gui_manager.addComponent(CVortex(),"Vortex");
launcher.gui_manager.addComponent(CParticleLife(),"Particle Life");
launcher.gui_manager.addComponent(CGravity(),"Gravity");
EntityTemplate* tmpl;
EntityTemplate* base_tmpl = launcher.manager.allocateTemplate([CLocation.component_id, CColor.component_id, CVelocity.component_id, CDamping.component_id].staticArray);
launcher.gui_manager.addTemplate(base_tmpl,"Particle");
tmpl = launcher.manager.allocateTemplate(base_tmpl);
tmpl.getComponent!CColor().value = 0xFF251010;
launcher.gui_manager.addTemplate(tmpl,"Particle (blue)");
tmpl = launcher.manager.allocateTemplate(base_tmpl);
tmpl.getComponent!CColor().value = 0xFF102010;
launcher.gui_manager.addTemplate(tmpl,"Particle (green)");
tmpl = launcher.manager.allocateTemplate(base_tmpl);
tmpl.getComponent!CColor().value = 0xFF101540;
launcher.gui_manager.addTemplate(tmpl,"Particle (red)");
// tmpl = launcher.manager.allocateTemplate(tmpl, [CDamping.component_id].staticArray);
// launcher.gui_manager.addTemplate(tmpl,"Particle (damping)");
tmpl = launcher.manager.allocateTemplate(tmpl);
tmpl.getComponent!CDamping().power = 4;
launcher.gui_manager.addTemplate(tmpl,"Particle (damping!)");
tmpl = launcher.manager.allocateTemplate([CAttractor.component_id, CLocation.component_id, CForceRange.component_id].staticArray);
launcher.gui_manager.addTemplate(tmpl,"Attractor");
tmpl = launcher.manager.allocateTemplate(tmpl, [CVortex.component_id].staticArray);
launcher.gui_manager.addTemplate(tmpl,"Vortex");
tmpl = launcher.manager.allocateTemplate(tmpl);
tmpl.getComponent!CVortex().strength = -0.6;
launcher.gui_manager.addTemplate(tmpl,"Vortex (reversed)");
}
void particlesEnd()
{
particles_demo.texture.destroy();
//launcher.manager.freeTemplate(simple.tmpl);
Mallocator.dispose(particles_demo);
}
void particlesEvent(SDL_Event* event)
{
}
bool particlesLoop()
{
launcher.render_position = (vec2(launcher.window_size.x,launcher.window_size.y)*launcher.scalling - vec2(400,300)) * 0.5;
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;
}

View file

@ -4,46 +4,38 @@ 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 cimgui.cimgui;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
import game_core.basic;
extern(C):
struct Simple
{
__gshared const (char)* tips = "Use \"space\" to spwan entities.\n\nSystems can be enabled/disabled from \"Simple\" window.";
/*#######################################################################################################################
------------------------------------------------ Components ------------------------------------------------------------------
#######################################################################################################################*/
EntityTemplate* tmpl;
Texture texture;
bool move_system = true;
bool draw_system = true;
}
struct CLocation
/*struct CLocation
{
mixin ECS.Component;
alias location this;
vec2 location;
}
}*/
struct CTexture
{
mixin ECS.Component;
Texture tex;
}
/*#######################################################################################################################
------------------------------------------------ Systems ------------------------------------------------------------------
#######################################################################################################################*/
struct DrawSystem
{
@ -54,7 +46,6 @@ struct DrawSystem
uint length;
//uint thread_id;
uint job_id;
@readonly CTexture[] textures;
@readonly CLocation[] locations;
}
@ -63,7 +54,7 @@ struct DrawSystem
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(simple.texture, data.locations[i], 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));
}
@ -85,12 +76,24 @@ struct MoveSystem
{
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;
data.locations[i].y = data.locations[i].y + 1;
if(data.locations[i].y > 300)data.locations[i].y = 0;
}
}
}
/*#######################################################################################################################
------------------------------------------------ Functions ------------------------------------------------------------------
#######################################################################################################################*/
struct Simple
{
__gshared const (char)* tips = "Use \"space\" to spwan entities.\n\nSystems can be enabled/disabled from \"Simple\" window.";
EntityTemplate* tmpl;
Texture texture;
}
__gshared Simple* simple;
void simpleStart()
@ -103,7 +106,6 @@ void simpleStart()
launcher.manager.beginRegister();
launcher.manager.registerComponent!CLocation;
launcher.manager.registerComponent!CTexture;
launcher.manager.registerSystem!MoveSystem(0);
launcher.manager.registerSystem!DrawSystem(1);
@ -113,19 +115,17 @@ void simpleStart()
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];
ushort[1] components = [CLocation.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;
//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);
//loc_comp.value = vec2(i*16+64,j*16+64);
launcher.manager.addEntity(simple.tmpl,[CLocation(vec2(i*16+64,j*16+64)).ref_].staticArray);
}
}
@ -140,46 +140,15 @@ void simpleEnd()
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);
//CLocation* loc_comp = simple.tmpl.getComponent!CLocation;
//loc_comp.value = vec2(randomf() * 400,0);
launcher.manager.addEntity(simple.tmpl,[CLocation(vec2(randomf() * 400,0)).ref_].staticArray);
}
bool simpleLoop()
@ -188,7 +157,7 @@ bool simpleLoop()
if(launcher.getKeyState(SDL_SCANCODE_SPACE))
{
foreach(i;0..1)spawnEntity();
foreach(i;0..20)spawnEntity();
}
launcher.manager.begin();

View file

@ -4,8 +4,6 @@ import app;
import bindbc.sdl;
import cimgui.cimgui;
import bubel.ecs.attributes;
import bubel.ecs.core;
import bubel.ecs.entity;
@ -13,10 +11,14 @@ import bubel.ecs.manager;
import bubel.ecs.std;
import bubel.ecs.vector;
import cimgui.cimgui;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
import game_core.basic;
//import std.array : staticArray;
enum float px = 1.0/512.0;
@ -31,22 +33,6 @@ struct MapElement
apple = 1,
wall = 2,
snake = 3,
/* snake_head_up = 5,
snake_head_down = 6,
snake_head_left = 7,
snake_head_right = 8,
snake_tail_up = 9,
snake_tail_down = 10,
snake_tail_left = 11,
snake_tail_right = 12,
snake_turn_ld = 13,
snake_turn_lu = 14,
snake_turn_rd = 15,
snake_turn_ru = 16,
snake_vertical = 17,
snake_horizontal = 18*/
}
Type type;
EntityID id;
@ -129,10 +115,7 @@ struct Snake
}
if(base_pos.x == random_pos.x && base_pos.y == random_pos.y)return;
}
//CILocation* location = apple_tmpl.getComponent!CILocation;
//*location = random_pos;
//Entity* apple =
launcher.manager.addEntity(apple_tmpl,[CILocation(random_pos).ref_].staticArray);
launcher.manager.addEntity(apple_tmpl,[CLocation(cast(vec2)(random_pos)*16).ref_].staticArray);
}
}
@ -158,14 +141,14 @@ struct CILocation
ivec2 location;
}
struct CLocation
{
mixin ECS.Component;
// struct CLocation
// {
// mixin ECS.Component;
alias location this;
// alias location this;
vec2 location = vec2(0,0);
}
// vec2 location = vec2(0,0);
// }
struct CSnake
{
@ -264,8 +247,8 @@ struct AppleSystem
struct EntitiesData
{
uint length;
@readonly Entity[] entities;
@readonly CApple[] movement;
@readonly Entity[] entity;
@readonly CApple[] apple;
@readonly CILocation[] location;
}
@ -273,7 +256,17 @@ struct AppleSystem
{
foreach(i;0..data.length)
{
snake.element(MapElement(MapElement.Type.apple,data.entities[i].id),data.location[i]);
if(snake.element(data.location[i]).id == EntityID())snake.element(MapElement(MapElement.Type.apple,data.entity[i].id),data.location[i]);
else launcher.manager.removeEntity(data.entity[i].id);
}
}
void onRemoveEntity(EntitiesData data)
{
foreach(i;0..data.length)
{
if(snake.element(data.location[i].location).id == data.entity[i].id)
snake.element(MapElement(MapElement.Type.empty, EntityID()),data.location[i].location);
}
}
}
@ -315,7 +308,7 @@ struct ParticleMovementSystem
{
foreach(i;0..data.length)
{
data.location[i].location -= data.movement[i].velocity;
data.location[i] -= data.movement[i].velocity;
}
}
}
@ -357,7 +350,7 @@ struct AnimationRenderSystem
{
foreach(i;0..data.length)
{
launcher.renderer.draw(snake.texture, cast(vec2)cast(ivec2)data.location[i].location, vec2(16,16), data.animation[i].frames[cast(int)(data.animation[i].time)], -1, 0x80808080);
launcher.renderer.draw(snake.texture, cast(vec2)cast(ivec2)data.location[i], vec2(16,16), data.animation[i].frames[cast(int)(data.animation[i].time)], -1, 0x80808080);
}
}
}
@ -477,7 +470,12 @@ struct MoveSystem
break;
case MapElement.Type.apple:
launcher.manager.removeEntity(snake.element(data.location[i].location).id);
if(data.snakes[i].parts.length < 100)data.snakes[i].parts.add(new_location);
if(data.snakes[i].parts.length >= 99)
{
snake.addApple();
goto case(MapElement.Type.empty);
}
data.snakes[i].parts.add(new_location);
if(data.snakes[i].parts.length > 1)
{
@ -506,7 +504,40 @@ struct MoveSystem
}
}
}
}
}
struct SnakeSystem
{
mixin ECS.System!1;
struct EntitiesData
{
uint length;
Entity[] entity;
@readonly CSnake[] snake;
@readonly CILocation[] location;
}
void onAddSystem(EntitiesData data)
{
foreach(i;0..data.length)
{
if(snake.element(data.location[i]).id == EntityID())snake.element(MapElement(MapElement.Type.snake,data.entity[i].id),data.location[i]);
else launcher.manager.removeEntity(data.entity[i].id);
}
}
void onRemoveEntity(EntitiesData data)
{
foreach(i;0..data.length)
{
if(snake.element(data.location[i].location).id == data.entity[i].id)
snake.element(MapElement(MapElement.Type.empty, EntityID()),data.location[i].location);
foreach(part; data.snake[i].parts.array)
if(snake.element(part).id == data.entity[i].id)
snake.element(MapElement(MapElement.Type.empty, EntityID()),part);
}
}
}
@ -750,6 +781,35 @@ struct CleanSystem
}
}
struct CopyLocationSystem
{
mixin ECS.System!32;
struct EntitiesData
{
uint length;
const (Entity)[] entity;
CLocation[] location;
@readonly CILocation[] ilocation;
}
void onAddEntity(EntitiesData data)
{
foreach(i;0..data.length)
{
data.ilocation[i] = cast(ivec2)(data.location[i] / 16);
}
}
void onUpdate(EntitiesData data)
{
foreach(i;0..data.length)
{
data.location[i] = cast(vec2)(data.ilocation[i] * 16);
}
}
}
__gshared Snake* snake;
void snakeStart()
@ -776,7 +836,6 @@ void snakeStart()
launcher.manager.registerSystem!MoveSystem(0,"fixed");
launcher.manager.registerSystem!InputSystem(-100);
launcher.manager.registerSystem!FixSnakeDirectionSystem(-1,"fixed");
launcher.manager.registerSystem!AppleSystem(-1,"fixed");
launcher.manager.registerSystem!AnimationRenderSystem(100);
launcher.manager.registerSystem!AnimationSystem(-1);
launcher.manager.registerSystem!ParticleSystem(-1);
@ -784,8 +843,20 @@ void snakeStart()
launcher.manager.registerSystem!DrawAppleSystem(99);
launcher.manager.registerSystem!DrawSnakeSystem(101);
launcher.manager.registerSystem!CopyLocationSystem(100);
//launcher.manager.registerSystem!AppleRemoveSystem(100);
launcher.manager.registerSystem!AppleSystem(101);
launcher.manager.registerSystem!SnakeSystem(101);
launcher.manager.endRegister();
launcher.gui_manager.addComponent(CApple(),"Apple");
launcher.gui_manager.addComponent(CSnake(),"Snake");
launcher.gui_manager.addComponent(CParticle(1000),"Particle (1s)");
launcher.gui_manager.addComponent(CParticleVector(vec2(0,1)),"Particle Vector (UP)");
launcher.gui_manager.addComponent(CInput(),"Input");
launcher.gui_manager.addComponent(CMovement(CMovement.Direction.up),"Movement (UP)");
launcher.gui_manager.addSystem(MoveSystem.system_id,"Move System");
launcher.gui_manager.addSystem(InputSystem.system_id,"Input System");
launcher.gui_manager.addSystem(FixSnakeDirectionSystem.system_id,"Fix Direction System");
@ -797,15 +868,13 @@ void snakeStart()
snake.snake_destroy_particle_frames = Mallocator.makeArray([vec4(64,144,16,16)*px,vec4(80,144,16,16)*px,vec4(96,144,16,16)*px,vec4(112,144,16,16)*px].staticArray);
{
ushort[4] components = [CILocation.component_id, CSnake.component_id, CMovement.component_id, CInput.component_id];
ushort[5] components = [CILocation.component_id, CSnake.component_id, CMovement.component_id, CInput.component_id, CLocation.component_id];
snake.snake_tmpl = launcher.manager.allocateTemplate(components);
//CILocation* loc_comp = snake.snake_tmpl.getComponent!CILocation;
//*loc_comp = ivec2(2,2);
launcher.manager.addEntity(snake.snake_tmpl,[CILocation(ivec2(2,2)).ref_].staticArray);
}
{
snake.snake_destroy_particle = launcher.manager.allocateTemplate([CLocation.component_id, CParticle.component_id, CParticleVector.component_id, CAnimation.component_id].staticArray);
snake.snake_destroy_particle = launcher.manager.allocateTemplate([CLocation.component_id, CParticle.component_id, CParticleVector.component_id, CAnimation.component_id, CLocation.component_id].staticArray);
CAnimation* canim = snake.snake_destroy_particle.getComponent!CAnimation;
canim.frames = snake.snake_destroy_particle_frames;
CParticle* particle = snake.snake_destroy_particle.getComponent!CParticle;
@ -813,7 +882,7 @@ void snakeStart()
}
{
ushort[2] components = [CILocation.component_id, CApple.component_id];
ushort[3] components = [CILocation.component_id, CApple.component_id, CLocation.component_id];
snake.apple_tmpl = launcher.manager.allocateTemplate(components);
snake.addApple();
}
@ -824,13 +893,6 @@ void snakeStart()
MoveSystem* move_system = launcher.manager.getSystem!MoveSystem();
move_system.setTemplates();
/*foreach(i; 0..10)
foreach(j; 0..10)
{
loc_compation = vec2(i*32+64,j*32+64);
launcher.manager.addEntity(simple.tmpl);
}*/
}
void snakeEnd()
@ -839,39 +901,6 @@ void snakeEnd()
Mallocator.dispose(snake);
}
void snakeTool(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;
*location = position;
}
CILocation* ilocation = tmpl.getComponent!CILocation;
if(ilocation)
{
position.x += (randomf() - 0.5) * size;
position.y += (randomf() - 0.5) * size;
ivec2 ipos;
ipos.x = cast(int)(position.x / 16);
ipos.y = cast(int)(position.y / 16);
*ilocation = ipos;
if(snake.element(ipos).type != MapElement.Type.empty)return;
}
launcher.manager.addEntity(tmpl);
}
break;
default:
break;
}
}
void snakeEvent(SDL_Event* event)
{
@ -881,16 +910,16 @@ bool snakeLoop()
{
launcher.render_position = (vec2(launcher.window_size.x,launcher.window_size.y)*launcher.scalling - vec2(288,288)) * 0.5;
/*if(launcher.show_demo_wnd)
{
igSetNextWindowPos(ImVec2(800 - 260, 30), ImGuiCond_Once, ImVec2(0,0));
igSetNextWindowSize(ImVec2(250, 0), ImGuiCond_Once);
if(igBegin("Snake",&launcher.show_demo_wnd,0))
{
// if(launcher.show_demo_wnd)
// {
// igSetNextWindowPos(ImVec2(800 - 260, 30), ImGuiCond_Once, ImVec2(0,0));
// igSetNextWindowSize(ImVec2(250, 0), ImGuiCond_Once);
// if(igBegin("Snake",&launcher.show_demo_wnd,0))
// {
}
igEnd();
}*/
// }
// igEnd();
// }
launcher.manager.begin();
@ -912,7 +941,5 @@ bool snakeLoop()
launcher.manager.end();
//snake.drawMap();
return true;
}

View file

@ -4,25 +4,26 @@ 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 cimgui.cimgui;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
import game_core.basic;
//import std.math : PI;
enum PI = 3.141592653589793238462643383279502884197169399375105820;
//import std.array : staticArray;
enum float px = 1.0/512.0;
private enum float px = 1.0/512.0;
extern(C):
@ -114,14 +115,14 @@ enum Direction : byte
------------------------------------------------ Components ------------------------------------------------------------------
#######################################################################################################################*/
struct CLocation
/*struct CLocation
{
mixin ECS.Component;
alias value this;
vec2 value = vec2(0);
}
}*/
struct CScale
{
@ -273,7 +274,7 @@ struct CTargetParent
mixin ECS.Component;
EntityID parent;
vec2 rel_pos;
vec2 rel_pos = vec2(0,0);
}
@ -699,7 +700,7 @@ struct ShipWeaponSystem
Entity[] entity;
CInit[] init;
//CShip[] ship;
//CChildren[] children;
CChildren[] children;
}
struct Ship
@ -1062,7 +1063,7 @@ struct CollisionSystem
}
}
struct LaserShootingSystem
struct ShootingSystem
{
mixin ECS.System!32;
@ -1187,7 +1188,7 @@ struct LaserShootingSystem
else fire_velocity.value = vec2(0,0);
fire_location.value = data.location[i];
if(data.shoot_direction[i].direction == Direction.down)
if(data.shoot_direction && data.shoot_direction[i].direction == Direction.down)
{
fire_rotation.value = PI;
//fire_location.value.y -= 16;
@ -1270,7 +1271,7 @@ struct DampingSystem
}
}
struct LaserCollisionSystem
struct BulletsCollisionSystem
{
mixin ECS.System!32;
@ -1423,9 +1424,12 @@ struct UpgradeSystem
if(ship)
{
CChildren* children = entity.getComponent!CChildren;
foreach(child;children.childern)
if(children)
{
launcher.manager.sendEvent(child,EUpgrade());
foreach(child;children.childern)
{
launcher.manager.sendEvent(child,EUpgrade());
}
}
}
}
@ -1742,7 +1746,7 @@ struct ShootWaveSystem
CLocation* location = entity.getComponent!CLocation;
CGuild* guild = entity.getComponent!CGuild;
//LaserShootingSystem.bullet_tmpl
//ShootingSystem.bullet_tmpl
EntityTemplate* tmpl = space_invaders.bullet_tmpl[wave.bullet_type];
foreach(dir;dirs)
{
@ -2180,12 +2184,6 @@ struct CShipIterator
//void handleEvent(Entity* entity, )
}//*/
extern(C) float sqrtf(float x) @nogc nothrow @system;
extern(C) float acosf(float x) @nogc nothrow @system;
extern(C) float sinf(float x) @nogc nothrow @system;
extern(C) float cosf(float x) @nogc nothrow @system;
extern(C) float powf(float x, float y) @nogc nothrow @system;
/**
*System is responsible for movement of objects with CInput component.
*In this example every entity has same speed when using movement system.
@ -2348,9 +2346,9 @@ void spaceInvadersStart()
launcher.manager.registerSystem!InputMovementSystem(-100);
launcher.manager.registerSystem!MovementSystem(-99);
launcher.manager.registerSystem!ClampPositionSystem(-90);
launcher.manager.registerSystem!LaserShootingSystem(0);
launcher.manager.registerSystem!ShootingSystem(0);
launcher.manager.registerSystem!ChangeDirectionSystem(0);
launcher.manager.registerSystem!LaserCollisionSystem(-70);
launcher.manager.registerSystem!BulletsCollisionSystem(-70);
launcher.manager.registerSystem!ShootGridManager(-80);
launcher.manager.registerSystem!ShootGridCleaner(-101);
launcher.manager.registerSystem!HitPointsSystem(0);
@ -2374,13 +2372,49 @@ void spaceInvadersStart()
launcher.manager.endRegister();
launcher.gui_manager.addComponent(CInput(),"Input");
launcher.gui_manager.addComponent(CShip(),"Ship");
launcher.gui_manager.addComponent(CEnemy(),"Enemy");
launcher.gui_manager.addComponent(CAutoShoot(),"Auto Shoot");
launcher.gui_manager.addComponent(CWeapon(0, CWeapon.Type.laser),"Weapon (laser)");
launcher.gui_manager.addComponent(CVelocity(vec2(0,0)),"Velocity (0,0)");
launcher.gui_manager.addComponent(CBullet(),"Bullet (dmg1)");
launcher.gui_manager.addComponent(CSideMove(),"Side Move");
launcher.gui_manager.addComponent(CSideMove(0),"Side Move (g1)");
launcher.gui_manager.addComponent(CSideMove(1),"Side Move (g2)");
launcher.gui_manager.addComponent(CShootGrid(),"Shoot Grid");
launcher.gui_manager.addComponent(CGuild(),"Guild (Player)");
launcher.gui_manager.addComponent(CGuild(1),"Guild (Enemy)");
launcher.gui_manager.addComponent(CHitPoints(10),"Hit Points (10)");
launcher.gui_manager.addComponent(CHitMark(),"Hit Mark");
launcher.gui_manager.addComponent(CUpgrade(CUpgrade.Upgrade.laser),"Upgrade (laser)");
launcher.gui_manager.addComponent(CParticle(1000),"Particle (1s)");
//launcher.gui_manager.addComponent(CMaxHitPoints(),"Max Hit Points");
launcher.gui_manager.addComponent(CDamping(0),"Damping (0)");
launcher.gui_manager.addComponent(CDamping(4),"Damping (4)");
launcher.gui_manager.addComponent(CDamping(8),"Damping (8)");
launcher.gui_manager.addComponent(CTargetParent(),"Target Parent");
launcher.gui_manager.addComponent(CTargetPlayerShip(),"Target Player Ship");
launcher.gui_manager.addComponent(CTarget(),"Target");
launcher.gui_manager.addComponent(CChildren(),"Children");
launcher.gui_manager.addComponent(CWeaponLocation(vec2(0,16)),"Weapon Location (0,16)");
launcher.gui_manager.addComponent(CInit(CInit.Type.space_ship),"Init (Ship)");
launcher.gui_manager.addComponent(CInit(CInit.Type.boss),"Init (Boss)");
launcher.gui_manager.addComponent(CInit(CInit.Type.tower),"Init (Tower)");
launcher.gui_manager.addComponent(CBoss(),"Boss");
launcher.gui_manager.addComponent(CColliderScale(),"Collider Scale");
launcher.gui_manager.addComponent(CParticleEmitter(),"Particle Emitter");
launcher.gui_manager.addComponent(CParticleEmitterTime(),"Particle Emitter Time");
//launcher.gui_manager.addComponent(CSpawnUponDeath(),"Spawn Upon Death");
launcher.gui_manager.addComponent(CShootWaveUponDeath(CWeapon.Type.canon),"Wave Upon Death");
launcher.gui_manager.addSystem(DrawSystem.system_id,"Draw System");
launcher.gui_manager.addSystem(InputMovementSystem.system_id,"Input Movement");
launcher.gui_manager.addSystem(LaserShootingSystem.system_id,"Laser Shooting");
launcher.gui_manager.addSystem(ShootingSystem.system_id,"Shooting System");
launcher.gui_manager.addSystem(MovementSystem.system_id,"Movement System");
launcher.gui_manager.addSystem(ClampPositionSystem.system_id,"Clamp Position System");
launcher.gui_manager.addSystem(ChangeDirectionSystem.system_id,"Change Direction System");
launcher.gui_manager.addSystem(LaserCollisionSystem.system_id,"Laser Collision System");
launcher.gui_manager.addSystem(BulletsCollisionSystem.system_id,"Bullets Collision System");
launcher.gui_manager.addSystem(ShootGridManager.system_id,"Shoot Grid Manager");
launcher.gui_manager.addSystem(ShootGridCleaner.system_id,"Shoot Grid Cleaner");
launcher.gui_manager.addSystem(HitPointsSystem.system_id,"Hit Points System");
@ -2560,7 +2594,7 @@ void spaceInvadersEnd()
{
/*launcher.manager.getSystem(DrawSystem.system_id).disable();
launcher.manager.getSystem(InputMovementSystem.system_id).disable();
launcher.manager.getSystem(LaserShootingSystem.system_id).disable();
launcher.manager.getSystem(ShootingSystem.system_id).disable();
launcher.manager.getSystem(MovementSystem.system_id).disable();
launcher.manager.getSystem(ClampPositionSystem.system_id).disable();
launcher.manager.getSystem(ShootGridCleaner.system_id).disable();*/
@ -2570,35 +2604,6 @@ void spaceInvadersEnd()
space_invaders = null;
}
void spaceInvadersTool(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.y < 16)position.y = 16;
else if(position.y > 299)position.y = 299;
*location = position;
}
CWeapon* laser_weapon = tmpl.getComponent!CWeapon;
if(laser_weapon)
{
laser_weapon.shoot_time = randomf * CWeapon.levels[laser_weapon.level - 1].reload_time;
}
launcher.manager.addEntity(tmpl);
}
break;
default:
break;
}
}
void spaceInvadersEvent(SDL_Event* event)
{