#include "animation.h" #include "bullet.h" #include "components.h" #include "fx.h" #include "game.h" #include "game_config.h" #include "input.h" #include "player.h" #include "standard.h" #include "world.h" extern Game game; void init_player(Player* player, World* world) { CSprite* sprite; CPosition* pos; CAnimated* animated; CCollider* col; Entity e; e = new_entity(world); player->entity = e; add_components( world, e, ctype_sprite | ctype_position | ctype_animated | ctype_collider | ctype_player | ctype_moveable ); pos = &world->positions[e]; sprite = &world->sprites[e]; animated = &world->animateds[e]; col = &world->colliders[e]; pos->x = 32 << fbits; pos->y = 70 << fbits; sprite->id = asset_id_char; sprite->rect = make_rect(0, 16, 16, 16); animated->id = animation_player_walk_left; animated->frame = 0; animated->timer = 0; col->x = 3 << fbits; col->y = 1 << fbits; col->w = 10 << fbits; col->h = 15 << fbits; player->face = 0; player->shoot_cooldown = 15; player->shoot_countdown = 0; player->hp = player_max_hp; } void update_player(Player* player, World* world) { int dx, dy; int face, moving = 0; Entity e; CPosition* pos; CAnimated* animated; e = player->entity; pos = &world->positions[e]; animated = &world->animateds[e]; dx = dy = 0; if (button_pressed(btn_dpad_left)) { dx -= 1 << fbits; } if (button_pressed(btn_dpad_right)) { dx += 1 << fbits; } if (button_pressed(btn_dpad_up)) { dy -= 1 << fbits; } if (button_pressed(btn_dpad_down)) { dy += 1 << fbits; } if (dx || dy) { vec_nrmise(&dx, &dy); if (dx && !button_pressed(btn_shoot)) { face = dx < 0 ? 0 : 1; } else { face = player->face; } pos->x += (dx * player_move_speed) >> fbits; pos->y += (dy * player_move_speed) >> fbits; player->ldx = dx; player->ldy = dy; moving = 1; } if (!moving) { animated->frame = 0; animated->id = player->face ? animation_player_idle_right : animation_player_idle_left; } else { player->face = face; animated->id = player->face ? animation_player_walk_right : animation_player_walk_left; } if (!button_pressed(btn_shoot)) { player->sdx = player->ldx; player->sdy = player->ldy; } if (button_pressed(btn_shoot) && player->shoot_countdown <= 0) { new_player_bullet( world, pos->x, pos->y, (player->sdx * player_bullet_speed) >> fbits, (player->sdy * player_bullet_speed) >> fbits, 100 ); player->shoot_countdown = player->shoot_cooldown; } if (player->invul) { player->invul--; player->invul_counter++; if (player->invul_counter > 3) { if (world->bitmask[e] & ctype_sprite) { world->bitmask[e] &= ~ctype_sprite; } else { world->bitmask[e] |= ctype_sprite; } player->invul_counter = 0; } } else { world->bitmask[e] |= ctype_sprite; } if (player->hp <= 0) { world->oom = 0; game_change_state(&game, game_state_dead); } player->shoot_countdown--; } void player_take_damage( World* world, Player* player, int dmg ) { const CPosition* pos; pos = &world->positions[player->entity]; if (player->invul) { return; } player->hp -= dmg; player->invul = player_invul_frames; player->invul_counter = 0; new_heart_break( world, pos->x + (4 << fbits), pos->y - (8 << fbits) ); } void update_camera(Player* player, World* world) { const CPosition* pos; int cbx, cby; pos = &world->positions[player->entity]; world->cam_x = pos->x - ((renderer_w / 2) << fbits); world->cam_y = pos->y - ((renderer_h / 2) << fbits); if (world->cam_x < 0) { world->cam_x = 0; } if (world->cam_y < 0) { world->cam_y = 0; } cbx = (mbmp_w - renderer_w) << fbits; if (world->cam_x > cbx) { world->cam_x = cbx; } cby = (mbmp_h - renderer_h) << fbits; if (world->cam_y > cby) { world->cam_y = cby; } }