Elphorina/src/main.c

187 lines
5.6 KiB
C
Executable File
Raw Blame History

#include <gint/display.h>
#include <gint/keyboard.h>
#include <gint/clock.h>
#define bloc 1
#define other 2
#define true 1
#define false 0
extern bopti_image_t img_personnage;
extern bopti_image_t img_personnagemarche;
extern bopti_image_t img_personnagemarcheleft;
extern bopti_image_t img_personnageleft;
extern bopti_image_t img_personnage2;
extern bopti_image_t img_personnage2left;
extern bopti_image_t img_bloc;
typedef int type;
typedef int bool;
struct object {
bopti_image_t *img;
int y;
type typeObj;
bool isStart;
bool isFinish;
int XrefAdd;
struct object *next;
};
struct object ListOfObjects[10] = {
{ &img_bloc, 55 , bloc , true, false, 16, &ListOfObjects[1] },
{ &img_bloc, 55 , bloc , false, false,32, &ListOfObjects[2] },
{ &img_bloc, 55 , bloc , false, false,48, &ListOfObjects[3] },
{ &img_bloc, 55 , bloc , false, false,64, &ListOfObjects[4] },
{ &img_bloc, 55 , bloc , false, false,80, &ListOfObjects[5] },
{ &img_bloc, 55 , bloc , false, false,96, &ListOfObjects[6] },
{ &img_bloc, 55 , bloc , false, false,112,&ListOfObjects[7] },
{ &img_bloc, 55 , bloc , false, false,128,&ListOfObjects[8] },
{ &img_bloc, 55 , bloc , false, false,144,&ListOfObjects[9] },
{ &img_bloc, 55 , bloc , false, true, 160,&ListOfObjects[0] },
};
struct player {
int x;
int y;
bool isWalkableTop;
bool isWalkableBack;
struct player *next;
};
struct player Player1[1] = {
{ 64,31,true, true, &Player1[0] },
};
struct anim {
bopti_image_t *img;
int duration;
struct anim *next;
};
struct anim anim_idle[2] = {
{ &img_personnage, 40, &anim_idle[1] },
{ &img_personnage2, 40, &anim_idle[0] },
};
struct anim anim_walk[2] = {
{ &img_personnagemarche, 3 , &anim_walk[1] },
{ &img_personnage, 3, &anim_walk[0] },
};
struct anim anim_idle_left[2] = {
{ &img_personnageleft, 40, &anim_idle_left[1] },
{ &img_personnage2left, 40, &anim_idle_left[0] },
};
struct anim anim_walk_left[2] = {
{ &img_personnagemarcheleft, 3, &anim_walk_left[1] },
{ &img_personnageleft, 3, &anim_walk_left[0] },
};
int main(void)
{
struct anim *current_anim = &anim_idle[0];
int current_anim_time_left = 0;
extern bopti_image_t img_personnage2left;
extern bopti_image_t img_personnageleft;
extern bopti_image_t img_personnagemarcheleft;
extern bopti_image_t img_personnage;
extern bopti_image_t img_personnagemarche;
extern bopti_image_t img_personnage2;
extern bopti_image_t img_bloc;
int a = 0;
int xref = 0;
unsigned int i = 0;
struct object *current_object = &ListOfObjects[0];
/* <20>tat du personnage : 0=arr<72>t<EFBFBD>, 1=marche */
int state = 0;
/* <20>tat du personnage au frame pr<70>c<EFBFBD>dent */
int previous_state = 0;
struct player *current_player = &Player1[0];
while(a != 1)
{
/* Affichage */
dclear(C_WHITE);
dimage(current_player->x,current_player->y, current_anim->img);
for(i=0;i<=(sizeof(ListOfObjects) / sizeof(struct object));i++)
{
dimage(xref+current_object->XrefAdd, current_object->y, current_object->img);
current_object = current_object->next;
}
dupdate();
current_object = &ListOfObjects[0];
/* Lecture des entr<74>es ; si on n'appuie sur rien, state=0 */
clearevents();
state = 0;
if(keydown(KEY_EXE))
a = 1;
if(keydown(KEY_RIGHT))
state = 1;
if(keydown(KEY_LEFT))
state = -1;
/* Ex<45>cution des animations */
if((previous_state == 0 && state == 1) || (previous_state == -1 && state == 1))
{
/* On vient de commencer <20> marcher */
current_anim = &anim_walk[0];
current_anim_time_left = current_anim->duration;
}
else if((previous_state == 1 && state == 0) || (previous_state == 1 && state == -1))
{
/* On vient de s'arr<72>ter */
current_anim = &anim_idle[0];
current_anim_time_left = current_anim->duration;
}
else if((previous_state == 0 && state == -1) || (previous_state == 1 && state == -1))
{
current_anim = &anim_walk_left[0];
current_anim_time_left = current_anim->duration;
}
else if((previous_state == -1 && state == 0) || (previous_state == -1 && state == 1))
{
current_anim = &anim_idle_left[0];
current_anim_time_left = current_anim->duration;
}
else
{
/* On continue l'anim pr<70>c<EFBFBD>dente */
current_anim_time_left--;
if(current_anim_time_left <= 0)
{
current_anim = current_anim->next;
current_anim_time_left = current_anim->duration;
}
}
/* Simulation du monde */
current_player->isWalkableTop = true;
for(i=0;i<=(sizeof(ListOfObjects) / sizeof(struct object));i++)
{
if(current_player->x == xref+current_object->XrefAdd && current_object->isFinish == true)
{
current_player->isWalkableTop = false;
}
}
if(state == 1)
{
if(current_player->isWalkableTop == true)
{
xref = xref-1;
}
}
else if(state == -1)
{
xref = xref+1;
}
/* D<>lai */
sleep_us(25000);
/* Pr<50>paration des invariants du frame suivant */
previous_state = state;
}
getkey();
return 1;
}