SpaceShip demo update

-fixed issue with firing lasers in multithreaded mode
-fix: enies can't grab upgrade anymore
-there is a change that enemy drop bonus upon death
This commit is contained in:
Mergul 2020-05-14 22:20:41 +02:00
parent 9589a5cb2d
commit 6c3c803d1e

View file

@ -16,7 +16,7 @@ import ecs_utils.gfx.texture;
import ecs_utils.math.vector; import ecs_utils.math.vector;
import ecs_utils.utils; import ecs_utils.utils;
import std.array : staticArray; //import std.array : staticArray;
enum float px = 1.0/512.0; enum float px = 1.0/512.0;
@ -252,6 +252,11 @@ struct EUpgrade
mixin ECS.Event; mixin ECS.Event;
} }
struct EDeath
{
mixin ECS.Event;
}
struct EDamage struct EDamage
{ {
mixin ECS.Event; mixin ECS.Event;
@ -499,32 +504,72 @@ struct LaserShootingSystem
bool shoot = false; 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]; 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; CVelocity* laser_velocity;
CGuild* laser_guild; CGuild* laser_guild;*/
struct EntitiesData struct EntitiesData
{ {
///variable named "length" contain entites count ///variable named "length" contain entites count
uint length; uint length;
///variable named "length" contain thread identifier
uint thread_id;
@readonly CShootDirection[] shoot_direction; @readonly CShootDirection[] shoot_direction;
@readonly @optional CAutoShoot[] auto_shoot; @readonly @optional CAutoShoot[] auto_shoot;
@readonly CLocation[] location; @readonly CLocation[] location;
@readonly CGuild[] guild; @readonly CGuild[] guild;
CLaserWeapon[] laser; CLaserWeapon[] laser;
} }
struct ThreadData
{
EntityTemplate* laser_tmpl;
CLocation* laser_location;
CVelocity* laser_velocity;
CGuild* laser_guild;
}
ThreadData[] threads;
///Called inside "registerSystem" function ///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() 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_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)) if(launcher.getKeyState(SDL_SCANCODE_SPACE))
{ {
shoot = true; shoot = true;
@ -535,6 +580,7 @@ struct LaserShootingSystem
void onUpdate(EntitiesData data) void onUpdate(EntitiesData data)
{ {
ThreadData* thread = &threads[data.thread_id];
//conditional branch for whole entities block //conditional branch for whole entities block
if(shoot || data.auto_shoot) if(shoot || data.auto_shoot)
{ {
@ -545,10 +591,10 @@ struct LaserShootingSystem
while(laser.shoot_time > laser_shoot_times[laser.level - 1]) while(laser.shoot_time > laser_shoot_times[laser.level - 1])
{ {
laser.shoot_time -= laser_shoot_times[laser.level - 1]; laser.shoot_time -= laser_shoot_times[laser.level - 1];
laser_location.value = data.location[i]; thread.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); thread.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; thread.laser_guild.guild = data.guild[i].guild;
launcher.manager.addEntity(space_invaders.laser_tmpl); 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))) if(space_invaders.shoot_grid.test(id, data.location[i], cast(ubyte)(0xFF)))
{ {
launcher.manager.sendEvent(id, EUpgrade()); Entity* entity = launcher.manager.getEntity(id);
launcher.manager.removeEntity(data.entity[i].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; if(direction != cast(Direction)-1)//return true;
{ {
has_changes = true; has_changes = true;
INFO: Uniform block alig
break; break;
} }
} }
@ -833,19 +884,61 @@ struct HitPointsSystem
{ {
mixin ECS.System; mixin ECS.System;
EntityTemplate* upgrade_tmpl;
CLocation* upgrade_location;
struct EntitiesData struct EntitiesData
{ {
CHitPoints[] hp; 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) void handleEvent(Entity* entity, EDamage event)
{ {
CHitPoints* hp = entity.getComponent!CHitPoints; CHitPoints* hp = entity.getComponent!CHitPoints;
if(*hp < 0)return;
*hp -= event.damage; *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; CHitMark* hit_mark = entity.getComponent!CHitMark;
if(hit_mark)hit_mark.value = 127; 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 struct ClampPositionSystem
@ -1076,6 +1169,7 @@ void spaceInvadersStart()
launcher.manager.registerEvent!EChangeDirection; launcher.manager.registerEvent!EChangeDirection;
launcher.manager.registerEvent!EDamage; launcher.manager.registerEvent!EDamage;
launcher.manager.registerEvent!EUpgrade; launcher.manager.registerEvent!EUpgrade;
launcher.manager.registerEvent!EDeath;
//launcher.manager.registerSystem!MoveSystem(0); //launcher.manager.registerSystem!MoveSystem(0);
launcher.manager.registerSystem!DrawSystem(100); launcher.manager.registerSystem!DrawSystem(100);