bool ScriptSystem::Init() { L = lua_open(); luaL_openlibs(L); // Register our version of the global "print" function const luaL_reg global_functions [] = { {"print", lua_print}, {nullptr, nullptr} }; luaL_register(L, "_G", global_functions); luaopen_sprite(L); luaopen_render(L); luaopen_model(L); luaopen_camera(L); luaopen_animodel(L); return true; }
game_status state_in_game(SDL_Surface *screen, game* p_game) { int i=0; bool done=false; game_status ret_code = GAME_OVER; SDL_Rect srcrect = {0,0,160,144}; dstrect = set_rect(0,0,SCREEN_HEIGHT,SCREEN_WIDTH); #ifdef USE_LUA lua_State *L; L = lua_open(); luaopen_base(L); luaL_openlibs(L); luaopen_globals(L); luaopen_game(L); luaopen_unit(L); luaopen_sprite(L); luaopen_var(L); luaopen_map(L); //register lua functions lua_register(L ,"say", script_lua_unit_say); lua_register(L ,"unit_get_x", script_lua_unit_get_x); lua_register(L ,"unit_get_y", script_lua_unit_get_y); lua_register(L ,"unit_set_index_x", script_lua_unit_set_index_x); lua_register(L ,"unit_set_index_y", script_lua_unit_set_index_y); lua_register(L ,"event_text", script_lua_event_exec_text); lua_register(L ,"event_teleport", script_lua_event_exec_teleport); lua_register(L ,"unit_set_life", script_lua_unit_set_life); lua_register(L ,"unit_set_speed", script_lua_unit_set_speed); p_game->L = L; g_game = p_game; #endif long timelastcall=SDL_GetTicks(); if(map_get_current(p_game->p_map, p_game->cur_map)->music != NULL){ char* music = strdup(map_get_current(p_game->p_map, p_game->cur_map)->music); Mix_Music* ingame_music = music_load(music); music_play(ingame_music); } message_box* p_menu = NULL; p_menu = menu_ingame_create(p_game); bool action; while (!done) { action = false; SDL_Event event; SDL_JoystickUpdate(); while (SDL_PollEvent(&event)){ switch ( event.type ){ case SDL_QUIT: ret_code = QUIT; done = true; break; case SDL_KEYUP: switch ( event.key.keysym.sym ){ case SDLK_RETURN: switch(menu_start(get_backbuffer_surface(), p_game)){ case 0: menu_status(get_backbuffer_surface(), p_game); break; case 1: menu_save(get_backbuffer_surface(), p_game); break; case 2: ret_code = LOAD; done=true; break; } break; default:break; } break; case SDL_JOYBUTTONDOWN: switch(event.jbutton.button){ case 0: switch(menu_start(get_backbuffer_surface(), p_game)){ case 0: menu_status(get_backbuffer_surface(), p_game); break; case 1: menu_save(get_backbuffer_surface(), p_game); break; case 2: ret_code = LOAD; done=true; break; } break; case 2: action = true; break; } break; default:break; } unit_handle_key(p_game->p_unit, &event, p_game); } int joystate = SDL_JoystickGetHat(p_game->p_unit->joystick, 0); switch (joystate){ case SDL_HAT_DOWN: unit_set_vel_y(p_game->p_unit, p_game->p_unit->speed); unit_set_vel_x(p_game->p_unit, 0); p_game->p_unit->p_sprite->animation_current = DOWN; p_game->p_unit->dir = DOWN; p_game->p_unit->current_action = NOTHING; break; case SDL_HAT_UP: unit_set_vel_y(p_game->p_unit, -p_game->p_unit->speed); unit_set_vel_x(p_game->p_unit, 0); p_game->p_unit->p_sprite->animation_current = UP; p_game->p_unit->dir = UP; p_game->p_unit->current_action = NOTHING; break; case SDL_HAT_RIGHT: unit_set_vel_x(p_game->p_unit, p_game->p_unit->speed); unit_set_vel_y(p_game->p_unit, 0); p_game->p_unit->p_sprite->animation_current = RIGHT; p_game->p_unit->dir = RIGHT; p_game->p_unit->current_action = NOTHING; break; case SDL_HAT_LEFT: unit_set_vel_x(p_game->p_unit, -p_game->p_unit->speed); unit_set_vel_y(p_game->p_unit, 0); p_game->p_unit->p_sprite->animation_current = LEFT; p_game->p_unit->dir = LEFT; p_game->p_unit->current_action = NOTHING; break; default: break; } //drawing SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0)); map_draw(map_get_current(p_game->p_map, p_game->cur_map), backbuffer); if(p_game->p_unit->current_action == FIGHT){ anim_sprite_draw(p_game->p_unit->weapon, get_backbuffer_surface()); } anim_sprite_draw(p_game->p_unit->p_sprite, backbuffer); for(i=0;i<p_game->NPC_nb;i++){ NPC_update(p_game->p_NPC+i, map_get_current(p_game->p_map, p_game->cur_map), p_game->p_unit); anim_sprite_draw(p_game->p_NPC[i].p_sprite, backbuffer); } for(i=0;i<p_game->ennemie_nb;i++){ ennemie_update(p_game->p_ennemie+i, map_get_current(p_game->p_map, p_game->cur_map), p_game->p_unit); anim_sprite_draw(p_game->p_ennemie[i].p_sprite, backbuffer); } Uint8* key = SDL_GetKeyState(NULL); if(key[p_game->p_unit->key_b] || action){ for(i=0;i<p_game->NPC_nb;i++){ if(sprite_intersect((sprite*)p_game->p_unit->p_sprite, (sprite*)p_game->p_NPC[i].p_sprite)){ struct t_event* p_event = malloc(sizeof(struct t_event)); p_event->is_auto = true; p_event->p_next = NULL; p_event->type = EVENT_SCRIPT; event_script* p_event_script = malloc(sizeof(event_script)); p_event_script->language = strdup("lua"); p_event_script->script = strdup(p_game->p_NPC[i].script); p_event_script->version = strdup("1.0"); p_event->data = p_event_script; event_exec_script(p_event, p_game); event_free(p_event); } } } //logic sprite_calc_bounding_box((sprite*)p_game->p_unit->p_sprite, true); //events when entering a tile point* p_point_enter = unit_check_on_tile_enter(p_game->p_unit, map_get_current(p_game->p_map, p_game->cur_map)); if( p_point_enter != NULL && map_get_current(p_game->p_map, p_game->cur_map)->pp_tile[p_point_enter->y][p_point_enter->x].p_event != NULL && map_get_current(p_game->p_map, p_game->cur_map)->pp_tile[p_point_enter->y][p_point_enter->x].p_event->is_auto){ p_game->p_unit->p_sprite->v_anim[p_game->p_unit->p_sprite->animation_current].frame_current = 1; event_dispatch(map_get_current(p_game->p_map, p_game->cur_map)->pp_tile[p_point_enter->y][p_point_enter->x].p_event, p_game); free(p_point_enter); } map* p_cur_map = map_get_current(p_game->p_map, p_game->cur_map); if(SDL_GetTicks() - timelastcall>1000/40) { unit_update(p_game->p_unit, p_cur_map); for(i=0;i<p_game->ennemie_nb;i++){ //Check weapon / ennemie collides if(p_game->p_unit->current_action == FIGHT){ if(sprite_intersect((sprite*)p_game->p_unit->weapon, (sprite*)p_game->p_ennemie[i].p_sprite)){ p_game->p_ennemie[i].HP--; if(p_game->p_ennemie[i].HP <= 0){ //ennemie dead //sound dead //animation dead p_game->p_unit->XP += p_game->p_ennemie[i].XP; //increase unit status p_game->p_unit->gold += p_game->p_ennemie[i].gold; ennemie_remove(p_game->p_ennemie, i, &p_game->ennemie_nb); //remove ennemie break; } } } //Check unit / ennemie collides if(p_game->p_unit->invincible_time <= 0 && sprite_intersect((sprite*)p_game->p_unit->p_sprite, (sprite*)p_game->p_ennemie[i].p_sprite)){ //sample_play(unit_hitted); p_game->p_unit->invincible_time = 40; unit_hitted(p_game->p_unit, p_game->p_ennemie+i, p_cur_map); if(p_game->p_unit->HP <= 0){ //sound dead //animation dead //message game over ret_code = GAME_OVER; done = true; break; } } } //Game events menu_ingame_update(get_backbuffer_surface(), p_menu, p_game); //update timer timelastcall=SDL_GetTicks(); } #if RENDER == 3 && !GEKKO SDL_GL_SwapBuffers(); #else SDL_SoftStretch(backbuffer, &srcrect, screen, &dstrect); SDL_Flip(screen); #endif } game_free(p_game); return ret_code; }