Added Upgrade bonuses to SpaceInvaders demo

This commit is contained in:
Mergul 2020-05-13 21:51:38 +02:00
parent d26c940b80
commit f731b4cedb

View file

@ -226,6 +226,11 @@ struct CHitMark
ubyte value = 0;
}
struct CUpgrade
{
mixin ECS.Component;
}
/*#######################################################################################################################
------------------------------------------------ Events ------------------------------------------------------------------
#######################################################################################################################*/
@ -242,6 +247,11 @@ struct EChangeDirection
Direction direction;
}
struct EUpgrade
{
mixin ECS.Event;
}
struct EDamage
{
mixin ECS.Event;
@ -487,7 +497,7 @@ struct LaserShootingSystem
mixin ECS.System!32;
bool shoot = false;
static float[22] laser_shoot_times = [2000,1500,1000,700,500,350,250,170,130,80,50,25,15,10,5,2.5,2,1,0.8,0.5,0.3,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;
CVelocity* laser_velocity;
@ -583,7 +593,55 @@ struct LaserCollisionSystem
}
}
}
}
struct UpgradeCollisionSystem
{
mixin ECS.System!32;
mixin ECS.ReadOnlyDependencies!(ShootGridDependency);
struct EntitiesData
{
///variable named "length" contain entites count
uint length;
const (Entity)[] entity;
@readonly CLocation[] location;
@readonly CUpgrade[] upgrade;
}
void onUpdate(EntitiesData data)
{
EntityID id;
foreach(i; 0..data.length)
{
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);
}
}
}
}
struct UpgradeSystem
{
mixin ECS.System;
struct EntitiesData
{
const (Entity)[] entity;
@readonly CShip[] ship;
}
void handleEvent(Entity* entity, EUpgrade event)
{
CLaserWeapon* laser = entity.getComponent!CLaserWeapon;
if(laser)
{
if(laser.level < LaserShootingSystem.laser_shoot_times.length)laser.level++;
}
}
}
struct ChangeDirectionSystem
@ -893,6 +951,7 @@ struct MovementSystem
}
}
extern(C) float sqrtf(float x) @nogc nothrow @system;
/**
@ -1012,9 +1071,11 @@ void spaceInvadersStart()
launcher.manager.registerComponent!CGuild;
launcher.manager.registerComponent!CHitPoints;
launcher.manager.registerComponent!CHitMark;
launcher.manager.registerComponent!CUpgrade;
launcher.manager.registerEvent!EChangeDirection;
launcher.manager.registerEvent!EDamage;
launcher.manager.registerEvent!EUpgrade;
//launcher.manager.registerSystem!MoveSystem(0);
launcher.manager.registerSystem!DrawSystem(100);
@ -1028,6 +1089,8 @@ void spaceInvadersStart()
launcher.manager.registerSystem!ShootGridCleaner(-101);
launcher.manager.registerSystem!HitPointsSystem(0);
launcher.manager.registerSystem!HitMarkingSystem(-100);
launcher.manager.registerSystem!UpgradeCollisionSystem(-70);
launcher.manager.registerSystem!UpgradeSystem(-100);
launcher.manager.endRegister();
@ -1051,7 +1114,7 @@ void spaceInvadersStart()
CLocation* loc_comp = space_invaders.ship_tmpl.getComponent!CLocation;
loc_comp.value = vec2(64,64);
CLaserWeapon* weapon = space_invaders.ship_tmpl.getComponent!CLaserWeapon;
weapon.level = 22;
weapon.level = 1;
space_invaders.ship_tmpl.getComponent!CHitPoints().value = 1000;
launcher.manager.addEntity(space_invaders.ship_tmpl);
@ -1112,6 +1175,18 @@ void spaceInvadersStart()
grouped_id = current_entity.id;
//grouped_tmpl = launcher.manager.allocateTemplate(current_entity.id);
}
EntityTemplate* upgrade_tmpl;
{
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);
}
launcher.manager.commit();
enemy_tmpl = launcher.manager.allocateTemplate(enemy_id);
@ -1121,6 +1196,7 @@ void spaceInvadersStart()
launcher.gui_manager.addTemplate(enemy_tmpl,"Enemy");
launcher.gui_manager.addTemplate(grouped_tmpl,"Grouped enemy");
launcher.gui_manager.addTemplate(launcher.manager.allocateTemplate(space_invaders.laser_tmpl),"Laser");
launcher.gui_manager.addTemplate(upgrade_tmpl,"Upgrade");
}
@ -1149,6 +1225,8 @@ void spaceInvadersTool(vec2 position, Tool tool, int size)
{
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;
}
CLaserWeapon* laser_weapon = tmpl.getComponent!CLaserWeapon;