[ADD] simple enemy collision
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -95,4 +95,5 @@ cybershot.velf
|
||||
cybershot.vpk
|
||||
cybershot.vpk_param.sfo
|
||||
|
||||
commit.sh
|
||||
commit.sh
|
||||
push
|
||||
|
||||
103
src/main.c
103
src/main.c
@@ -19,8 +19,10 @@
|
||||
|
||||
#define SCREEN_HEIGTH 544
|
||||
#define SCREEN_WIDTH 940
|
||||
#define SIMPLE_ENEMY_MAX_AMOUNT 20
|
||||
#define BULLET_MARGIN 5.0 // extra hitbox space to make sure bullets hit
|
||||
|
||||
size_t running = 1, drawing_circle = 0;
|
||||
uint8_t running = 1, drawing_circle = 0;
|
||||
|
||||
stick_data left_stick = {0, 0}, right_stick = {0, 0};
|
||||
SceCtrlData pad;
|
||||
@@ -40,7 +42,9 @@ SceKernelSysClock sysclock;
|
||||
timing_timer bullet_timer = {0, 250, 0}; // 0 as starting time, 400 ms timeout, not elapsed
|
||||
|
||||
timer_t bullt = 0;
|
||||
ENEMY_SPRITE simple_enemies[SIMPLE_ENEMY_MAX_AMOUNT];
|
||||
|
||||
ENEMY_SPRITE enemies[SIMPLE_ENEMY_MAX_AMOUNT];
|
||||
uint32_t enemy_count = 0;
|
||||
|
||||
float player_x = 300, player_y = 50, x2_pos = 400, y2_pos = 50, radius = 5.0;
|
||||
|
||||
@@ -56,6 +60,8 @@ __attribute__((__noreturn__)) void shit_yourself(void)
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------ GENERATE SPRITES ------------------
|
||||
|
||||
void generate_bullet()
|
||||
{
|
||||
// {1, x1_pos, y1_pos, RGBA8(100, 100, 0, 255)};
|
||||
@@ -76,6 +82,77 @@ void generate_smoke_particle()
|
||||
current_smoke_particle = (current_smoke_particle + 1) % 254;
|
||||
}
|
||||
|
||||
// ------------------------ END GENERATE SPRITES ------------------
|
||||
|
||||
// ------------------------ COLLISION ------------------
|
||||
|
||||
/**
|
||||
* @brief checks if a bullet has hit the given object
|
||||
*
|
||||
* @param bullet the bullet to check for
|
||||
* @param x1 the x pos of the center of the other object
|
||||
* @param x2 the y pos of the center of the other object
|
||||
* @param width the width of the other object
|
||||
* @param heigth the heigth of the other object
|
||||
* @return uint8_t 0 if no collision, 1 if there is a collision.
|
||||
*/
|
||||
uint8_t bullet_is_collision(BULLET *bullet, float x, float y, float width, float heigth)
|
||||
{
|
||||
return toolbox_is_collision(bullet->x + BULLET_WIDTH / 2.0, bullet->y + BULLET_HEIGTH / 2.0, BULLET_WIDTH + BULLET_MARGIN, BULLET_HEIGTH, x, y, width, heigth);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief checks wether a bullet has hit an enemy
|
||||
*
|
||||
* @param bullet the bulle to check for
|
||||
* @param enemy the enemy to check for
|
||||
* @return uint8_t 0 if no collision, 1 if there is a collision
|
||||
*/
|
||||
uint8_t bullet_hit_enemy(BULLET *bullet, ENEMY_SPRITE *enemy)
|
||||
{
|
||||
if (enemy->enemy_type == SIMPLE)
|
||||
{
|
||||
return bullet_is_collision(bullet, enemy->x, enemy->y, SIMPLE_ENEMY_SIZE, SIMPLE_ENEMY_SIZE);
|
||||
} else if (enemy->enemy_type == COMPLEX)
|
||||
{
|
||||
return bullet_is_collision(bullet, enemy->x, enemy->y, COMPLEX_ENEMY_SIZE, COMPLEX_ENEMY_SIZE);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief checks the collision for all bullets, also checks for:
|
||||
* - enemies
|
||||
*
|
||||
*/
|
||||
void check_bullet_collisions()
|
||||
{
|
||||
for (int e = 0; e < enemy_count; e++)
|
||||
{
|
||||
if (enemies[e].active == ACTIVE)
|
||||
{
|
||||
if (enemies[e].enemy_type == SIMPLE)
|
||||
{
|
||||
for (int b = 0; b < 255; b++)
|
||||
{
|
||||
if (bullets[b].active == ACTIVE)
|
||||
{
|
||||
if (bullet_hit_enemy(&bullets[b],&enemies[e]))
|
||||
{
|
||||
bullets[b].active = NONACTIVE;
|
||||
enemies[e].active = NONACTIVE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------ END COLLISION ------------------
|
||||
|
||||
void init()
|
||||
{
|
||||
/* to enable analog sampling */
|
||||
@@ -101,11 +178,15 @@ void init()
|
||||
smoke_particles[i] = s;
|
||||
}
|
||||
|
||||
// add simple enemies
|
||||
for (i = 0; i < SIMPLE_ENEMY_MAX_AMOUNT; i++)
|
||||
{
|
||||
ENEMY_SPRITE e = {ACTIVE, SIMPLE, 20 * i + 10, 10, RGBA8(245, 90, 66, 255), 1.0};
|
||||
simple_enemies[i] = e;
|
||||
enemies[i] = e;
|
||||
enemy_count++;
|
||||
}
|
||||
|
||||
//TODO add other enemies
|
||||
}
|
||||
|
||||
void update()
|
||||
@@ -152,6 +233,8 @@ void update()
|
||||
}
|
||||
}
|
||||
|
||||
check_bullet_collisions();
|
||||
|
||||
for (int i = 0; i < 255; i++)
|
||||
{
|
||||
bullets[i].y -= bullets[i].movement_speed * (deltaTime / 1000.0);
|
||||
@@ -179,15 +262,7 @@ void draw()
|
||||
sprites_draw_player(player_x, player_y, PLAYER_SCALE);
|
||||
// vita2d_draw_rectangle(x2_pos, y2_pos, (float)10, (float)10, RGBA8(169, 60, 23, 255));
|
||||
|
||||
vita2d_draw_rectangle(300, 50, 30, 30, RGBA8(255, 0, 255, 255));
|
||||
|
||||
size_t collision_player = toolbox_is_collision(player_x, player_y - (PLAYER_SCALE * SHIP_HEIGHT)/2,SHIP_WIDTH * PLAYER_SCALE, SHIP_HEIGHT * PLAYER_SCALE, 300+15,50+15,30,30);
|
||||
if (collision_player)
|
||||
{
|
||||
vita2d_pgf_draw_text(pgf, 700, 30, RGBA8(0, 255, 0, 255), 1.0f, "collision");
|
||||
} else {
|
||||
vita2d_pgf_draw_text(pgf, 700, 30, RGBA8(0, 255, 0, 255), 1.0f, "no col");
|
||||
}
|
||||
// vita2d_draw_rectangle(300, 50, 30, 30, RGBA8(255, 0, 255, 255));
|
||||
|
||||
char fps[15] = "fps: ";
|
||||
sprintf(fps, "%d", timing_get_fps(deltaTime));
|
||||
@@ -204,9 +279,9 @@ void draw()
|
||||
sprites_draw_smoke_circle(&smoke_particles[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < SIMPLE_ENEMY_MAX_AMOUNT; i++)
|
||||
for (int i = 0; i < enemy_count; i++)
|
||||
{
|
||||
sprites_draw_enemy(&simple_enemies[i]);
|
||||
sprites_draw_enemy(&enemies[i]);
|
||||
}
|
||||
|
||||
vita2d_end_drawing();
|
||||
|
||||
@@ -52,10 +52,10 @@ void sprites_draw_enemy(ENEMY_SPRITE *enemy)
|
||||
|
||||
void sprites_draw_simple_enemy(ENEMY_SPRITE *enemy)
|
||||
{
|
||||
vita2d_draw_line(enemy->x - 9.0, enemy->y, enemy->x, enemy->y + 9.0, enemy->color);
|
||||
vita2d_draw_line(enemy->x, enemy->y + 9.0, enemy->x + 9.0, enemy->y, enemy->color);
|
||||
vita2d_draw_line(enemy->x + 9.0, enemy->y, enemy->x, enemy->y - 9.0, enemy->color);
|
||||
vita2d_draw_line(enemy->x, enemy->y - 9.0, enemy->x - 9.0, enemy->y, enemy->color);
|
||||
vita2d_draw_line(enemy->x - SIMPLE_ENEMY_SIZE, enemy->y, enemy->x, enemy->y + SIMPLE_ENEMY_SIZE, enemy->color);
|
||||
vita2d_draw_line(enemy->x, enemy->y + SIMPLE_ENEMY_SIZE, enemy->x + SIMPLE_ENEMY_SIZE, enemy->y, enemy->color);
|
||||
vita2d_draw_line(enemy->x + SIMPLE_ENEMY_SIZE, enemy->y, enemy->x, enemy->y - SIMPLE_ENEMY_SIZE, enemy->color);
|
||||
vita2d_draw_line(enemy->x, enemy->y - SIMPLE_ENEMY_SIZE, enemy->x - SIMPLE_ENEMY_SIZE, enemy->y, enemy->color);
|
||||
}
|
||||
|
||||
void sprites_draw_complex_enemy(ENEMY_SPRITE *enemy)
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
#define SHIP_SECONDARY_COLOR RGBA8(153, 153, 153, 255)
|
||||
#define SHIP_PRIMARY_COLOR RGBA8(0, 255, 195, 255)
|
||||
|
||||
#define SHIP_WIDTH 4
|
||||
#define SHIP_WIDTH 4
|
||||
#define SHIP_HEIGHT 5
|
||||
|
||||
#define BULLET_WIDTH 3
|
||||
@@ -21,16 +21,17 @@
|
||||
|
||||
#define PLAYER_SCALE 3.0
|
||||
|
||||
#define SIMPLE_ENEMY_SIZE 9.0
|
||||
#define COMPLEX_ENEMY_SIZE 9.0
|
||||
|
||||
#define SMOKE_START_RADIUS 8.0
|
||||
#define SMOKE_MAX_RADIUS 10.0
|
||||
#define SMOKE_DISSAPPEAR_SPEED 35.0
|
||||
|
||||
#define SIMPLE_ENEMY_MAX_AMOUNT 10
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SIMPLE,
|
||||
COMPLEX
|
||||
COMPLEX
|
||||
} ENEMY_TYPE;
|
||||
|
||||
typedef enum
|
||||
@@ -45,28 +46,28 @@ typedef enum
|
||||
*/
|
||||
typedef struct sprite_t
|
||||
{
|
||||
SPRITE_ACTIVE active; // whether or not the sprite should be drawn (0 or 1)
|
||||
float x; // the x position
|
||||
float y; // the y position
|
||||
SPRITE_ACTIVE active; // whether or not the sprite should be drawn (0 or 1)
|
||||
float x; // the top left x position
|
||||
float y; // the top left y position
|
||||
unsigned int color; // color of the sprite
|
||||
float movement_speed; // speed of the sprite (how much it should move each frame)
|
||||
} BULLET;
|
||||
|
||||
typedef struct enemy_sprite_t
|
||||
{
|
||||
SPRITE_ACTIVE active; // whether or not the sprite should be drawn (0 or 1)
|
||||
ENEMY_TYPE enemy_type; // type of enemy
|
||||
float x; // the x position
|
||||
float y; // the y position
|
||||
unsigned int color; // color of the sprite
|
||||
float movement_speed; // speed of the sprite (how much it should move each frame)
|
||||
SPRITE_ACTIVE active; // whether or not the sprite should be drawn (0 or 1)
|
||||
ENEMY_TYPE enemy_type; // type of enemy
|
||||
float x; // the center x position
|
||||
float y; // the center y position
|
||||
unsigned int color; // color of the sprite
|
||||
float movement_speed; // speed of the sprite (how much it should move each frame)
|
||||
} ENEMY_SPRITE;
|
||||
|
||||
typedef struct smoke_particle_t
|
||||
{
|
||||
SPRITE_ACTIVE active; // whether or not to draw the smoke circle (0 or 1)
|
||||
float x; // x position
|
||||
float y; // y position
|
||||
SPRITE_ACTIVE active; // whether or not to draw the smoke circle (0 or 1)
|
||||
float x; // center x position
|
||||
float y; // center y position
|
||||
float radius; // radius of circle
|
||||
int8_t explosion_direction; // wether the explosion is expanding or shrinking.
|
||||
// 1 = expanding, -1 = shrinking
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "toolbox.h"
|
||||
|
||||
|
||||
float toolbox_random_float(float a, float b)
|
||||
{
|
||||
float random = ((float)rand()) / (float)RAND_MAX;
|
||||
@@ -8,11 +9,11 @@ float toolbox_random_float(float a, float b)
|
||||
return a + r;
|
||||
}
|
||||
|
||||
size_t toolbox_is_collision(float x1, float y1, float width1, float heigth1, float x2, float y2, float width2, float height2)
|
||||
uint8_t toolbox_is_collision(float x1, float y1, float width1, float heigth1, float x2, float y2, float width2, float height2)
|
||||
{
|
||||
//TODO make it work
|
||||
return x1 < x2 + width2 / 2 &&
|
||||
x1 + width1 / 2 > x2 &&
|
||||
y1 < y2 + height2 / 2 &&
|
||||
y1 + heigth1 / 2 > y2;
|
||||
//TODO make it work?
|
||||
return x1 - width1 / 2 < x2 + width2 / 2 &&
|
||||
x1 + width1 / 2 > x2 - width2 / 2 &&
|
||||
y1 - heigth1 / 2 < y2 + height2 / 2 &&
|
||||
y1 + heigth1 / 2 > y2 - height2 / 2;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef TOOLBOX_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "../sprites/sprites.h"
|
||||
|
||||
/**
|
||||
* @brief generates a random float between the given floats
|
||||
@@ -21,8 +22,8 @@ float toolbox_random_float(float a, float b);
|
||||
* @param y2 the center y of the second object
|
||||
* @param width2 the width of the second object
|
||||
* @param height2 the heigth of the second object
|
||||
* @return size_t 0 if no collision, 1 if there is a collision.
|
||||
* @return uint8_t 0 if no collision, 1 if there is a collision.
|
||||
*/
|
||||
size_t toolbox_is_collision(float x1, float y1, float width1, float heigth1, float x2, float y2, float width2, float height2);
|
||||
uint8_t toolbox_is_collision(float x1, float y1, float width1, float heigth1, float x2, float y2, float width2, float height2);
|
||||
|
||||
#endif // !TOOLBOX_H
|
||||
Reference in New Issue
Block a user