Skip to content

Commit

Permalink
Has been implemented ModuleParticles
Browse files Browse the repository at this point in the history
  • Loading branch information
knela96 committed Apr 9, 2018
1 parent 9b68f72 commit cd5fd16
Show file tree
Hide file tree
Showing 16 changed files with 230 additions and 355 deletions.
28 changes: 9 additions & 19 deletions Animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ class Animation
SDL_Rect& GetCurrentFrame()
{
current_frame += speed;
if(current_frame >= last_frame)
current_frame = 0;
if (current_frame >= last_frame)
{
current_frame = (loop) ? 0.0f : last_frame - 1;
loops++;
}

return frames[(int)current_frame];
}

SDL_Rect& GetCurrentFrameNotCycling(int lastframe)
bool Finished() const
{
if (current_frame < lastframe) {
current_frame += speed;
}
return frames[(int)current_frame];
return loops > 0;
}

void reset_currentFrame() {
void Reset()
{
current_frame = 0;
frames[0];
}

bool islastframe() {
Expand All @@ -53,16 +53,6 @@ class Animation
return false;
}
}

bool Finished() const
{
return loops > 0;
}





};

#endif
7 changes: 3 additions & 4 deletions Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ Application::Application()
modules[2] = input = new ModuleInput();
modules[3] = textures = new ModuleTextures();
modules[4] = audio = new ModuleAudio();
modules[12] = player = new ModulePlayer();
modules[11] = particles = new ModuleParticles();
modules[10] = enemy = new ModuleEnemy();
modules[8] = scene_MainMenu = new ModuleSceneMainMenu();
modules[9] = scene_choosePlayer = new ModuleSceneChoosePlayer();
modules[7] = scene_stage1 = new ModuleSceneStage1();
modules[6] = scene_stage2 = new ModuleSceneStage2();
modules[5] = scene_congrats = new ModuleSceneCongrats();
modules[12] = player = new ModulePlayer();
modules[11] = particles = new ModuleParticles();
modules[10] = enemy = new ModuleEnemy();
modules[13] = fade = new ModuleFadeToBlack();//Must be after all scenes
}

Expand All @@ -51,7 +51,6 @@ bool Application::Init()

player->Disable();
enemy->Disable();
particles->Disable();


for(int i = 0; i < NUM_MODULES && ret == true; ++i)
Expand Down
15 changes: 6 additions & 9 deletions Module.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef __MODULE_H__
#define __MODULE_H__

#include "Globals.h"

