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
This commit is contained in:
Dawid Masiukiewicz 2020-05-28 16:48:42 +00:00
parent 2ddb97e9ce
commit 024356df9b
62 changed files with 5918 additions and 1673 deletions

View file

@ -6,9 +6,9 @@ import cimgui.cimgui;
import game_core.job_updater;
import ecs.manager;
import ecs.core;
import ecs.std;
import bubel.ecs.manager;
import bubel.ecs.core;
import bubel.ecs.std;
import ecs_utils.gfx.renderer;
import ecs_utils.imgui_bind;
@ -58,6 +58,7 @@ struct Launcher
uint style = 3;
uint entities_count;
bool multithreading;
int threads = 1;
ulong timer_freq;
double delta_time;
uint fps;
@ -95,6 +96,7 @@ struct Launcher
void switchDemo(void function() start, bool function() loop, void function() end, void function(SDL_Event*) event, void function(vec2, Tool, int) tool, const (char)* tips)
{
gui_manager.clear();
//launcher.ent
if(this.end)this.end();
@ -102,6 +104,14 @@ struct Launcher
manager.update("clean");
manager.end();
foreach(ref system; manager.systems)
{
if(system.id != CountSystem.system_id && system.id != CleanSystem.system_id)system.disable();
}
/*launcher.manager.getSystem(CountSystem.system_id).enable();
launcher.manager.getSystem(CleanSystem.system_id).enable();//*/
if(start)start();
this.loop = loop;
this.end = end;
@ -259,7 +269,6 @@ void mainLoop(void* arg)
launcher.repeat_time -= range;
launcher.tool((launcher.mouse.position*launcher.scalling)-launcher.render_position, launcher.used_tool, launcher.tool_size);
}
}
version(WebAssembly)
@ -316,6 +325,22 @@ void mainLoop(void* arg)
if(igMenuItemBool("Multithreading", null, launcher.multithreading, true))
{
launcher.multithreading = !launcher.multithreading;
if(launcher.multithreading)
{
launcher.job_updater.pool.setThreadsNum(launcher.threads);
}
else
{
launcher.job_updater.pool.setThreadsNum(1);
}
}
igSetNextItemWidth(0);
igLabelText("Threads:",null);
igSameLine(0,4);
if(igSliderInt("##Threads",&launcher.threads, 1, 32, null))//"Multithreading", null, launcher.multithreading, true))
{
launcher.job_updater.pool.setThreadsNum(launcher.threads);
//launcher.threads = !launcher.multithreading;
}
if(igBeginMenu("Show",true))
{
@ -439,7 +464,7 @@ void mainLoop(void* arg)
if(launcher.show_demo_wnd)
{
igSetNextWindowPos(ImVec2(launcher.window_size.x - 260, 30), ImGuiCond_Once, ImVec2(0,0));
igSetNextWindowSize(ImVec2(250, 250), ImGuiCond_Once);
igSetNextWindowSize(ImVec2(250, 500), ImGuiCond_Once);
if(igBegin("Demo",&launcher.show_demo_wnd,0))
{
ImDrawList* draw_list = igGetWindowDrawList();
@ -539,11 +564,14 @@ void mainLoop(void* arg)
launcher.renderer.clear();
double loop_time = launcher.getTime();
launcher.job_updater.pool.tryWaitCount = 10000;
if(launcher.loop && !launcher.loop())
{
quit();
*cast(bool*)arg = false;
}
launcher.job_updater.pool.tryWaitCount = 0;
loop_time = launcher.getTime() - loop_time;
double draw_time = launcher.getTime();
@ -686,10 +714,10 @@ int main(int argc, char** argv)
setStyle(3);
launcher.job_updater = Mallocator.make!ECSJobUpdater(12);
launcher.job_updater = Mallocator.make!ECSJobUpdater(1);
//launcher.job_updater.onCreate();
EntityManager.initialize(12);
EntityManager.initialize(32, 1<<16);
launcher.manager = EntityManager.instance;
//launcher.manager.m_thread_id_func = &launcher.job_updater.getThreadID;
@ -709,10 +737,16 @@ int main(int argc, char** argv)
launcher.renderer.initialize();
import mmutils.thread_pool : ThreadPool;
launcher.threads = ThreadPool.getCPUCoresCount();
if(launcher.threads < 2)launcher.threads = 2;
launcher.gui_manager = Mallocator.make!GUIManager();
{
import demos.simple;
import demos.space_invaders;
// launcher.switchDemo(&spaceInvadersStart,&spaceInvadersLoop,&spaceInvadersEnd,&spaceInvadersEvent,&spaceInvadersTool,SpaceInvaders.tips);
launcher.switchDemo(&simpleStart,&simpleLoop,&simpleEnd,&simpleEvent,&simpleTool,Simple.tips);
}
@ -741,6 +775,8 @@ int main(int argc, char** argv)
}
}
EntityManager.destroy();
return 0;
}
@ -785,7 +821,15 @@ void loadGFX()
GfxConfig.materials[0].compile();
GfxConfig.materials[0].bindAttribLocation("positions",0);
GfxConfig.materials[0].bindAttribLocation("tex_coords",1);
GfxConfig.materials[0].bindAttribLocation("depth",2);
GfxConfig.materials[0].bindAttribLocation("vcolor",3);
GfxConfig.materials[0].link();
/* import std.stdio;
writeln("positions ",glGetAttribLocation(GfxConfig.materials[0].data.modules[0].gl_handle,"positions"));
writeln("tex_coords ",glGetAttribLocation(GfxConfig.materials[0].data.modules[0].gl_handle,"tex_coords"));
writeln("depth ",glGetAttribLocation(GfxConfig.materials[0].data.modules[0].gl_handle,"depth"));
writeln("vcolor ",glGetAttribLocation(GfxConfig.materials[0].data.modules[0].gl_handle,"vcolor"));*/
GfxConfig.materials[0].data.uniforms = Mallocator.makeArray!(Material.Uniform)(3);
GfxConfig.materials[0].data.uniforms[0] = Material.Uniform(Material.Type.float4, GfxConfig.materials[0].getLocation("matrix_1"), 0);

