aboutsummaryrefslogtreecommitdiff
path: root/bullet.c
blob: 7885862a8aea6ccf19207e4d085936cd6d5b1f1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "game.h"
#include <time.h>

static const float BULLET_SPEED = 600.0f;

static void destroy_bullet(unsigned char index)
{
  struct Bullet *b =  game->bullets[index];
  
  free(b);
  game->bullets[index] = NULL;

  game->bullets_left ++;
}

static void bullet_on_collision(struct Entity *this, struct Entity *other)
{
  int i, j;
  // check if it is the enemy player
  // TODO: Ask if this is good practice
  struct Bullet *b = (struct Bullet *)this - offsetof(struct Bullet, entity); 
  
  if((void *)b->owner == (void *)other)
    return;

  if(!this->collidable && !other->collidable)
    return;
  
  for(i = 0; i < game->num_entities; i ++)
    {
      if(game->entities[i] == this)
	{
	  for(j = i; j < game->num_entities - 1; j ++)
	    game->entities[j] = game->entities[j + 1];
	  //game->entities[i] = NULL;
	}
    }
  game->num_entities --;
  destroy_bullet(b->index);
}

void shoot_bullet(struct Player *owner)
{
  int i, j;
  float speed, angle;
  struct Bullet *b;
  struct Gun *g = &owner->guns[owner->current_gun];
  
  if(game->bullets_left < g->projectiles_per_shot
     || !g->ready || !g->bullets || owner->reloading)
    return;

  for(i = 0; i < g->projectiles_per_shot; i ++)
    {
      b = malloc( sizeof(struct Bullet) );
      b->owner = owner;
      b->entity.on_collision = bullet_on_collision;
      b->entity.collidable = false;
      b->entity.use_src_rect = false;
      b->entity.texture = game->bullet_texture;
      b->entity.x = owner->entity.x;
      b->entity.y = owner->entity.y + 10;
      b->entity.w = BULLET_WIDTH;
      b->entity.h = BULLET_WIDTH;
      b->damage = g->damage;
      b->entity.name = "bullet";
      angle = g->angle * ((float)rand() / (float)RAND_MAX) - g->angle / 2.0f;
      speed = (owner->entity.mirror ? -BULLET_SPEED : BULLET_SPEED);
  
      b->entity.dx = speed;
      b->entity.dy = SDL_sinf(angle) * 100.0f;

      for(j = 0; j < MAX_BULLETS; j ++)
	{
	  if(!game->bullets[j])
	    {
	      game->bullets[j] = b;
	      b->index = j;
	      break;
	    }
	}

      game->bullets_left --;
    }
  
  g->bullets --;
  g->ready = false;
  g->cooldown_elapsed = 0.0f;

  Mix_PlayChannel(-1, g->sound_effect, 0);
}

void update_bullets(void)
{
  int i;
  struct Bullet *b;
  
  for(i = 0; i < MAX_BULLETS; i ++)
    {
      b = game->bullets[i];
      
      if(b)
	{
	  b->entity.x += b->entity.dx * game->frame_time;
	  b->entity.y += b->entity.dy * game->frame_time;

	  if(b->entity.x > (float)game->width || b->entity.x < 0.0f
	     || b->entity.y > (float)game->height || b->entity.y < 0.0f)
	    {
	      destroy_bullet(b->index);
	      continue;
	    }

	  game->entities[game->num_entities ++] = &b->entity;
	}
    }
}