227 lines
6.7 KiB
C
227 lines
6.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
#include <math.h>
|
|
#include <stdbool.h>
|
|
#include <ncurses.h>
|
|
#include <unistd.h>
|
|
#include <termios.h>
|
|
#include <limits.h>
|
|
#include <time.h>
|
|
#include <SDL2/SDL.h>
|
|
#include <SDL2/SDL_image.h>
|
|
|
|
#include "hash.h"
|
|
#include "structure.h"
|
|
#include "base.h"
|
|
#include "entities.h"
|
|
#include "move.h"
|
|
|
|
// ---------------------------------------------------------------------------------------------------- //
|
|
double sensitivity = 0.12 ;
|
|
double fov = 90.0 ;
|
|
double speed = 0.22 ;
|
|
double min_dist = 0.7 ;
|
|
// ---------------------------------------------------------------------------------------------------- //
|
|
|
|
int player_hp ;
|
|
|
|
bool stop_evetything;
|
|
|
|
double camx ;
|
|
double camy ;
|
|
double camz ;
|
|
|
|
double rot_hz ;
|
|
double rot_vt ;
|
|
|
|
double tan_fov ;
|
|
|
|
int draw_type ;
|
|
int fade_dmg ;
|
|
|
|
bool has_changed ;
|
|
|
|
void init_csts() {
|
|
camx = 3.0 ;
|
|
camy = 3.0 ;
|
|
camz = 3.0 ;
|
|
rot_hz = 0.0 ;
|
|
rot_vt = 180.0*3.14159/180.0 ;
|
|
draw_type = 0 ;
|
|
player_hp = 1000 ;
|
|
fade_dmg = 0;
|
|
stop_evetything = false ;
|
|
tan_fov = tan((fov * 3.14159 / 180.0) / 2.0) ;
|
|
}
|
|
|
|
void set_player_coords(int old_chx, int old_chy) {
|
|
for(int k = 0; k < current_room->tps_size; k++) {
|
|
if(current_room->tps[k].dest_chx == old_chx && current_room->tps[k].dest_chy == old_chy) {
|
|
if(true) {
|
|
camx = current_room->tps[k].hitbox->x + current_room->tps[k].hitbox->w/2.0;
|
|
camy = current_room->tps[k].hitbox->y + current_room->tps[k].hitbox->h +1.5;
|
|
camz = current_room->tps[k].hitbox->z + current_room->tps[k].hitbox->d/2.0;
|
|
}
|
|
return ;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool is_colliding(float dtime) {
|
|
for(int k = 0; k < current_room->map_size; k++) {
|
|
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->map[k]) ;
|
|
if(dist <= min_dist) {
|
|
return true ;
|
|
}
|
|
}
|
|
for(int k = 0; k < current_room->tps_size; k++) {
|
|
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->tps[k].hitbox) ;
|
|
if(dist <= min_dist) {
|
|
int old_chx = player_chx ;
|
|
int old_chy = player_chy ;
|
|
player_chx = current_room->tps[k].dest_chx ;
|
|
player_chy = current_room->tps[k].dest_chy ;
|
|
current_room = hashtbl_find_opt(visited, player_chx, player_chy);
|
|
set_player_coords(old_chx, old_chy);
|
|
return true ;
|
|
}
|
|
}
|
|
for(int k = 0; k < current_room->ent_len; k++) {
|
|
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->ents[k].pos) ;
|
|
if(dist <= min_dist) {
|
|
if(current_room->ents[k].onHit != NULL) {
|
|
(*current_room->ents[k].onHit)(dtime, current_room->ents[k].hitpoints, ¤t_room->ents[k].damage, &(*(current_room->ents[k].pos)));
|
|
if(*(current_room->ents[k].hitpoints) <= 0) {
|
|
if(current_room->ents[k].onDeath != NULL) {
|
|
(*current_room->ents[k].onDeath)(dtime);
|
|
}
|
|
remove_entity(¤t_room->ents, ¤t_room->ent_memlen, ¤t_room->ent_len, k);
|
|
}
|
|
}
|
|
return true ;
|
|
}
|
|
}
|
|
return false ;
|
|
}
|
|
|
|
bool pass = true ;
|
|
void playerActions(float dtime) {
|
|
SDL_Event event;
|
|
while(SDL_PollEvent(&event)) {
|
|
switch (event.type) {
|
|
case SDL_QUIT:
|
|
break;
|
|
case SDL_MOUSEMOTION:
|
|
has_changed = true ;
|
|
rot_hz += sensitivity * event.motion.xrel / 100.0 ;
|
|
rot_vt -= sensitivity * event.motion.yrel / 100.0 ;
|
|
if(rot_hz >= 2*3.141592) {
|
|
rot_hz -= 2*3.141592 ;
|
|
} else if(rot_hz < 0.0) {
|
|
rot_hz += 2*3.141592 ;
|
|
}
|
|
if(rot_vt <= 3.14159/2.0) {
|
|
rot_vt = 3.14159/2.0;
|
|
} else if(rot_vt >= 3.0*3.14159/2.0) {
|
|
rot_vt = 3.0*3.14159/2.0 ;
|
|
}
|
|
break;
|
|
|
|
}
|
|
}
|
|
const Uint8* state = SDL_GetKeyboardState(NULL);
|
|
if(state[SDL_SCANCODE_Z] == 1) {
|
|
for(int k = 0; k < 10; k++) {
|
|
camz += speed*cos(rot_hz)*cos(rot_vt)/10;
|
|
camx -= speed*sin(rot_hz)*cos(rot_vt)/10;
|
|
camy += speed*sin(rot_vt)/10;
|
|
if(is_colliding(dtime)) {
|
|
camz -= speed*cos(rot_hz)*cos(rot_vt)/10;
|
|
camx += speed*sin(rot_hz)*cos(rot_vt)/10;
|
|
camy -= speed*sin(rot_vt)/10;
|
|
k=11;
|
|
}
|
|
}
|
|
}
|
|
if(state[SDL_SCANCODE_Q] == 1) {
|
|
for(int k = 0; k < 10; k++) {
|
|
camx -= speed*cos(rot_hz)/10;
|
|
camz -= speed*sin(rot_hz)/10;
|
|
if(is_colliding(dtime)) {
|
|
camx += speed*cos(rot_hz)/10;
|
|
camz += speed*sin(rot_hz)/10;
|
|
k=11;
|
|
}
|
|
}
|
|
}
|
|
if(state[SDL_SCANCODE_S] == 1) {
|
|
for(int k = 0; k < 10; k++) {
|
|
camz -= speed*cos(rot_hz)*cos(rot_vt)/10;
|
|
camx += speed*sin(rot_hz)*cos(rot_vt)/10;
|
|
camy -= speed*sin(rot_vt)/10;
|
|
if(is_colliding(dtime)) {
|
|
camz += speed*cos(rot_hz)*cos(rot_vt)/10;
|
|
camx -= speed*sin(rot_hz)*cos(rot_vt)/10;
|
|
camy += speed*sin(rot_vt)/10;
|
|
k=11;
|
|
}
|
|
}
|
|
}
|
|
if(state[SDL_SCANCODE_D] == 1) {
|
|
for(int k = 0; k < 10; k++) {
|
|
camx += speed*cos(rot_hz)/10;
|
|
camz += speed*sin(rot_hz)/10;
|
|
if(is_colliding(dtime)) {
|
|
camx -= speed*cos(rot_hz)/10;
|
|
camz -= speed*sin(rot_hz)/10;
|
|
k=11;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(state[SDL_SCANCODE_SPACE] == 1) {
|
|
stop_evetything = true ;
|
|
} else {
|
|
pass = true ;
|
|
}
|
|
|
|
if(state[SDL_SCANCODE_T] == 1) {
|
|
fprintf(stderr, "Killed.\n") ;
|
|
exit(1) ;
|
|
}
|
|
|
|
if(state[SDL_SCANCODE_A] == 1) {
|
|
rot_hz -= sensitivity;
|
|
}
|
|
if(state[SDL_SCANCODE_E] == 1) {
|
|
rot_hz += sensitivity;
|
|
}
|
|
|
|
if(state[SDL_SCANCODE_P] == 1) {
|
|
rot_vt -= sensitivity;
|
|
}
|
|
if(state[SDL_SCANCODE_M] == 1) {
|
|
rot_vt += sensitivity;
|
|
}
|
|
}
|
|
|
|
void drawHPbar(SDL_Renderer* renderer) {
|
|
SDL_Rect r ;
|
|
r.x = 1400 ;
|
|
r.y = 100 ;
|
|
r.w = 50 ;
|
|
r.h = 800 ;
|
|
SDL_SetRenderDrawColor(renderer, 96, 96, 96, SDL_ALPHA_OPAQUE);
|
|
SDL_RenderFillRect(renderer, &r);
|
|
r.x += 10 ;
|
|
r.w -= 20 ;
|
|
r.y += 10 ;
|
|
r.h -= 20 ;
|
|
SDL_SetRenderDrawColor(renderer, 32, 32, 32, SDL_ALPHA_OPAQUE);
|
|
SDL_RenderFillRect(renderer, &r);
|
|
r.y = 110+(780*(1000-player_hp))/1000 ;
|
|
r.h = 780-r.y+110 ;
|
|
SDL_SetRenderDrawColor(renderer, fade_dmg, 255-fade_dmg, 32, SDL_ALPHA_OPAQUE);
|
|
SDL_RenderFillRect(renderer, &r);
|
|
} |