strength bar
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
+69
-11
@@ -1,4 +1,5 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
#include <math.h>
|
||||||
#include <SDL2/SDL_image.h>
|
#include <SDL2/SDL_image.h>
|
||||||
#include <SDL2/SDL_mixer.h>
|
#include <SDL2/SDL_mixer.h>
|
||||||
#include <SDL2/SDL_surface.h>
|
#include <SDL2/SDL_surface.h>
|
||||||
@@ -6,7 +7,7 @@
|
|||||||
#include "scene.h"
|
#include "scene.h"
|
||||||
#include "text.h"
|
#include "text.h"
|
||||||
|
|
||||||
#define SPEED 1.f/10000000000
|
#define SLIDER_SPEED 1.f/500
|
||||||
#define GRAVITY 10000.f
|
#define GRAVITY 10000.f
|
||||||
|
|
||||||
static Mix_Music * music;
|
static Mix_Music * music;
|
||||||
@@ -21,7 +22,8 @@ static const float startPersonPositionY = CAMERA_HEIGHT * 0.4f;
|
|||||||
static float personPositionX = startPersonPositionX;
|
static float personPositionX = startPersonPositionX;
|
||||||
static float personPositionY = startPersonPositionY;
|
static float personPositionY = startPersonPositionY;
|
||||||
|
|
||||||
static const float personVelocityX = 200;
|
static const float startPersonVelocityX = 200;
|
||||||
|
static float personVelocityX = 200;
|
||||||
static float personVelocityY = 0;
|
static float personVelocityY = 0;
|
||||||
|
|
||||||
float jetons = 0.f;
|
float jetons = 0.f;
|
||||||
@@ -31,6 +33,22 @@ static RenderId_t personId;
|
|||||||
RenderObject_t scoreText;
|
RenderObject_t scoreText;
|
||||||
static RenderId_t scoreTextId = -1;
|
static RenderId_t scoreTextId = -1;
|
||||||
|
|
||||||
|
static RenderObject_t strengthBar;
|
||||||
|
static RenderId_t strengthBarId;
|
||||||
|
static float strength = 0.f;
|
||||||
|
static bool increaseStrength = true;
|
||||||
|
|
||||||
|
|
||||||
|
static float adameStrength = 1.f; // this value is not const as it will be possible to improve the strenght of Adame.
|
||||||
|
|
||||||
|
static RenderObject_t strengthBarSlider;
|
||||||
|
static float strengthBarSliderDistance = 0.f;
|
||||||
|
static RenderId_t strengthBarSliderId;
|
||||||
|
static bool increaseStrengthBarSlider = true;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void playMusic(){
|
static void playMusic(){
|
||||||
music = Mix_LoadMUS("./assets/music/jojo arab.mp3");
|
music = Mix_LoadMUS("./assets/music/jojo arab.mp3");
|
||||||
if(music == NULL) {
|
if(music == NULL) {
|
||||||
@@ -91,20 +109,54 @@ int game_staticRender(void){
|
|||||||
scoreTextId = scene_addRenderObject(&gameScene, scoreText, gameScene.x + 2, 0);
|
scoreTextId = scene_addRenderObject(&gameScene, scoreText, gameScene.x + 2, 0);
|
||||||
|
|
||||||
|
|
||||||
|
SDL_Surface* strengthBarSurface = IMG_Load("./assets/img/strengthBar.png");
|
||||||
|
strcpy(strengthBar.name, "strengthBar");
|
||||||
|
strengthBar.z = 100;
|
||||||
|
strengthBar.surface = strengthBarSurface;
|
||||||
|
strengthBar.width = 0.50f;
|
||||||
|
strengthBar.height = 0.025f;
|
||||||
|
strengthBarId = scene_addRenderObject(&gameScene, strengthBar, 0.30f * CAMERA_WIDTH, 0.9f * CAMERA_HEIGHT);
|
||||||
|
|
||||||
|
|
||||||
|
SDL_Surface* strengthBarSliderSurface = IMG_Load("./assets/img/building.png");
|
||||||
|
strcpy(strengthBarSlider.name, "strengthBarSlider");
|
||||||
|
strengthBarSlider.z = 101;
|
||||||
|
strengthBarSlider.surface = strengthBarSliderSurface;
|
||||||
|
strengthBarSlider.width = 0.005f;
|
||||||
|
strengthBarSlider.height = 0.05f;
|
||||||
|
strengthBarSliderId = scene_addRenderObject(&gameScene, strengthBarSlider, 0.30f * CAMERA_WIDTH, 0.89f * CAMERA_HEIGHT);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int game_render([[maybe_unused]] float delta){
|
int game_render([[maybe_unused]] float delta){
|
||||||
//for(unsigned i = 0; i < menuObjectsSize; ++i){
|
|
||||||
// scene_removeRenderObject(&gameScene, menuObjects[i]);
|
|
||||||
//}
|
|
||||||
// if(scoreTextId > 0)
|
|
||||||
// scene_removeRenderObject(&gameScene, scoreTextId);
|
|
||||||
|
|
||||||
|
|
||||||
deltaAccu += delta;
|
deltaAccu += delta;
|
||||||
|
|
||||||
|
if(!isSpaceKeypressed && !crashed && deltaAccu >= SLIDER_SPEED){
|
||||||
|
// if(increaseStrength && strength >= 2.f)
|
||||||
|
// increaseStrength = false;
|
||||||
|
// else if(increaseStrength)
|
||||||
|
// strength += 0.1;
|
||||||
|
// else if(!increaseStrength && strength <= 0.f)
|
||||||
|
// increaseStrength = true;
|
||||||
|
// else
|
||||||
|
// strength -= 0.1;
|
||||||
|
|
||||||
|
if(increaseStrengthBarSlider && strengthBarSliderDistance >= 0.50f)
|
||||||
|
increaseStrengthBarSlider = false;
|
||||||
|
else if(increaseStrengthBarSlider){
|
||||||
|
strengthBarSliderDistance += 0.005f;
|
||||||
|
scene_moveRenderObject(&gameScene, strengthBarSliderId, 0.30f * CAMERA_WIDTH + strengthBarSliderDistance * CAMERA_WIDTH, 0.89f * CAMERA_HEIGHT);
|
||||||
|
}
|
||||||
|
else if(!increaseStrengthBarSlider && strengthBarSliderDistance <= 0.0001f)
|
||||||
|
increaseStrengthBarSlider = true;
|
||||||
|
else{
|
||||||
|
strengthBarSliderDistance -= 0.005f;
|
||||||
|
scene_moveRenderObject(&gameScene, strengthBarSliderId, 0.30f * CAMERA_WIDTH + strengthBarSliderDistance * CAMERA_WIDTH, 0.89f * CAMERA_HEIGHT);
|
||||||
|
}
|
||||||
|
|
||||||
|
deltaAccu = 0;
|
||||||
|
}
|
||||||
// if(!crashed && deltaAccu >= SPEED && isSpaceKeypressed){
|
// if(!crashed && deltaAccu >= SPEED && isSpaceKeypressed){
|
||||||
if(!crashed && isSpaceKeypressed){
|
if(!crashed && isSpaceKeypressed){
|
||||||
const float distance = personVelocityX * delta;
|
const float distance = personVelocityX * delta;
|
||||||
@@ -131,8 +183,6 @@ int game_render([[maybe_unused]] float delta){
|
|||||||
scene_moveRenderObject(&gameScene, personId, personPositionX, personPositionY);
|
scene_moveRenderObject(&gameScene, personId, personPositionX, personPositionY);
|
||||||
|
|
||||||
|
|
||||||
// scoreTextId = scene_addRenderObject(&gameScene, scoreText, gameScene.x + 2, 0);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,13 +197,21 @@ void game_handleSpaceKeyInput(void){
|
|||||||
crashed = false;
|
crashed = false;
|
||||||
personPositionX = startPersonPositionX;
|
personPositionX = startPersonPositionX;
|
||||||
personPositionY = startPersonPositionY;
|
personPositionY = startPersonPositionY;
|
||||||
|
personVelocityX = startPersonVelocityX;
|
||||||
personVelocityY = 0;
|
personVelocityY = 0;
|
||||||
isSpaceKeypressed = false;
|
isSpaceKeypressed = false;
|
||||||
gameScene.x = 0;
|
gameScene.x = 0;
|
||||||
gameScene.y = 0;
|
gameScene.y = 0;
|
||||||
|
strengthBarSliderDistance = 0.f;
|
||||||
scene_moveRenderObject(&gameScene, scoreTextId, gameScene.x + 2, 0);
|
scene_moveRenderObject(&gameScene, scoreTextId, gameScene.x + 2, 0);
|
||||||
|
scene_moveRenderObject(&gameScene, strengthBarId, 0.30f * CAMERA_WIDTH, 0.9f * CAMERA_HEIGHT);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
isSpaceKeypressed = true;
|
isSpaceKeypressed = true;
|
||||||
|
strength = - 32 * powf(fabs(strengthBarSliderDistance - 0.5f/2), 2) + 2;
|
||||||
|
personVelocityX *= strength * adameStrength;
|
||||||
|
scene_moveRenderObject(&gameScene, strengthBarId, 0, CAMERA_HEIGHT * 2);
|
||||||
|
scene_moveRenderObject(&gameScene, strengthBarSliderId, 0, CAMERA_HEIGHT * 2);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-3
@@ -8,7 +8,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
const RenderObject_t EMPTY_RENDER_OBJECT = { 0, 0, 0, 0, NULL, "Undefined" };
|
const RenderObject_t EMPTY_RENDER_OBJECT = { 0, 0.f, 0.f, 0.f, 0.f, 0.f, NULL, "Undefined" };
|
||||||
|
|
||||||
static int compartSort(const SceneObject_t * a, const SceneObject_t * b) {
|
static int compartSort(const SceneObject_t * a, const SceneObject_t * b) {
|
||||||
return a->renderObj.z - b->renderObj.z;
|
return a->renderObj.z - b->renderObj.z;
|
||||||
@@ -18,7 +18,7 @@ void scene_render(const Scene_t * scene) {
|
|||||||
MonitorSize_t monitorSize = renderer_getMonitorSize();
|
MonitorSize_t monitorSize = renderer_getMonitorSize();
|
||||||
|
|
||||||
for(int i = 0; i < scene->objectCount; i++) {
|
for(int i = 0; i < scene->objectCount; i++) {
|
||||||
printf("Rendering %d, %s\n", scene->objects[i].id, scene->objects[i].renderObj.name);
|
// printf("Rendering %d, %s\n", scene->objects[i].id, scene->objects[i].renderObj.name);
|
||||||
const SceneObject_t * object = &scene->objects[i];
|
const SceneObject_t * object = &scene->objects[i];
|
||||||
float x = (object->x - scene->x) / CAMERA_WIDTH;
|
float x = (object->x - scene->x) / CAMERA_WIDTH;
|
||||||
float y = (object->y - scene->y) / CAMERA_HEIGHT;
|
float y = (object->y - scene->y) / CAMERA_HEIGHT;
|
||||||
@@ -31,7 +31,17 @@ void scene_render(const Scene_t * scene) {
|
|||||||
round(object->renderObj.height * monitorSize.height)
|
round(object->renderObj.height * monitorSize.height)
|
||||||
};
|
};
|
||||||
|
|
||||||
int error = SDL_RenderCopyEx(renderer, object->cache, NULL, &rect, object->renderObj.angle_degre, NULL, SDL_FLIP_NONE);
|
int error;
|
||||||
|
|
||||||
|
if(object->renderObj.angle_x == FLT_MIN || object->renderObj.angle_y == FLT_MIN){
|
||||||
|
error = SDL_RenderCopyEx(renderer, object->cache, NULL, &rect, object->renderObj.angle_degre, NULL, SDL_FLIP_NONE);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
SDL_Point center;
|
||||||
|
center.x = object->renderObj.angle_x * rect.w;
|
||||||
|
center.x = object->renderObj.angle_y * rect.h;
|
||||||
|
error = SDL_RenderCopyEx(renderer, object->cache, NULL, &rect, object->renderObj.angle_degre, ¢er, SDL_FLIP_NONE);
|
||||||
|
}
|
||||||
if(error != 0) {
|
if(error != 0) {
|
||||||
fprintf(stderr, "Render error in object %d with error %s\n", object->id, SDL_GetError());
|
fprintf(stderr, "Render error in object %d with error %s\n", object->id, SDL_GetError());
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ typedef struct RenderObject {
|
|||||||
int z;
|
int z;
|
||||||
float width, height; //ratio between 0 & 1 of the monitor resolution
|
float width, height; //ratio between 0 & 1 of the monitor resolution
|
||||||
float angle_degre;
|
float angle_degre;
|
||||||
|
float angle_x, angle_y; // position for the center of the rotation
|
||||||
SDL_Surface * surface;
|
SDL_Surface * surface;
|
||||||
char name[40];
|
char name[40];
|
||||||
} RenderObject_t;
|
} RenderObject_t;
|
||||||
|
|||||||
Reference in New Issue
Block a user