View file

@ -6,11 +6,11 @@ import bindbc.sdl;
import cimgui.cimgui;
import ecs.attributes;
import ecs.core;
import ecs.entity;
import ecs.manager;
import ecs.std;
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;

View file

@ -1,19 +0,0 @@
module demos.chipmunk2d;
import app;
import bindbc.sdl;
import cimgui.cimgui;
import ecs.attributes;
import ecs.core;
import ecs.entity;
import ecs.manager;
import ecs.std;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
extern(C):

View file

@ -1,19 +0,0 @@
module demos.events;
import app;
import bindbc.sdl;
import cimgui.cimgui;
import ecs.attributes;
import ecs.core;
import ecs.entity;
import ecs.manager;
import ecs.std;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
extern(C):

View file

@ -1,19 +0,0 @@
module demos.flag_component;
import app;
import bindbc.sdl;
import cimgui.cimgui;
import ecs.attributes;
import ecs.core;
import ecs.entity;
import ecs.manager;
import ecs.std;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
extern(C):

View file

@ -6,11 +6,11 @@ import bindbc.sdl;
import cimgui.cimgui;
import ecs.attributes;
import ecs.core;
import ecs.entity;
import ecs.manager;
import ecs.std;
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;

View file

@ -6,11 +6,11 @@ import bindbc.sdl;
import cimgui.cimgui;
import ecs.attributes;
import ecs.core;
import ecs.entity;
import ecs.manager;
import ecs.std;
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;
@ -47,22 +47,27 @@ struct CTexture
struct DrawSystem
{
mixin ECS.System!1;
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), 0, 0 , 0);
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();
}
}

