NEWS: Welcome to my new homepage! <3

effect.c - freezo - A retro platform game

freezo

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

effect.c (1846B)


      1 #include <stdlib.h>
      2 
      3 #include "effect.h"
      4 #include "util.h"
      5 #include "const.h"
      6 
      7 effect_t *effect_create(pos_t pos, effect_e type) {
      8   effect_t *effect = malloc(sizeof(effect_t));
      9   effect->type = type;
     10   effect->pos = pos;
     11   switch (type) {
     12     case EFFECT_FALL:
     13       effect->timer = timer_create(7, 3);
     14       break;
     15     case EFFECT_BREAK:
     16       effect->timer = timer_create(10, 3);
     17       break;
     18     case EFFECT_PARTICLE:
     19       effect->timer = timer_create(14, 4);
     20       break;
     21   }
     22   effect->count = 0;
     23   return effect;
     24 }
     25 
     26 void effect_update(effect_t *effect, game_t *game) {
     27   timer_update(effect->timer);
     28   if (timer_check(effect->timer)) {
     29     effect->count++;
     30   }
     31   // remove effect after one iteration
     32   if (effect->count > 0) {
     33     for (int i = 0; i < game->effects_len; i++) {
     34       if (game->effects[i] == effect) {
     35         if (i + 1 < game->effects_len) {
     36           for (int j = i; j < game->effects_len - 1; j++) {
     37             game->effects[j] = game->effects[j + 1];
     38           }
     39         }
     40         game->effects_len--;
     41         game->effects = realloc(game->effects, sizeof(effect_t) * game->effects_len);
     42         break;
     43       }
     44     }
     45   }
     46 }
     47 
     48 void effect_draw(effect_t *effect, game_t *game) {
     49   int x;
     50   switch (effect->type) {
     51     case EFFECT_FALL:
     52       x = 1;
     53       break;
     54     case EFFECT_BREAK:
     55       x = 0;
     56       break;
     57     case EFFECT_PARTICLE:
     58       x = 3;
     59       break;
     60   }
     61   int frame = timer_get(effect->timer);
     62   DrawTextureRec(game->assets.entities, texture_rect(frame, x, PLAYER_WIDTH, PLAYER_HEIGHT), pos_snap(effect->pos), WHITE);
     63 }
     64 
     65 void effect_free(effect_t *effect) {
     66   free(effect);
     67 }
     68 
     69 void effect_play(pos_t pos, effect_e type, game_t *game) {
     70   game->effects = realloc(game->effects, sizeof(effect_t) * (game->effects_len + 1));
     71   game->effects[game->effects_len] = effect_create(pos, type);
     72   game->effects_len++;
     73 }