diff --git a/demos/source/demos/space_invaders.d b/demos/source/demos/space_invaders.d index 1c5b3e4..bef65fe 100644 --- a/demos/source/demos/space_invaders.d +++ b/demos/source/demos/space_invaders.d @@ -16,7 +16,7 @@ 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; @@ -252,6 +252,11 @@ struct EUpgrade mixin ECS.Event; } +struct EDeath +{ + mixin ECS.Event; +} + struct EDamage { mixin ECS.Event; @@ -499,32 +504,72 @@ struct LaserShootingSystem bool shoot = false; static float[18] laser_shoot_times = [500,400,300,200,100,50,25,10,5,2,1,0.8,0.6,0.5,0.4,0.3,0.2,0.1]; - CLocation* laser_location; + /*CLocation* laser_location; CVelocity* laser_velocity; - CGuild* laser_guild; + CGuild* laser_guild;*/ struct EntitiesData { ///variable named "length" contain entites count uint length; + ///variable named "length" contain thread identifier + uint thread_id; @readonly CShootDirection[] shoot_direction; @readonly @optional CAutoShoot[] auto_shoot; @readonly CLocation[] location; @readonly CGuild[] guild; CLaserWeapon[] laser; } + + struct ThreadData + { + EntityTemplate* laser_tmpl; + CLocation* laser_location; + CVelocity* laser_velocity; + CGuild* laser_guild; + } + + ThreadData[] threads; ///Called inside "registerSystem" function - /*void onCreate() + void onCreate() { - laser_location = space_invaders.laser_tmpl.getComponent!CLocation; - }*/ + threads = Mallocator.makeArray!ThreadData(32); + threads[0].laser_tmpl = launcher.manager.allocateTemplate([CLocation.component_id, CTexture.component_id, CVelocity.component_id, CScale.component_id, CLaser.component_id, CGuild.component_id].staticArray); + + CTexture* tex_comp = threads[0].laser_tmpl.getComponent!CTexture; + tex_comp.tex = space_invaders.texture;//laser_tex; + tex_comp.coords = vec4(0*px,24*px,2*px,8*px); + CScale* scale_comp = threads[0].laser_tmpl.getComponent!CScale; + scale_comp.value = vec2(2,8); + threads[0].laser_location = threads[0].laser_tmpl.getComponent!CLocation; + threads[0].laser_velocity = threads[0].laser_tmpl.getComponent!CVelocity; + threads[0].laser_guild = threads[0].laser_tmpl.getComponent!CGuild; + + foreach(ref ThreadData thread;threads[1..$]) + { + thread.laser_tmpl = launcher.manager.allocateTemplate(threads[0].laser_tmpl); + thread.laser_location = thread.laser_tmpl.getComponent!CLocation; + thread.laser_velocity = thread.laser_tmpl.getComponent!CVelocity; + thread.laser_guild = thread.laser_tmpl.getComponent!CGuild; + } + //laser_location = space_invaders.laser_tmpl.getComponent!CLocation; + } + + void onDestroy() + { + foreach(ref ThreadData thread;threads[1..$]) + { + launcher.manager.freeTemplate(thread.laser_tmpl); + } + Mallocator.dispose(threads); + } bool onBegin() { - laser_location = space_invaders.laser_tmpl.getComponent!CLocation; + /*laser_location = space_invaders.laser_tmpl.getComponent!CLocation; laser_velocity = space_invaders.laser_tmpl.getComponent!CVelocity; - laser_guild = space_invaders.laser_tmpl.getComponent!CGuild; + laser_guild = space_invaders.laser_tmpl.getComponent!CGuild;*/ if(launcher.getKeyState(SDL_SCANCODE_SPACE)) { shoot = true; @@ -535,6 +580,7 @@ struct LaserShootingSystem void onUpdate(EntitiesData data) { + ThreadData* thread = &threads[data.thread_id]; //conditional branch for whole entities block if(shoot || data.auto_shoot) { @@ -545,10 +591,10 @@ struct LaserShootingSystem while(laser.shoot_time > laser_shoot_times[laser.level - 1]) { laser.shoot_time -= laser_shoot_times[laser.level - 1]; - laser_location.value = data.location[i]; - laser_velocity.value = vec2(randomf()*0.5-0.25,data.shoot_direction[i].direction == Direction.up ? 1.0 : -1.0); - laser_guild.guild = data.guild[i].guild; - launcher.manager.addEntity(space_invaders.laser_tmpl); + thread.laser_location.value = data.location[i]; + thread.laser_velocity.value = vec2(randomf()*0.5-0.25,data.shoot_direction[i].direction == Direction.up ? 1.0 : -1.0); + thread.laser_guild.guild = data.guild[i].guild; + launcher.manager.addEntity(thread.laser_tmpl); } } } @@ -617,8 +663,12 @@ struct UpgradeCollisionSystem { if(space_invaders.shoot_grid.test(id, data.location[i], cast(ubyte)(0xFF))) { - launcher.manager.sendEvent(id, EUpgrade()); - launcher.manager.removeEntity(data.entity[i].id); + Entity* entity = launcher.manager.getEntity(id); + if(entity.hasComponent(CShip.component_id)) + { + launcher.manager.sendEvent(id, EUpgrade()); + launcher.manager.removeEntity(data.entity[i].id); + } } } } @@ -676,6 +726,7 @@ struct ChangeDirectionSystem if(direction != cast(Direction)-1)//return true; { has_changes = true; +INFO: Uniform block alig break; } } @@ -833,19 +884,61 @@ struct HitPointsSystem { mixin ECS.System; + EntityTemplate* upgrade_tmpl; + CLocation* upgrade_location; + struct EntitiesData { CHitPoints[] hp; } + void onCreate() + { + upgrade_tmpl = launcher.manager.allocateTemplate([CVelocity.component_id, CLocation.component_id, CTexture.component_id, CScale.component_id, CUpgrade.component_id].staticArray); + CTexture* tex_comp = upgrade_tmpl.getComponent!CTexture; + tex_comp.tex = space_invaders.texture;//ship_tex; + tex_comp.coords = vec4(0*px,32*px,16*px,16*px); + CVelocity* vel_comp = upgrade_tmpl.getComponent!CVelocity; + vel_comp.value = vec2(0,-0.1); + upgrade_location = upgrade_tmpl.getComponent!CLocation; + } + + void onDestroy() + { + launcher.manager.freeTemplate(upgrade_tmpl); + } + void handleEvent(Entity* entity, EDamage event) { CHitPoints* hp = entity.getComponent!CHitPoints; + if(*hp < 0)return; *hp -= event.damage; - if(*hp < 0)launcher.manager.removeEntity(entity.id); + if(*hp < 0) + { + launcher.manager.sendEvent(entity.id, EDeath()); + //launcher.manager.removeEntity(entity.id); + } CHitMark* hit_mark = entity.getComponent!CHitMark; if(hit_mark)hit_mark.value = 127; } + + void handleEvent(Entity* entity, EDeath event) + { + CEnemy* enemy = entity.getComponent!CEnemy; + if(enemy) + { + if(randomRange(0, 1000) < 5) + { + CLocation* location = entity.getComponent!CLocation; + if(location) + { + *upgrade_location = *location; + launcher.manager.addEntity(upgrade_tmpl); + } + } + } + launcher.manager.removeEntity(entity.id); + } } struct ClampPositionSystem @@ -1076,6 +1169,7 @@ void spaceInvadersStart() launcher.manager.registerEvent!EChangeDirection; launcher.manager.registerEvent!EDamage; launcher.manager.registerEvent!EUpgrade; + launcher.manager.registerEvent!EDeath; //launcher.manager.registerSystem!MoveSystem(0); launcher.manager.registerSystem!DrawSystem(100);