NEWS: Welcome to my new homepage! <3

gate.c - freezo - A retro platform game

freezo

A retro platform game
git clone git://192.168.2.2/freezo
Log | Files | Refs | README

gate.c (2977B)


      1 #include <stdlib.h>
      2 
      3 #include "entity.h"
      4 #include "gate.h"
      5 #include "const.h"
      6 
      7 gate_t *gate_create(pos_t pos, int enemy_max, int time) {
      8   gate_t *gate = malloc(sizeof(gate_t));
      9   gate->pos = pos;
     10   gate->enemy_max = enemy_max;
     11   gate->timer_spawn = timer_create(1, time);
     12   gate->frozen = false;
     13   gate->frozen_timer = 0;
     14   return gate;
     15 }
     16 
     17 void gate_update(gate_t *gate, game_t *game) {
     18   if (!gate->frozen) {
     19     bool stunned = false;
     20     if (
     21       game->player->shooting &&
     22       game->player->target_entity != NULL &&
     23       game->player->target_entity->type == ENTITY_GATE &&
     24       game->player->target_entity->gate == gate
     25     ) {
     26       if (gate->frozen) {
     27         if (game->player->dir > 0) {
     28           gate->pos.x += 2.0;
     29         }
     30         else {
     31           gate->pos.x -= 2.0;
     32         }
     33       }
     34       else {
     35         stunned = true;
     36       }
     37     }
     38     // handle freezing
     39     if (stunned) {
     40       if (gate->frozen_timer >= 300) {
     41         gate->frozen = true;
     42         gate->frozen_timer = 0;
     43       }
     44       gate->frozen_timer++;
     45     }
     46     else if (gate->frozen_timer > 0) {
     47       gate->frozen_timer--;
     48     }
     49     // handle spawning
     50     timer_update(gate->timer_spawn);
     51     int enemies_len = 0;
     52     for (int i = 0; i < game->entities_len; i++) {
     53       if(game->entities[i].type == ENTITY_ENEMY) {
     54         enemies_len++;
     55       }
     56     }
     57     if (timer_check(gate->timer_spawn) && enemies_len < gate->enemy_max) {
     58       game->entities = realloc(game->entities, sizeof(entity_t) * (game->entities_len + 1));
     59       enemy_t *enemy = enemy_create((pos_t) {
     60         .x = gate->pos.x + (TILE_WIDTH - ENEMY_WIDTH) / 2.0,
     61         .y = gate->pos.y + TILE_HEIGHT - ENEMY_HEIGHT,
     62       }, ENEMY_TEST);
     63       enemy->dir = (rand() & 1) ? 1 : -1;
     64       game->entities[game->entities_len] = (entity_t) {
     65         .type = ENTITY_ENEMY,
     66         .enemy = enemy,
     67       };
     68       game->entities_len++;
     69     }
     70   }
     71 }
     72 
     73 void gate_draw(gate_t *gate, game_t *game) {
     74   DrawTextureRec(game->assets.tiles, texture_rect(0, 2, TILE_WIDTH, TILE_HEIGHT), (pos_t) { gate->pos.x, gate->pos.y - TILE_HEIGHT }, WHITE);
     75   DrawTextureRec(game->assets.tiles, texture_rect(0, 3, TILE_WIDTH, TILE_HEIGHT), gate->pos, WHITE);
     76   pos_t pos = pos_snap((pos_t) {
     77     .x = gate->pos.x,
     78     .y = gate->pos.y - (ENEMY_HEIGHT - TILE_HEIGHT),
     79   });
     80   if (gate->frozen) {
     81     DrawTextureRec(game->assets.entities, texture_rect(3, 2, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);
     82   }
     83   if (!gate->frozen && gate->frozen_timer > 0) {
     84     if (gate->frozen_timer <= 100) {
     85       DrawTextureRec(game->assets.entities, texture_rect(0, 2, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);
     86     }
     87     else if (gate->frozen_timer <= 200) {
     88       DrawTextureRec(game->assets.entities, texture_rect(1, 2, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);
     89     }
     90     else {
     91       DrawTextureRec(game->assets.entities, texture_rect(2, 2, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);
     92     }
     93   }
     94 }
     95 
     96 void gate_free(gate_t *gate) {
     97   free(gate->timer_spawn);
     98   free(gate);
     99 }