From a55b1f80fd716f7588d9f65c4e7a737072a770f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=88ric=20Canela?= Date: Wed, 18 Apr 2018 23:01:45 +0200 Subject: [PATCH] tentacles moving with the player --- Application.cpp | 3 + Application.h | 4 +- ModuleAudio.cpp | 2 +- ModulePlayer.cpp | 21 +++++- ModuleRender.cpp | 6 +- ModuleRender.h | 2 +- ModuleSceneStage1.cpp | 21 +++--- ModuleTentacles.cpp | 139 ++++++++++++++++++++++++++++++++++++++ ModuleTentacles.h | 45 ++++++++++++ Tentacles.cpp | 16 ----- Tentacles.h | 26 ------- XMultiply.vcxproj | 4 +- XMultiply.vcxproj.filters | 4 +- 13 files changed, 229 insertions(+), 64 deletions(-) create mode 100644 ModuleTentacles.cpp create mode 100644 ModuleTentacles.h delete mode 100644 Tentacles.cpp delete mode 100644 Tentacles.h diff --git a/Application.cpp b/Application.cpp index e0f47e5..9715692 100644 --- a/Application.cpp +++ b/Application.cpp @@ -14,6 +14,7 @@ #include "ModuleSceneStage2.h" #include "ModuleSceneCongrats.h" #include "ModuleCollision.h" +#include "ModuleTentacles.h" #include "ModuleFonts.h" Application::Application() @@ -30,6 +31,7 @@ Application::Application() modules[i++] = scene_stage1 = new ModuleSceneStage1(); modules[i++] = scene_stage2 = new ModuleSceneStage2(); modules[i++] = scene_congrats = new ModuleSceneCongrats(); + modules[i++] = tentacles = new ModuleTentacles(); modules[i++] = player = new ModulePlayer(); modules[i++] = enemies = new ModuleEnemies(); modules[i++] = particles = new ModuleParticles(); @@ -58,6 +60,7 @@ bool Application::Init() scene_congrats->Disable(); + tentacles->Disable(); player->Disable(); enemies->Disable(); diff --git a/Application.h b/Application.h index 4769567..1c96f6d 100644 --- a/Application.h +++ b/Application.h @@ -3,7 +3,7 @@ #include "Globals.h" -#define NUM_MODULES 16 +#define NUM_MODULES 17 class Module; class ModuleWindow; @@ -22,6 +22,7 @@ class ModuleSceneStage1; class ModuleSceneStage2; class ModuleSceneCongrats; class ModuleCollision; +class ModuleTentacles; class Application { @@ -44,6 +45,7 @@ class Application ModuleSceneStage2* scene_stage2 = nullptr; ModuleSceneCongrats* scene_congrats = nullptr; ModuleCollision* collision = nullptr; + ModuleTentacles* tentacles = nullptr; public: diff --git a/ModuleAudio.cpp b/ModuleAudio.cpp index fe2c0ef..e61d03a 100644 --- a/ModuleAudio.cpp +++ b/ModuleAudio.cpp @@ -68,7 +68,7 @@ Mix_Chunk *const ModuleAudio::LoadS(const char* path) bool ModuleAudio::UnloadS(Mix_Chunk * sound) { if (sound != nullptr) - Mix_FreeChunk(sound); + //Mix_FreeChunk(sound); return true; } diff --git a/ModulePlayer.cpp b/ModulePlayer.cpp index 1c07467..cd208a4 100644 --- a/ModulePlayer.cpp +++ b/ModulePlayer.cpp @@ -8,7 +8,7 @@ #include "ModuleCollision.h" #include "ModuleFadeToBlack.h" #include "ModuleFonts.h" -#include "Tentacles.h" +#include "ModuleTentacles.h" #include "SDL/include/SDL.h" @@ -53,6 +53,9 @@ ModulePlayer::ModulePlayer() downwardreturn.speed = 0.075f; current_animation = &idle; + + + } ModulePlayer::~ModulePlayer() @@ -75,6 +78,19 @@ bool ModulePlayer::Start() powerup[PARABOLA_SHOOT] = true; + App->tentacles->AddTentacle(App->tentacles->tentacle, position.x, position.y, false); + App->tentacles->AddTentacle(App->tentacles->tentacle, position.x, position.y, false); + App->tentacles->AddTentacle(App->tentacles->tentacle, position.x, position.y, false); + App->tentacles->AddTentacle(App->tentacles->tentacle, position.x, position.y, false); + App->tentacles->AddTentacle(App->tentacles->tentacle, position.x, position.y, false); + App->tentacles->AddTentacle(App->tentacles->tentacle, position.x, position.y, false); + App->tentacles->AddTentacle(App->tentacles->tentacle, position.x, position.y, true); + App->tentacles->AddTentacle(App->tentacles->tentacle, position.x, position.y, true); + App->tentacles->AddTentacle(App->tentacles->tentacle, position.x, position.y, true); + App->tentacles->AddTentacle(App->tentacles->tentacle, position.x, position.y, true); + App->tentacles->AddTentacle(App->tentacles->tentacle, position.x, position.y, true); + App->tentacles->AddTentacle(App->tentacles->tentacle, position.x, position.y, true); + return ret; } @@ -128,7 +144,6 @@ update_status ModulePlayer::Update() App->particles->AddParticle(App->particles->shoot1, position.x + 40, position.y, COLLIDER_PLAYER_SHOT); App->particles->AddParticle(App->particles->shoot2, position.x + 40, position.y, COLLIDER_PLAYER_SHOT); } - } if (App->input->keyboard[SDL_SCANCODE_S] == KEY_STATE::KEY_IDLE @@ -136,6 +151,8 @@ update_status ModulePlayer::Update() current_animation = &idle; collider->SetPos(position.x, position.y); + + // Draw everything -------------------------------------- if (!dead) App->render->Blit(graphics, position.x, position.y, ¤t_animation->GetCurrentFrame()); diff --git a/ModuleRender.cpp b/ModuleRender.cpp index 14fc2bf..36c2a9c 100644 --- a/ModuleRender.cpp +++ b/ModuleRender.cpp @@ -119,7 +119,7 @@ bool ModuleRender::CleanUp() // return ret; //} -bool ModuleRender::Blit(SDL_Texture* texture, int x, int y, SDL_Rect* section, float speed, bool use_camera, bool flip_horizontal) +bool ModuleRender::Blit(SDL_Texture* texture, int x, int y, SDL_Rect* section, float speed, float angle , bool use_camera, bool flip_horizontal) { bool ret = true; SDL_Rect rect; @@ -151,9 +151,9 @@ bool ModuleRender::Blit(SDL_Texture* texture, int x, int y, SDL_Rect* section, f int result = 0; if (flip_horizontal == true) - result = SDL_RenderCopyEx(renderer, texture, section, &rect, 0.0, NULL, SDL_FLIP_HORIZONTAL); + result = SDL_RenderCopyEx(renderer, texture, section, &rect, angle, NULL, SDL_FLIP_HORIZONTAL); else - result = SDL_RenderCopy(renderer, texture, section, &rect); + result = SDL_RenderCopyEx(renderer, texture, section, &rect, angle, NULL, SDL_FLIP_NONE); if (result != 0) { diff --git a/ModuleRender.h b/ModuleRender.h index 0a6513c..ca268d4 100644 --- a/ModuleRender.h +++ b/ModuleRender.h @@ -19,7 +19,7 @@ class ModuleRender : public Module update_status PreUpdate(); bool CleanUp(); - bool Blit(SDL_Texture* texture, int x, int y, SDL_Rect* section, float speed = 1.0f, bool use_camera = true, bool flip_horizontal = false); + bool Blit(SDL_Texture* texture, int x, int y, SDL_Rect* section, float speed = 1.0f, float angle = 0.0f, bool use_camera = true, bool flip_horizontal = false); bool DrawQuad(const SDL_Rect& rect, Uint8 r, Uint8 g, Uint8 b, Uint8 a, bool use_camera = true); public: diff --git a/ModuleSceneStage1.cpp b/ModuleSceneStage1.cpp index ba3a78b..39b2541 100644 --- a/ModuleSceneStage1.cpp +++ b/ModuleSceneStage1.cpp @@ -11,6 +11,7 @@ #include "ModuleParticles.h" #include "ModuleAudio.h" #include "ModuleCollision.h" +#include "ModuleTentacles.h" ModuleSceneStage1::ModuleSceneStage1() { @@ -37,7 +38,7 @@ bool ModuleSceneStage1::Start() bool ret = true; - + App->tentacles->Enable(); App->player->Enable(); App->particles->Enable(); App->collision->Enable(); @@ -57,15 +58,15 @@ bool ModuleSceneStage1::Start() App->collision->AddCollider({ 1380, 140, 100, 120 }, COLLIDER_WALL); // Enemies - App->enemies->AddEnemy(BROWN_WORM, 300, 50); - App->enemies->AddEnemy(BROWN_WORM, 300, 100); - App->enemies->AddEnemy(BROWN_WORM, 300, 150); - App->enemies->AddEnemy(BROWN_WORM, 300, 200); - App->enemies->AddEnemy(LITTLE_SHRIMP, 400, 200); - App->enemies->AddEnemy(LITTLE_SHRIMP, 415, 200); - App->enemies->AddEnemy(LITTLE_SHRIMP, 440, 200); - App->enemies->AddEnemy(LITTLE_SHRIMP, 465, 200); - App->enemies->AddEnemy(NEMONA_TENTACLE, 520, 148); + /*app->enemies->addenemy(brown_worm, 300, 50); + app->enemies->addenemy(brown_worm, 300, 100); + app->enemies->addenemy(brown_worm, 300, 150); + app->enemies->addenemy(brown_worm, 300, 200); + app->enemies->addenemy(little_shrimp, 400, 200); + app->enemies->addenemy(little_shrimp, 415, 200); + app->enemies->addenemy(little_shrimp, 440, 200); + app->enemies->addenemy(little_shrimp, 465, 200); + app->enemies->addenemy(nemona_tentacle, 520, 148);*/ //POWERUPS App->enemies->AddEnemy(POWERUPSHIP, 400, 150); diff --git a/ModuleTentacles.cpp b/ModuleTentacles.cpp new file mode 100644 index 0000000..397e82a --- /dev/null +++ b/ModuleTentacles.cpp @@ -0,0 +1,139 @@ +#include "Application.h" +#include "ModuleRender.h" +#include "ModuleTextures.h" +#include "ModuleTentacles.h" +#include "ModulePlayer.h" + +ModuleTentacles::ModuleTentacles() +{} + +ModuleTentacles::~ModuleTentacles() +{} + +bool ModuleTentacles::Start() { + + LOG("Loading tentacle textures"); + + graphics = App->textures->Load("Assets/Player.png"); + tentacle.anim.PushBack({ 6, 19, 4, 11 }); + tentacle.anim.loop = false; + tentacle.anim.speed = 0.0f; + /* + tentacle.anim->PushBack({ 17,0,16,16 }); + tentacle.anim->PushBack({ 33,0,16,16 }); + tentacle.anim->PushBack({ 49,0,16,16 }); + tentacle.anim->PushBack({ 65,0,16,16 });*/ + return true; +} + +bool ModuleTentacles::CleanUp() +{ + LOG("Unloading particles"); + App->textures->Unload(graphics); + + for (uint i = 0; i < MAX_TENTACLES; ++i) + { + if (tentacles[i] != nullptr) + { + delete tentacles[i]; + tentacles[i] = nullptr; + } + } + + return true; +} + +update_status ModuleTentacles::Update() { + for (uint i = 0; i < MAX_TENTACLES; ++i) + { + Tentacle* p = tentacles[i]; + + if (p == nullptr) + continue; + + if (p->Update() == false) + { + delete p; + tentacles[i] = nullptr; + }else{ + setPosition(App->player->position.x, App->player->position.y); + if(!p->flip) + App->render->Blit(graphics, p->first_point.x, p->first_point.y, &p->anim.GetCurrentFrame(),1.0f, 0.0f); + else + App->render->Blit(graphics, p->first_point.x, p->first_point.y, &p->anim.GetCurrentFrame(), 1.0f, 180.0f); + + } + } + return UPDATE_CONTINUE; +} + +void ModuleTentacles::AddTentacle(const Tentacle& tentacle, int x, int y, bool flip) +{ + for (uint i = 0; i < MAX_TENTACLES; ++i) + { + if (tentacles[i] == nullptr) + { + Tentacle* p = new Tentacle(tentacle); + p->flip = flip; + tentacles[i] = p; + break; + } + } +} + +void ModuleTentacles::setPosition(int x, int y) { + for (uint i = 0; i < MAX_TENTACLES; ++i) + { + if (tentacles[i] != nullptr) + { + int invert = 0; + Tentacle* p = tentacles[i]; + if (tentacles[i]->flip) + invert = -1; + else + invert = 1; + + if (i != 0 && i != MAX_TENTACLES/2) { + p->first_point.x = tentacles[i - 1]->second_point.x; + p->first_point.y = tentacles[i - 1]->second_point.y; + p->second_point.x = p->first_point.x; + p->second_point.y = p->first_point.y - p->anim.GetCurrentFrame().h * invert; + } + else { + if (tentacles[i]->flip) { + p->first_point.y = y + 9; + } + else { + p->first_point.y = y - 4; + } + p->first_point.x = x + 20; + + p->second_point.x = p->first_point.x; + p->second_point.y = p->first_point.y - p->anim.GetCurrentFrame().h * invert; + } + tentacles[i] = p; + } + } +} + + +//-------------------------------------------- +//-------------------------------------------- + +Tentacle::Tentacle() +{ + first_point.SetToZero(); +} + +Tentacle::Tentacle(const Tentacle& p) : + first_point(p.first_point), second_point(p.second_point), + anchor(p.anchor), anim(p.anim) +{} + +bool Tentacle::Update() +{ + bool ret = true; + + + return ret; +} diff --git a/ModuleTentacles.h b/ModuleTentacles.h new file mode 100644 index 0000000..cd86ecd --- /dev/null +++ b/ModuleTentacles.h @@ -0,0 +1,45 @@ +#ifndef __MODULETENTACLES_H__ +#define __MODULETENTACLES_H__ + +#include "p2Point.h" +#include "Animation.h" +#include "ModuleCollision.h" + +#define MAX_TENTACLES 12 + +struct SDL_Texture; +struct SDL_Rect; +struct Collider; + +struct Tentacle { + Collider* collider = nullptr; + iPoint first_point; + iPoint second_point; + bool anchor = false; + Animation anim; + bool flip = false; + + Tentacle(); + Tentacle(const Tentacle& p); + bool Update(); +}; + + +class ModuleTentacles : public Module +{ +public: + Tentacle tentacle; + Tentacle* tentacles[MAX_TENTACLES]; +private: + SDL_Texture * graphics = nullptr; +public: + ModuleTentacles(); + ~ModuleTentacles(); + bool Start(); + update_status Update(); + bool CleanUp(); + void AddTentacle(const Tentacle& tentacle, int x, int y,bool flip); + void setPosition(int x, int y); +}; + +#endif // __ENEMY_H__ \ No newline at end of file diff --git a/Tentacles.cpp b/Tentacles.cpp deleted file mode 100644 index dcb84f7..0000000 --- a/Tentacles.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "Application.h" -#include "Enemy.h" -#include "ModuleParticles.h" -#include "ModuleRender.h" -#include "Tentacles.h" - -Tentacles::Tentacles(int x, int y):first_point(x,y) -{} - -Tentacles::~Tentacles() -{} - -void Tentacles::Draw(SDL_Texture * sprites) -{ - App->render->Blit(sprites, first_point.x, first_point.y, rect); -} diff --git a/Tentacles.h b/Tentacles.h deleted file mode 100644 index a18b3f0..0000000 --- a/Tentacles.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef __TENTACLES_H__ -#define __TENTACLES_H__ - -#include "p2Point.h" - -struct SDL_Texture; -struct SDL_Rect; - -class Tentacles -{ -public: - iPoint first_point; - iPoint second_point; - SDL_Texture * graphics = nullptr; - bool anchor = false; - SDL_Rect* rect; - -public: - Tentacles(int x, int y); - virtual ~Tentacles(); - - virtual void Move() {}; - virtual void Draw(SDL_Texture* sprites); -}; - -#endif // __ENEMY_H__ \ No newline at end of file diff --git a/XMultiply.vcxproj b/XMultiply.vcxproj index add4ae1..8cbc801 100644 --- a/XMultiply.vcxproj +++ b/XMultiply.vcxproj @@ -99,7 +99,7 @@ - + @@ -126,7 +126,7 @@ - + diff --git a/XMultiply.vcxproj.filters b/XMultiply.vcxproj.filters index df43920..8f234f0 100644 --- a/XMultiply.vcxproj.filters +++ b/XMultiply.vcxproj.filters @@ -73,7 +73,7 @@ Modules - + Modules\Player @@ -162,7 +162,7 @@ Modules - + Modules\Player