added buttons interface and blocks

This commit is contained in:
Alexandre 2025-02-24 22:13:51 +01:00
parent 7bb62ba808
commit 9031dc18ea
23 changed files with 141 additions and 17 deletions

View File

@ -65,4 +65,14 @@ entities:
-> 10 (spinning platform)
[.. hz_speed, vt_speed] with
{hz_speed,vt_speed} = double
-> 11 (button trigger)
[.. freq, dtime] with
freq = int[0 - 15]
dtime = double[>0.0]
-> 12 (button block)
[.. freq, defaultState] with
freq = int[0 - 15]
defaultState = {0, 1}
```

BIN
bin/back

Binary file not shown.

View File

@ -4,8 +4,8 @@ Blocks :
[-4.0, 20.0, 0.0, 2.0, 2.0, 2.0, 0.0, 0.0, 192, 192, 192] // cp2
Entities:
[-2.0, 1.0 , -2.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 255, 128, 1, 0, 8, 100, 1, 128, 128, 128]
[-1.5, 1.5 , -1.5, 1.0, 1.0, 1.0, 0.0, 0.0, 192, 192, 192, 1, 0, 7, levels/level_01/, 7, great, 192, 192, 192]
[8.0, 1.0 , 8.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 255, 128, 1, 0, 8, 100, 1, 128, 128, 128]
[8.5, 1.5 , 8.5, 1.0, 1.0, 1.0, 0.0, 0.0, 192, 192, 192, 1, 0, 7, levels/level_01/, 7, great, 192, 192, 192]
[-3.5, 13.0, 0.5, 1.0, 1.0, 1.0, 0.0, 0.0, 255, 255, 128, 10, 0, 0] // cp1 coin
[-3.5, 23.0, 0.5, 1.0, 1.0, 1.0, 0.0, 0.0, 255, 255, 128, 10, 0, 0] // cp2 coin
@ -17,11 +17,11 @@ Entities:
[-4.0, 8.0, -4.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 255, 255, 1, 0, 9, 2.0, 2.0, 1]
[-4.0, 10.0, -2.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 255, 255, 1, 0, 9, 2.0, 2.0, 1]
[-4.0, 11.0, 7.0, 2.0, 1.0, 2.0, 0.0, 0.0, 192, 192, 192, 1, 0, 5, 0.0, 0.0, 5.0, 0.0, 0.0, 5.0] // section 2 (lines)
[ 1.0, 11.0, 14.0, 2.0, 1.0, 2.0, 0.0, 0.0, 192, 192, 192, 1, 0, 5, 5.0, 0.0, 0.0, -5.0, 0.0, 0.0]
[ 8.0, 16.0, 14.0, 2.0, 1.0, 2.0, 0.0, 0.0, 192, 192, 192, 1, 0, 5, 0.0, 5.0, 0.0, 0.0, 5.0, 4.0]
[-4.0, 21.0, 7.0, 2.0, 1.0, 2.0, 0.0, 0.0, 192, 192, 192, 1, 0, 5, 0.0, 0.0, 5.0, 0.0, 0.0, -5.0]
[ 1.0, 21.0, 14.0, 2.0, 1.0, 2.0, 0.0, 0.0, 192, 192, 192, 1, 0, 5, 5.0, 0.0, 0.0, 5.0, 0.0, 0.0]
[-4.0, 11.0, 7.0, 2.0, 1.0, 2.0, 0.0, 0.0, 192, 192, 192, 1, 0, 5, 0.0, 0.0, 5.0, 0.0, 0.0, 5.0]
[ 1.0, 11.0, 14.0, 2.0, 1.0, 2.0, 0.0, 0.0, 192, 192, 192, 1, 0, 5, 5.0, 0.0, 0.0, -5.0, 0.0, 0.0]
[ 8.0, 16.0, 14.0, 2.0, 1.0, 2.0, 0.0, 0.0, 192, 192, 192, 1, 0, 5, 0.0, 5.0, 0.0, 0.0, 5.0, 0.0]
[ 1.0, 21.0, 14.0, 2.0, 1.0, 2.0, 0.0, 0.0, 192, 192, 192, 1, 0, 5, 5.0, 0.0, 0.0, 5.0, 0.0, 0.0]
[-4.0, 21.0, 7.0, 2.0, 1.0, 2.0, 0.0, 0.0, 192, 192, 192, 1, 0, 5, 0.0, 0.0, 5.0, 0.0, 0.0, -5.0]
Weight :
0

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
res/button_off.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

BIN
res/button_on.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 312 KiB

View File

@ -98,6 +98,10 @@ double absf(double n) {
}
}
bool xor(bool a, bool b) {
return ((a && !b) || (!a && b));
}
// n >= 0
int reverse(int n) {
int res = 0;

View File

@ -12,6 +12,7 @@ double maxd(double a, double b);
float minf(float a, float b);
float maxf(float a, float b);
double absf(double n);
bool xor(bool a, bool b);
int reverse(int n);
int convex_seg(int x1, int x2, double theta);
double convex_tri(double a, double tha, double b, double thb, double c, double thc);

View File

@ -174,6 +174,14 @@ void gl_resetTexture() {
glBindTexture(GL_TEXTURE_2D, textures[0]);
}
bool is_button_block_on(entity* ent) {
return (
(ent->entity_type != 11 && ent->entity_type != 12) ||
(ent->entity_type == 11 && buttonSwitch[ent->metai1]) ||
(ent->entity_type == 12 && xor(buttonSwitch[ent->metai1], ent->metai2))
);
}
void gl_renderAll(room* rtd, double offx, double offy, double offz) {
//printf("------------------------\n");
if(rtd != NULL) {
@ -192,16 +200,12 @@ void gl_renderAll(room* rtd, double offx, double offy, double offz) {
}
for(int k = 0; k < rtd->ent_len; k++) {
if(is_visible(rtd->ents[k]->pos, offx, offy, offz)) {
//printf("%d\n", rtd->ents[k]->tex);
//printf("r %d\n", rtd->ents[k]->entity_type); fflush(stdout);
if(rtd->ents[k]->entity_type != 9 || rtd->ents[k]->metai1) {
if(is_button_block_on(rtd->ents[k]) && (rtd->ents[k]->entity_type != 9 || rtd->ents[k]->metai1)) {
glBindTexture(GL_TEXTURE_2D, textures[rtd->ents[k]->tex]);
} else {
//printf(">>%d<<\n", rtd->ents[k]->tex2);
glBindTexture(GL_TEXTURE_2D, textures[rtd->ents[k]->tex2]);
}
gl_renderCube(rtd->ents[k]->pos, offx, offy, offz);
//printf("OK\n"); fflush(stdout);
}
}
}

View File

@ -114,20 +114,23 @@ void moving_xyz(double x, double y, double z, double w, double h, double d, doub
// metai{3} = z_side (+/- 1)
void moving_xyz_line(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) {
//printf("%lf %lf %lf", ent->metad7, ent->metad8, ent->metad9);
//printf("| %lf %lf %lf\n", ent->metad4, ent->metad5, ent->metad6);
//printf("| %lf %lf %lf\n\n", ent->metad4, ent->metad5, ent->metad6);
//printf("%lf %lf %lf -> (%lf %lf %lf)\n", x, y, z, (ent->metai1)*ent->metad4*dtime, (ent->metai2)*ent->metad5*dtime, (ent->metai3)*ent->metad6*dtime);
//printf("(%d %lf) (%d %lf) (%d %lf)\n\n", (ent->metai1), ent->metad4, (ent->metai2), ent->metad5, (ent->metai3), ent->metad6);
ret->x += (ent->metai1)*ent->metad4*dtime;
if(absf(ent->metad1 - ret->x) > ent->metad7) {
//printf("-x-\n");
ent->metai1 *= (-1);
ret->x += (ent->metai1)*ent->metad4*dtime;
}
ret->y += (ent->metai2)*ent->metad5*dtime;
//printf("%lf, %lf | %lf\n", ent->metad2, ret->y, ent->metad8);
if(absf(ent->metad2 - ret->y) > ent->metad8) {
//printf("-y-\n");
ent->metai2 *= (-1);
ret->y += (ent->metai2)*ent->metad5*dtime;
}
ret->z += (ent->metai3)*ent->metad6*dtime;
if(absf(ent->metad3 - ret->z) > ent->metad9) {
//printf("-z-\n");
@ -175,6 +178,16 @@ void spinning_translate(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret
}
// metai1 = button freq (same for blocks)
// matei2 = trigger state (0/1) ('OFF state' for blocks)
// metad1 = button activation time
void update_button(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) {
if(!buttonSwitch[ent->metai1]) {
buttonSwitch[ent->metai1] = true;
buttonTimes[ent->metai1] = ent->metad1;
}
}
void detectHit(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) {
if(ret->red == 193) {
ret->red = 0;

View File

@ -20,6 +20,7 @@ void subtitle_text_box(double x, double y, double z, double w, double h, double
void beating_block(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret);
void spinning_platform(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret);
void update_button(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret);
void spinning_translate(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret);
void detectHit(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret);
void money(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret);

View File

@ -129,6 +129,22 @@ void init_ent_generator(int n) {
hashtbl_entities[10].updatePos = &spinning_platform;
hashtbl_entities[10].onHit = &spinning_translate;
hashtbl_entities[10].onDeath = NULL;
hashtbl_entities[11].id = 11;
hashtbl_entities[11].tex = 10;
hashtbl_entities[11].tex2 = 11;
hashtbl_entities[11].name = "buttonInterface";
hashtbl_entities[11].updatePos = NULL;
hashtbl_entities[11].onHit = &update_button;
hashtbl_entities[11].onDeath = NULL;
hashtbl_entities[12].id = 12;
hashtbl_entities[12].tex = 8;
hashtbl_entities[12].tex2 = 9;
hashtbl_entities[12].name = "buttonBlock";
hashtbl_entities[12].updatePos = NULL;
hashtbl_entities[12].onHit = NULL;
hashtbl_entities[12].onDeath = NULL;
}
fct_entry* get_entry(int k0) {
@ -519,6 +535,7 @@ void parse_one_room(int id, char* filename) {
double speed_x = read_float(ptr);
double speed_y = read_float(ptr);
double speed_z = read_float(ptr);
printf("%lf %lf %lf %lf %lf %lf\n", amp_x, amp_y, amp_z, speed_x, speed_y, speed_z);
pool[id].area->ents[k]->metad1 = cx;
pool[id].area->ents[k]->metad2 = cy;
pool[id].area->ents[k]->metad3 = cz;
@ -603,7 +620,7 @@ void parse_one_room(int id, char* filename) {
msg[ki] = 's'; ki += 1;
msg[ki] = 's'; ki += 1;
msg[ki] = '\0';
// k=18 if singlt digit
// k=18 if single digit
//pool[id].area->ents[k]->metai1 = build_text_box(msg, ired, igreen, iblue);
pool[id].area->ents[k]->metai2 = cost;
@ -629,6 +646,30 @@ void parse_one_room(int id, char* filename) {
pool[id].area->ents[k]->metad1 = hzspd;
pool[id].area->ents[k]->metad2 = vtspd;
} else if(entry->id == 11) {
int freq = read_int(ptr);
double acttime = read_float(ptr);
if(freq < 0 || freq >= 16) {
fprintf(stderr, "WARNING : invalid frequency for button : %d\n", freq);
fprintf(stderr, "using frequency 0\n");
freq = 0;
}
pool[id].area->ents[k]->metai1 = freq;
pool[id].area->ents[k]->metad1 = acttime;
} else if(entry->id == 12) {
int freq = read_int(ptr);
int offstate = read_int(ptr);
if(freq < 0 || freq >= 16) {
fprintf(stderr, "WARNING : invalid frequency for button : %d\n", freq);
fprintf(stderr, "using frequency 0\n");
freq = 0;
}
pool[id].area->ents[k]->metai1 = freq;
pool[id].area->ents[k]->metai2 = offstate;
} else {
pool[id].area->ents[k]->metai3 = 0;
}

View File

@ -518,6 +518,8 @@ int main_alt() {
//generate_texture_2D(9, "res/beat_off.png", JPG);
generate_texture_2D(8, "res/sq_full.png", PNG);
generate_texture_2D(9, "res/sq_dotted.png", PNG);
generate_texture_2D(10, "res/button_on.png", PNG);
generate_texture_2D(11, "res/button_off.jpg", JPG);
printf("-----------------------------------------------------------------------------------------------\n"); fflush(stdout);
@ -645,6 +647,7 @@ int main_alt() {
teleport_on_edge();
update_nearby_entities(delta, 1);
updateProj(delta);
update_buttons(delta);
} else {
gl_drawRectAlpha(shaderProgramR, -1.0f, -1.0f, 2.0f, 2.0f, 0, 0, 0, 128);
gl_drawString(shaderProgramR, "game Paused", 0.0f, 0.1f, 0.08f, 128, 128, 255, 0.005f, 0);

View File

@ -64,6 +64,9 @@ double sq2;
float dmgCD;
int lastDmg;
bool buttonSwitch[16];
float buttonTimes[16];
void init_csts() {
camx = 2.0;
camy = 5.0;
@ -86,6 +89,11 @@ void init_csts() {
draw_type = 0;
fade_dmg = 0;
for(int k = 0; k < 16; k++) {
buttonTimes[k] = 0.0f;
buttonSwitch[k] = false;
}
dmgCD = 0.0f;
lastDmg = player_hp;
@ -94,6 +102,19 @@ void init_csts() {
sq2 = sqrt(2);
}
void update_buttons(float dtime) {
for(int k = 0; k < 16; k++) {
if(buttonSwitch[k]) {
if(buttonTimes[k] <= dtime) {
buttonTimes[k] = 0.0f;
buttonSwitch[k] = false;
} else {
buttonTimes[k] -= dtime;
}
}
}
}
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) {
@ -249,7 +270,10 @@ bool is_colliding(float dtime) {
for(int k = 0; k < vstd->ent_len; k++) {
//printf("%d -> %d\n", k, vstd->ents[k]->entity_type);
if(vstd->ents[k]->entity_type != 9 || /* entityType == 9 */vstd->ents[k]->metai1) {
if(
(vstd->ents[k]->entity_type != 12 || xor(buttonSwitch[vstd->ents[k]->metai1], vstd->ents[k]->metai2)) &&
(vstd->ents[k]->entity_type != 9 || vstd->ents[k]->metai1)
) {
double dist = distance_pt_cube_0_3d_infinite(camx-2*room_width*w, camy, camz-2*room_depth*h, vstd->ents[k]->pos);
//printf("%lf vs %lf\n", dist, min_dist);
if(dist <= min_dist) {
@ -348,6 +372,10 @@ void teleport_on_edge() {
player_chx += 1;
current_room = hashtbl_find_opt(visited, player_chx, player_chy);
resetProj();
for(int k = 0; k < 16; k++) {
buttonSwitch[k] = false;
buttonTimes[k] = 0.0f;
}
}
} else if(camx <= -room_width) {
if(!is_one_room) {
@ -355,6 +383,10 @@ void teleport_on_edge() {
player_chx -= 1;
current_room = hashtbl_find_opt(visited, player_chx, player_chy);
resetProj();
for(int k = 0; k < 16; k++) {
buttonSwitch[k] = false;
buttonTimes[k] = 0.0f;
}
}
} else if(camz >= room_depth) {
if(!is_one_room) {
@ -362,6 +394,10 @@ void teleport_on_edge() {
player_chy += 1;
current_room = hashtbl_find_opt(visited, player_chx, player_chy);
resetProj();
for(int k = 0; k < 16; k++) {
buttonSwitch[k] = false;
buttonTimes[k] = 0.0f;
}
}
} else if(camz <= -room_depth) {
if(!is_one_room) {
@ -369,6 +405,10 @@ void teleport_on_edge() {
player_chy -= 1;
current_room = hashtbl_find_opt(visited, player_chx, player_chy);
resetProj();
for(int k = 0; k < 16; k++) {
buttonSwitch[k] = false;
buttonTimes[k] = 0.0f;
}
}
}
}

View File

@ -4,6 +4,8 @@
void init_csts();
bool is_colliding(float dtime);
void update_buttons(float dtime);
void teleport_on_edge();
void movePlayerG(float dtime);

View File

@ -190,4 +190,7 @@ extern unsigned int textures[16];
extern float dmgCD;
extern int lastDmg;
extern bool buttonSwitch[16];
extern float buttonTimes[16];
#endif

View File

@ -14,6 +14,8 @@ Teleporters :
Entities :
[-1.0, 13.0, -1.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 255, 255, 1, 0, 9, 1.5, 1.5, 0]
[-1.0, 16.0, -1.0, 2.0, 1.0, 2.0, 0.0, 0.0, 128, 128, 128, 1, 0, 11, 4, 1.75]
[-1.0, 19.0, -1.0, 2.0, 1.0, 2.0, 0.0, 0.0, 128, 128, 128, 1, 0, 12, 4, 1]
Weight :
50