View file

@ -6,18 +6,18 @@ import bindbc.sdl;
import cimgui.cimgui;
import ecs.attributes;
import ecs.core;
import ecs.entity;
import ecs.manager;
import ecs.std;
import ecs.vector;
import bubel.ecs.attributes;
import bubel.ecs.core;
import bubel.ecs.entity;
import bubel.ecs.manager;
import bubel.ecs.std;
import bubel.ecs.vector;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import ecs_utils.utils;
import std.array : staticArray;
//import std.array : staticArray;
enum float px = 1.0/512.0;
@ -30,8 +30,9 @@ struct MapElement
empty = 0,
apple = 1,
wall = 2,
snake = 3,
snake_head_up = 5,
/* snake_head_up = 5,
snake_head_down = 6,
snake_head_left = 7,
snake_head_right = 8,
@ -44,13 +45,31 @@ struct MapElement
snake_turn_rd = 15,
snake_turn_ru = 16,
snake_vertical = 17,
snake_horizontal = 18
snake_horizontal = 18*/
}
Type type;
EntityID id;
}
enum SnakePart : ubyte
{
head_up = 0,
head_down = 1,
head_left = 2,
head_right = 3,
tail_up = 4,
tail_down = 5,
tail_left = 6,
tail_right = 7,
turn_ld = 8,
turn_lu = 9,
turn_rd = 10,
turn_ru = 11,
vertical = 12,
horizontal = 13
}
struct Snake
{
__gshared const (char)* tips = "Use \"WASD\" keys to move.";
@ -71,6 +90,16 @@ struct Snake
MapElement[map_size * map_size] map;
~this() @nogc nothrow
{
if(snake_destroy_particle_frames)Mallocator.dispose(snake_destroy_particle_frames);
if(smoke_frames)Mallocator.dispose(smoke_frames);
if(apple_tmpl)launcher.manager.freeTemplate(apple_tmpl);
if(snake_tmpl)launcher.manager.freeTemplate(snake_tmpl);
if(snake_destroy_particle)launcher.manager.freeTemplate(snake_destroy_particle);
texture.destory();
}
MapElement element(ivec2 pos)
{
uint index = pos.x + pos.y * map_size;
@ -100,43 +129,11 @@ 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* location = apple_tmpl.getComponent!CILocation;
//*location = random_pos;
//Entity* apple =
launcher.manager.addEntity(apple_tmpl,[CILocation(random_pos).ref_].staticArray);
}
void drawMap()
{
foreach(x; 0 .. map_size)
{
foreach(y; 0 .. map_size)
{
switch(element(ivec2(x,y)).type)
{
case MapElement.Type.apple:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,32*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_head_up:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(48*px,112*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_head_down:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(48*px,144*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_head_left:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,128*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_head_right:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(32*px,128*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_tail_up:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(16*px,112*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_tail_down:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,112*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_tail_left:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(32*px,112*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_tail_right:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,144*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_turn_ld:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(64*px,128*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_turn_lu:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(32*px,144*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_turn_rd:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(16*px,144*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_turn_ru:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(64*px,112*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_vertical:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(16*px,128*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.snake_horizontal:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(48*px,128*px,16*px,16*px), 0, 0 , 0);break;
case MapElement.Type.wall:launcher.renderer.draw(texture, vec2(x*16,y*16), vec2(16,16), vec4(0,0,1,1), 0, 0 , 0);break;
default:break;
}
}
}
}
}
struct Animation
@ -184,6 +181,7 @@ struct CSnake
mixin ECS.Component;
struct Parts
{
uint length = 0;
@ -217,6 +215,7 @@ struct CSnake
}
Parts parts;
CMovement.Direction direction;
}
struct CApple
@ -287,7 +286,7 @@ struct ParticleSystem
{
uint length;
@readonly Entity[] entities;
@readonly CParticle[] particle;
CParticle[] particle;
}
void onUpdate(EntitiesData data)
@ -358,7 +357,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)], 0, 0 , 0);
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);
}
}
}
@ -368,8 +367,8 @@ struct MoveSystem
mixin ECS.System!64;
EntityTemplate* destroy_template;
CLocation* destroy_location;
CParticleVector* destroy_vector;
//CLocation* destroy_location;
//CParticleVector* destroy_vector;
struct EntitiesData
{
@ -383,8 +382,8 @@ struct MoveSystem
void setTemplates()
{
destroy_template = snake.snake_destroy_particle;
destroy_location = destroy_template.getComponent!CLocation;
destroy_vector = destroy_template.getComponent!CParticleVector;
//destroy_location = destroy_template.getComponent!CLocation;
//destroy_vector = destroy_template.getComponent!CParticleVector;
}
void moveLocation(ref CILocation location, CMovement.Direction direction)
@ -424,133 +423,56 @@ struct MoveSystem
else .snake.element(MapElement(),location);
}
static CMovement.Direction getDirection(ivec2 p1, ivec2 p2)
{
if(p1.x - p2.x == -1)return CMovement.direction.right;
else if(p1.x - p2.x == 1)return CMovement.direction.left;
else if(p1.y - p2.y == -1)return CMovement.direction.up;
else if(p1.y - p2.y == 1)return CMovement.direction.down;
else if(p1.x - p2.x > 1)return CMovement.direction.right;
else if(p1.x - p2.x < -1)return CMovement.direction.left;
else if(p1.y - p2.y > 1)return CMovement.direction.up;
else return CMovement.direction.down;
}
static MapElement.Type snakePart(ivec2 p1, ivec2 p2, ivec2 p3)
{
CMovement.Direction direction = getDirection(p1, p2);
CMovement.Direction direction2 = getDirection(p1, p3);
uint case_ = direction*4 + direction2;
final switch(case_)
{
case 0:return MapElement.Type.snake_horizontal;
case 1:return MapElement.Type.snake_horizontal;
case 2:return MapElement.Type.snake_turn_lu;
case 3:return MapElement.Type.snake_turn_ru;
case 4:return MapElement.Type.snake_horizontal;
case 5:return MapElement.Type.snake_horizontal;
case 6:return MapElement.Type.snake_turn_ld;
case 7:return MapElement.Type.snake_turn_rd;
case 8:return MapElement.Type.snake_turn_lu;
case 9:return MapElement.Type.snake_turn_ld;
case 10:return MapElement.Type.snake_vertical;
case 11:return MapElement.Type.snake_vertical;
case 12:return MapElement.Type.snake_turn_ru;
case 13:return MapElement.Type.snake_turn_rd;
case 14:return MapElement.Type.snake_vertical;
case 15:return MapElement.Type.snake_vertical;
}
}
static MapElement.Type snakeTail(ivec2 p1, ivec2 p2)
{
CMovement.Direction direction = getDirection(p1, p2);
final switch(direction)
{
case CMovement.Direction.up:return MapElement.Type.snake_tail_up;
case CMovement.Direction.down:return MapElement.Type.snake_tail_down;
case CMovement.Direction.left:return MapElement.Type.snake_tail_left;
case CMovement.Direction.right:return MapElement.Type.snake_tail_right;
}
}
void onUpdate(EntitiesData data)
{
if(data.snakes)
{
foreach(i; 0..data.length)
{
data.snakes[i].direction = data.movement[i].direction;
ivec2 new_location = data.location[i];
moveLocation(data.location[i], data.movement[i].direction);
final switch(snake.element(data.location[i].location).type)
{
case MapElement.Type.snake_head_up:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_head_down:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_head_left:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_head_right:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_tail_up:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_tail_down:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_tail_left:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_tail_right:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_turn_ld:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_turn_lu:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_turn_rd:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_turn_ru:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_vertical:goto case(MapElement.Type.snake_horizontal);
case MapElement.Type.snake_horizontal:
foreach(ivec2 loc; data.snakes[i].parts)
case MapElement.Type.snake:
foreach(loc; data.snakes[i].parts)
{
destroy_location.x = loc.x * 16;
destroy_location.y = loc.y * 16;
//destroy_location.x = loc.x * 16;
//destroy_location.y = loc.y * 16;
snake.element(MapElement(MapElement.Type.empty, EntityID()),loc);
launcher.manager.addEntity(snake.snake_destroy_particle);
launcher.manager.addEntity(snake.snake_destroy_particle,[CLocation(cast(vec2)(loc * 16)).ref_].staticArray);
CLocation destroy_location;
foreach(j;0..10)
{
destroy_location.x = loc.x * 16 + randomf() * 8 - 4;
destroy_location.y = loc.y * 16 + randomf() * 8 - 4;
destroy_vector.velocity = vec2(randomf(),randomf())*0.4-0.2;
//destroy_vector.velocity = vec2(randomf(),randomf())*0.4-0.2;
snake.element(MapElement(MapElement.Type.empty, EntityID()),loc);
launcher.manager.addEntity(snake.snake_destroy_particle);
launcher.manager.addEntity(snake.snake_destroy_particle, [destroy_location.ref_, CParticleVector(vec2(randomf(),randomf())*0.4-0.2).ref_].staticArray);
}
}
destroy_location.x = new_location.x * 16;
destroy_location.y = new_location.y * 16;
//destroy_location.x = new_location.x * 16;
//destroy_location.y = new_location.y * 16;
snake.element(MapElement(MapElement.Type.empty, EntityID()),new_location);
launcher.manager.addEntity(snake.snake_destroy_particle);
launcher.manager.addEntity(snake.snake_destroy_particle,[CLocation(cast(vec2)(new_location * 16)).ref_].staticArray);
launcher.manager.removeEntity(data.entities[i].id);
break;
case MapElement.Type.wall:break;
//launcher.manager.removeEntity(data.entities[i].id);
//break;
case MapElement.Type.empty:
moveSnake(data.snakes[i], new_location);
final switch(data.movement[i].direction)
{
case CMovement.Direction.up:
snake.element(MapElement(MapElement.Type.snake_head_up, data.entities[i].id),data.location[i].location);
break;
case CMovement.Direction.right:
snake.element(MapElement(MapElement.Type.snake_head_right, data.entities[i].id),data.location[i].location);
break;
case CMovement.Direction.down:
snake.element(MapElement(MapElement.Type.snake_head_down, data.entities[i].id),data.location[i].location);
break;
case CMovement.Direction.left:
snake.element(MapElement(MapElement.Type.snake_head_left, data.entities[i].id),data.location[i].location);
break;
}
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.location[i].location);
if(data.snakes[i].parts.length > 1)
{
MapElement.Type elem_type = snakePart(data.snakes[i].parts[$-1],data.location[i],data.snakes[i].parts[$-2]);
snake.element(MapElement(elem_type, data.entities[i].id),data.snakes[i].parts[$-1]);
elem_type = snakeTail(data.snakes[i].parts[1], data.snakes[i].parts[0]);
snake.element(MapElement(elem_type, data.entities[i].id),data.snakes[i].parts[0]);
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.snakes[i].parts[$-1]);
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.snakes[i].parts[0]);
}
else if(data.snakes[i].parts.length == 1)
{
MapElement.Type elem_type = snakeTail(data.location[i], data.snakes[i].parts[0]);
snake.element(MapElement(elem_type, data.entities[i].id),data.snakes[i].parts[0]);
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.snakes[i].parts[0]);
}
break;
case MapElement.Type.apple:
@ -559,29 +481,13 @@ struct MoveSystem
if(data.snakes[i].parts.length > 1)
{
MapElement.Type elem_type = snakePart(data.snakes[i].parts[$-1],data.location[i],data.snakes[i].parts[$-2]);
snake.element(MapElement(elem_type, data.entities[i].id),data.snakes[i].parts[$-1]);
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.snakes[i].parts[$-1]);
}
else if(data.snakes[i].parts.length == 1)
{
MapElement.Type elem_type = snakeTail(data.location[i], new_location);
snake.element(MapElement(elem_type, data.entities[i].id),new_location);
}
final switch(data.movement[i].direction)
{
case CMovement.Direction.up:
snake.element(MapElement(MapElement.Type.snake_head_up, data.entities[i].id),data.location[i].location);
break;
case CMovement.Direction.right:
snake.element(MapElement(MapElement.Type.snake_head_right, data.entities[i].id),data.location[i].location);
break;
case CMovement.Direction.down:
snake.element(MapElement(MapElement.Type.snake_head_down, data.entities[i].id),data.location[i].location);
break;
case CMovement.Direction.left:
snake.element(MapElement(MapElement.Type.snake_head_left, data.entities[i].id),data.location[i].location);
break;
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),new_location);
}
snake.element(MapElement(MapElement.Type.snake, data.entities[i].id),data.location[i].location);
snake.addApple();
break;
}
@ -593,10 +499,10 @@ struct MoveSystem
{
final switch(data.movement[i].direction)
{
case CMovement.Direction.down:data.location[i].location.y -= 1;break;
case CMovement.Direction.up:data.location[i].location.y += 1;break;
case CMovement.Direction.left:data.location[i].location.x -= 1;break;
case CMovement.Direction.right:data.location[i].location.x += 1;break;
case CMovement.Direction.down:data.location[i].y -= 1;break;
case CMovement.Direction.up:data.location[i].y += 1;break;
case CMovement.Direction.left:data.location[i].x -= 1;break;
case CMovement.Direction.right:data.location[i].x += 1;break;
}
}
}
@ -699,6 +605,132 @@ struct FixSnakeDirectionSystem
}
}
struct DrawAppleSystem
{
mixin ECS.System!1;
struct EntitiesData
{
uint length;
@readonly CILocation[] location;
const (CApple)[] apple;
}
void onUpdate(EntitiesData data)
{
foreach(i; 0..data.location.length)
{
launcher.renderer.draw(snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(0,32*px,16*px,16*px), 0, 0x80808080, 0);
}
}
}
struct DrawSnakeSystem
{
mixin ECS.System!1;
struct EntitiesData
{
uint length;
@readonly CILocation[] location;
const (CSnake)[] snake;
}
static CMovement.Direction getDirection(ivec2 p1, ivec2 p2)
{
if(p1.x - p2.x == -1)return CMovement.direction.right;
else if(p1.x - p2.x == 1)return CMovement.direction.left;
else if(p1.y - p2.y == -1)return CMovement.direction.up;
else if(p1.y - p2.y == 1)return CMovement.direction.down;
else if(p1.x - p2.x > 1)return CMovement.direction.right;
else if(p1.x - p2.x < -1)return CMovement.direction.left;
else if(p1.y - p2.y > 1)return CMovement.direction.up;
else return CMovement.direction.down;
}
static SnakePart snakePart(ivec2 p1, ivec2 p2, ivec2 p3)
{
CMovement.Direction direction = getDirection(p1, p2);
CMovement.Direction direction2 = getDirection(p1, p3);
uint case_ = direction*4 + direction2;
final switch(case_)
{
case 0:return SnakePart.horizontal;
case 1:return SnakePart.horizontal;
case 2:return SnakePart.turn_lu;
case 3:return SnakePart.turn_ru;
case 4:return SnakePart.horizontal;
case 5:return SnakePart.horizontal;
case 6:return SnakePart.turn_ld;
case 7:return SnakePart.turn_rd;
case 8:return SnakePart.turn_lu;
case 9:return SnakePart.turn_ld;
case 10:return SnakePart.vertical;
case 11:return SnakePart.vertical;
case 12:return SnakePart.turn_ru;
case 13:return SnakePart.turn_rd;
case 14:return SnakePart.vertical;
case 15:return SnakePart.vertical;
}
}
static SnakePart snakeTail(ivec2 p1, ivec2 p2)
{
CMovement.Direction direction = getDirection(p1, p2);
final switch(direction)
{
case CMovement.Direction.up:return SnakePart.tail_up;
case CMovement.Direction.down:return SnakePart.tail_down;
case CMovement.Direction.left:return SnakePart.tail_left;
case CMovement.Direction.right:return SnakePart.tail_right;
}
}
static void drawElement(ivec2 loc, SnakePart part)
{
final switch(cast(ubyte)part)
{
case SnakePart.tail_up:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(16,112,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.tail_down:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(0,112,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.tail_left:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(32,112,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.tail_right:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(0,144,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.turn_ld:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(64,128,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.turn_lu:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(32,144,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.turn_rd:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(16,144,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.turn_ru:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(64,112,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.vertical:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(16,128,16,16)*px, 0, 0x80808080, 0);break;
case SnakePart.horizontal:launcher.renderer.draw(.snake.texture, cast(vec2)loc, vec2(16,16), vec4(48,128,16,16)*px, 0, 0x80808080, 0);break;
}
}
void onUpdate(EntitiesData data)
{
foreach(i; 0..data.length)
{
const (CSnake)* snake = &data.snake[i];
scope vec2 loc = cast(vec2)(data.location[i].location * 16);
final switch(snake.direction)
{
case CMovement.Direction.up:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(48,112,16,16)*px, 0, 0x80808080, 0);break;
case CMovement.Direction.down:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(48,144,16,16)*px, 0, 0x80808080, 0);break;
case CMovement.Direction.left:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(0,128,16,16)*px, 0, 0x80808080, 0);break;
case CMovement.Direction.right:launcher.renderer.draw(.snake.texture, vec2(data.location[i].x*16,data.location[i].y*16), vec2(16,16), vec4(32,128,16,16)*px, 0, 0x80808080, 0);break;
}
if(snake.parts.length >1)
{
foreach(j;1..snake.parts.length - 1)drawElement(snake.parts[j]*16, snakePart(snake.parts[j], snake.parts[j+1], snake.parts[j-1]));
drawElement(snake.parts[$-1]*16, snakePart(snake.parts[$-1], data.location[i], snake.parts[$-2]));
drawElement(snake.parts[0]*16, snakeTail(snake.parts[1], snake.parts[0]));
}
else if(snake.parts.length == 1)
{
drawElement(snake.parts[0]*16, snakeTail(data.location[i], snake.parts[0]));
}
}
}
}
struct CleanSystem
{
mixin ECS.System!64;
@ -749,6 +781,8 @@ void snakeStart()
launcher.manager.registerSystem!AnimationSystem(-1);
launcher.manager.registerSystem!ParticleSystem(-1);
launcher.manager.registerSystem!ParticleMovementSystem(-1);
launcher.manager.registerSystem!DrawAppleSystem(99);
launcher.manager.registerSystem!DrawSnakeSystem(101);
launcher.manager.endRegister();
@ -765,9 +799,9 @@ void snakeStart()
{
ushort[4] components = [CILocation.component_id, CSnake.component_id, CMovement.component_id, CInput.component_id];
snake.snake_tmpl = launcher.manager.allocateTemplate(components);
CILocation* loc_comp = snake.snake_tmpl.getComponent!CILocation;
loc_comp.location = ivec2(2,2);
launcher.manager.addEntity(snake.snake_tmpl);
//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);
}
{
@ -784,9 +818,9 @@ void snakeStart()
snake.addApple();
}
launcher.gui_manager.addTemplate(snake.snake_tmpl, "Snake");
launcher.gui_manager.addTemplate(snake.apple_tmpl, "Apple");
launcher.gui_manager.addTemplate(snake.snake_destroy_particle, "Particle");
launcher.gui_manager.addTemplate(launcher.manager.allocateTemplate(snake.snake_tmpl), "Snake");
launcher.gui_manager.addTemplate(launcher.manager.allocateTemplate(snake.apple_tmpl), "Apple");
launcher.gui_manager.addTemplate(launcher.manager.allocateTemplate(snake.snake_destroy_particle), "Particle");
MoveSystem* move_system = launcher.manager.getSystem!MoveSystem();
move_system.setTemplates();
@ -794,7 +828,7 @@ void snakeStart()
/*foreach(i; 0..10)
foreach(j; 0..10)
{
loc_comp.location = vec2(i*32+64,j*32+64);
loc_compation = vec2(i*32+64,j*32+64);
launcher.manager.addEntity(simple.tmpl);
}*/
}
@ -815,15 +849,15 @@ void snakeTool(vec2 position, Tool tool, int size)
CLocation* location = tmpl.getComponent!CLocation;
if(location)
{
position.x += (randomf - 0.5) * size;
position.y += (randomf - 0.5) * size;
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;
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);
@ -878,7 +912,7 @@ bool snakeLoop()
launcher.manager.end();
snake.drawMap();
//snake.drawMap();
return true;
}

File diff suppressed because it is too large Load diff

View file

@ -1,13 +1,13 @@
module game_core.job_updater;
import ecs.std;
import ecs.vector;
import ecs.atomic;
import bubel.ecs.std;
import bubel.ecs.vector;
import bubel.ecs.atomic;
import ecs_utils.utils;
//import core.time;
import ecs.manager;
import bubel.ecs.manager;
import mmutils.thread_pool;
version(LDC)
@ -63,6 +63,7 @@ struct ECSJobUpdater
JobData[1024] jobs;
JobCaller[1024] callers;
uint count = 0;
string name;
void dependantOn(Group* dependency)
{
@ -89,7 +90,7 @@ struct ECSJobUpdater
void add(JobCaller caller)
{
callers[count] = caller;
jobs[count] = JobData(&callers[count].callJob,"hmm");
jobs[count] = JobData(&callers[count].callJob,name);
count++;
}
}
@ -216,6 +217,8 @@ struct ECSJobUpdater
return;
}
jobs[group.id].name = cast(string)group.caller.system.name;
foreach(ref job;group.jobs)
{
uint index = 0;
@ -233,7 +236,7 @@ struct ECSJobUpdater
foreach(dep;group.dependencies)
{
if(jobs[dep.id].count && dep.caller.system.execute && dep.caller.system.enabled)jobs[group.id].dependantOn(&jobs[dep.id]);
if(jobs[dep.id].count && dep.caller.system.willExecute && dep.caller.system.enabled)jobs[group.id].dependantOn(&jobs[dep.id]);
else deps--;
}

View file

@ -4,10 +4,10 @@ import app;
import cimgui.cimgui;
import ecs.std;
import ecs.system;
import ecs.vector;
import ecs.entity;
import bubel.ecs.std;
import bubel.ecs.system;
import bubel.ecs.vector;
import bubel.ecs.entity;
import gui.system;
import gui.template_;
@ -30,6 +30,7 @@ struct GUIManager
systems.clear();
templates.clear();
selected_tempalte = 0;
}
EntityTemplate* getSelectedTemplate()

View file

@ -1,6 +1,6 @@
module gui.system;
import ecs.system;
import bubel.ecs.system;
struct SystemGUI
{

View file

@ -1,6 +1,6 @@
module gui.template_;
import ecs.entity;
import bubel.ecs.entity;
struct TemplateGUI
{