class Module
{
private:
Expand All @@ -11,12 +9,12 @@ class Module
public:
virtual ~Module() {}

virtual bool Init() { return true; }
virtual bool Start() { return true; }
virtual update_status PreUpdate() { return update_status::UPDATE_CONTINUE; }
virtual update_status Update() { return update_status::UPDATE_CONTINUE; }
virtual update_status PostUpdate() { return update_status::UPDATE_CONTINUE; }
virtual bool CleanUp() { return true; }
virtual bool Init() { return true; }
virtual bool Start() { return true; }
virtual update_status PreUpdate() { return update_status::UPDATE_CONTINUE; }
virtual update_status Update() { return update_status::UPDATE_CONTINUE; }
virtual update_status PostUpdate() { return update_status::UPDATE_CONTINUE; }
virtual bool CleanUp() { return true; }

bool IsEnabled() const { return enabled; }

Expand All @@ -31,7 +29,6 @@ class Module

void Disable()
{
// TODO 0: Call CleanUp() for disabling a module
if (enabled == true)
{
enabled = false;
Expand Down
25 changes: 18 additions & 7 deletions ModuleInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,29 @@ update_status ModuleInput::PreUpdate()
{
SDL_PumpEvents();

keyboard = SDL_GetKeyboardState(NULL);
const Uint8* keys = SDL_GetKeyboardState(NULL);

if(keyboard[SDL_SCANCODE_ESCAPE])
return update_status::UPDATE_STOP;

while (SDL_PollEvent(&Events))
for (int i = 0; i < MAX_KEYS; ++i)
{
if (Events.type == SDL_QUIT) {
return update_status::UPDATE_STOP;
if (keys[i] == 1)
{
if (keyboard[i] == KEY_IDLE)
keyboard[i] = KEY_DOWN;
else
keyboard[i] = KEY_REPEAT;
}
else
{
if (keyboard[i] == KEY_REPEAT || keyboard[i] == KEY_DOWN)
keyboard[i] = KEY_UP;
else
keyboard[i] = KEY_IDLE;
}
}

if(keyboard[SDL_SCANCODE_ESCAPE])
return update_status::UPDATE_STOP;

return update_status::UPDATE_CONTINUE;
}

Expand Down
13 changes: 11 additions & 2 deletions ModuleInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@

typedef unsigned char Uint8;

#define MAX_KEYS 300

enum KEY_STATE
{
KEY_IDLE = 0,
KEY_DOWN,
KEY_REPEAT,
KEY_UP
};

class ModuleInput : public Module
{
public:
Expand All @@ -20,8 +30,7 @@ class ModuleInput : public Module
bool CleanUp();

public:
const Uint8 *keyboard = nullptr;
SDL_Event Events;
KEY_STATE keyboard[MAX_KEYS];
};

#endif // __ModuleInput_H__
157 changes: 42 additions & 115 deletions ModuleParticles.cpp
Original file line number Diff line number Diff line change
@@ -1,158 +1,85 @@
#include "Globals.h"
#include "Application.h"
#include "ModuleParticles.h"
#include "ModuleEnemy.h"
#include "ModulePlayer.h"
#include "ModuleInput.h"
#include "ModuleAudio.h"
#include "SDL/include/SDL.h"
#include "ModuleTextures.h"
#include "ModuleRender.h"

#include "SDL/include/SDL.h"
#include "SDL/include/SDL_timer.h"

ModuleParticles::ModuleParticles() : Module() {

for (uint i = 0; i < MAX_ACTIVE_PARTICLES; ++i)
active[i] = nullptr;


}

// Destructor



ModuleParticles::~ModuleParticles() {}

// Called before render is available
bool ModuleParticles::Start()
{
bool ret = true;


LOG("Loading player textures");

shoot.common_fx = App->audio->LoadS("Assets/Audio Files/SFX in WAV/xmultipl-114.wav");

graphics = App->textures->Load("Assets/Player.png"); // arcade version
shoot.anim.PushBack({ 64, 30, 17, 18});
shoot.anim.speed = 0.1f;
return ret;
}
shoot.anim.loop = false;
shoot.anim.speed = 3.0f;

// Called every draw update
update_status ModuleParticles::Update()
{

player = App->player;





start_time = (Uint32 *)SDL_GetTicks();

//Rectangle Movement

if (App->input->keyboard[SDL_SCANCODE_SPACE] == 1) {
if (start_time - shooting_delay > 250) {
for (int i = 0; i < 10 && (start_time - shooting_delay > 250); ++i) {
if (shoot.bullet == nullptr) {
shooting_delay = start_time;
shoot.bullet = new SDL_Rect{ player->position.x + 15, player->position.y };
App->audio->PlaySound(shoot.common_fx);
shoot.position.x = App->player->position.x;
shoot.position.y = App->player->position.y;
break;
}
}
}
}
/*
/*
//Check Collisions
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 30; ++j) {
if (bullets[i].bullet != nullptr && App->enemy->enemies[j].collision != nullptr) {
if (checkCollision(bullets[i].bullet, App->enemy->enemies[j].collision)) {
bullets[i].bullet = nullptr;
App->enemy->enemies[j].collision = nullptr;
break;
}
}
}
}
*/
for (int i = 0; i < 10; ++i) {
if (shoot.bullet != nullptr) {
if (shoot.position.x > SCREEN_WIDTH) {
shoot.bullet = nullptr;
}
else {
shoot.position.x += 2;
App->render->Blit(graphics, shoot.position.x, shoot.position.y - 15, shoot.bullet);
}
}
}
;
return update_status::UPDATE_CONTINUE;
return ret;
}

// Called before quitting
// Unload assets
bool ModuleParticles::CleanUp()
{

for (int i = 0; i < 10; ++i) {
shoot.bullet = nullptr;
}
player = nullptr;

LOG("Unloading particles");
App->textures->Unload(graphics);
App->audio->UnloadS(shoot.common_fx);
return true;
}

bool ModuleParticles::checkCollision(SDL_Rect* bullet, SDL_Rect* enemy) {
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;

//Calculate the sides of rect A
leftA = bullet->x;
rightA = bullet->x + bullet->w;
topA = bullet->y;
bottomA = bullet->y + bullet->h;

//Calculate the sides of rect B
leftB = enemy->x;
rightB = enemy->x + enemy->w;
topB = enemy->y;
bottomB = enemy->y + enemy->h;

//If any of the sides from A are outside of B
if (bottomA <= topB)
for (uint i = 0; i < MAX_ACTIVE_PARTICLES; ++i)
{
return false;
if (active[i] != nullptr)
{
delete active[i];
active[i] = nullptr;
}
}

if (topA >= bottomB)
{
return false;
}
return true;
}

if (rightA <= leftB)
// Update: draw background
update_status ModuleParticles::Update()
{
for (uint i = 0; i < MAX_ACTIVE_PARTICLES; ++i)
{
return false;
}
Particle* p = active[i];

if (leftA >= rightB)
{
return false;
if (p == nullptr)
continue;

if (p->Update() == false)
{
delete p;
active[i] = nullptr;
}
else if (SDL_GetTicks() >= p->born)
{
App->render->Blit(graphics, p->position.x, p->position.y, &(p->anim.GetCurrentFrame()));
if (p->fx_played == false)
{
p->fx_played = true;
p->speed.x = 2;
App->audio->PlaySound(shoot.common_fx);
// Play particle fx here
}
}
}

//If none of the sides from A are outside B
return true;
return UPDATE_CONTINUE;
}

void ModuleParticles::AddParticle(const Particle& particle, int x, int y, Uint32 delay)
Expand All @@ -161,7 +88,7 @@ void ModuleParticles::AddParticle(const Particle& particle, int x, int y, Uint32
p->born = SDL_GetTicks() + delay;
p->position.x = x;
p->position.y = y;

p->life = 1000;
active[last_particle++] = p;
}

Expand Down
Loading

0 comments on commit cd5fd16

Please sign in to comment.