Example #1
0
static void move_sprite(game_t *p_game, sprite_t *p_sprite)
{
  level_mask_t left_tile = 0, right_tile = 0, up_tile, down_tile;
  point_t horiz_overlap, vert_overlap;
  point_t old_pt = p_sprite->pt;
  point_t new_pt;

  new_pt = move_table(p_game, p_sprite, p_sprite->p_anims[p_sprite->state]);

  /* Check collisions with walls */
  if (p_sprite->dir == LEFT &&
      p_sprite->state != SPRITE_CLIMB)
    left_tile = check_bg_collision(p_game, p_sprite, LEFT, new_pt, &horiz_overlap);

  if (p_sprite->dir == RIGHT &&
      p_sprite->state != SPRITE_CLIMB)
    right_tile = check_bg_collision(p_game, p_sprite, RIGHT, new_pt, &horiz_overlap);

  up_tile = check_bg_collision(p_game, p_sprite, UP, new_pt, &vert_overlap);

  if (tile_type_map[up_tile] == VINES ||
      tile_type_map[left_tile] == VINES ||
      tile_type_map[right_tile] == VINES)
    {
      if (p_sprite->state != SPRITE_CLIMB)
	sprite_set_state(p_sprite, SPRITE_CLIMB);
      p_sprite->pt = new_pt;
      return;
    }

  if (left_tile || right_tile)
    {
      /* Undo the movement */
      new_pt = old_pt;
      new_pt.x += right_tile ? -2 : 2;
      sprite_set_state(p_sprite, SPRITE_IDLE);
    }
  down_tile = check_bg_collision(p_game, p_sprite, DOWN, new_pt, &vert_overlap);

  /* Fall? */
  if (!down_tile)
    {
      if (p_sprite->state == SPRITE_RUN ||
	  p_sprite->state == SPRITE_IDLE ||
	  p_sprite->state == SPRITE_HIT)
	sprite_set_state(p_sprite, SPRITE_FALL);
    }
  else if (p_sprite->state == SPRITE_FALL ||
	   p_sprite->state == SPRITE_JUMP_RUN)
    {
      sprite_set_state(p_sprite, SPRITE_IDLE);
      new_pt.y -= vert_overlap.y;
    }

  p_sprite->pt = new_pt;
}
//Attack
int
action_attack (object *hero, int key_up)
{
    if (hero)
    {
        if (!key_up)
        {
            sprite_set_state (hero->sprinstance, SPRITESET_ATTACK);
        }
        else
        {
            sprite_set_state (hero->sprinstance, SPRITESET_IDLE);
        }
        return 1;
    }
    return 0;
}
//Jump
int
action_crouch (object *hero, int key_up)
{
    if (hero)
    {
        if (!key_up)
        {
            physics_setjump (levels[curlevel]->physics, hero->physicsdata, 0);
            sprite_set_state (hero->sprinstance, SPRITESET_CROUCH);
        }
        else
        {
            sprite_set_state (hero->sprinstance, SPRITESET_IDLE);
        }
        return 1;
    }
    return 0;
}
int
action_idle (object *hero, int key_up) {
    if (hero)
    {
        sprite_set_state (hero->sprinstance, SPRITESET_IDLE);
        hero->vx_desired = 0.0f;
        return 1;
    }
    return 0;
}
//Move right
int
action_direction_left (object *hero, int key_up)
{
    if (hero)
    {
        if (!key_up)
        {
            physics_setdirection (levels[curlevel]->physics, hero->physicsdata,
                                  DIR_LEFT);
            hero->facing = OBJECT_FACING_W;
            sprite_set_state (hero->sprinstance, SPRITESET_WALK);
        }
        else
        {
            physics_setdirection (levels[curlevel]->physics, hero->physicsdata,
                                  DIR_NONE);
            sprite_set_state (hero->sprinstance, SPRITESET_IDLE);
        }
        return 1;
    }
    return 0;
}
void level_goto(game_t *p_game, level_t *p_level, point_t p)
{
  MAP_HEADER *p_bgmap = &p_game->bgmap;
  int i = 0;
  int closest_dist = 10000;
  point_t closest;

  /* Dispose the old map */
  vMapDispose();

  /* Initialise the background map */
  p_bgmap->width = p_level->w;
  p_bgmap->height = p_level->h;
  p_bgmap->mapoffset = (uint8_t*)p_level->p_level_data;
  p_bgmap->tiledata = (uint8_t*)p_game->p_tiles;
  p_bgmap->flag = 0;
  p_bgmap->format = BG_TILES_FORMAT;    /* the format of the bitmaps */

  p_game->p_cur_level = p_level;

  if ( !vMapInit(p_bgmap) )
    error_msg("vMapInit failed");

  while (p_level->bruce_spawn_points[i].x != -1)
    {
      int dist = abs(p_level->bruce_spawn_points[i].x - p.x) + abs(p_level->bruce_spawn_points[i].y - p.y);

      if (dist < closest_dist)
	{
	  closest = p_level->bruce_spawn_points[i];
	  closest_dist = dist;
	  DbgPrintf("Pt %d:%d closest\n\n", closest.x, closest.y);
	}
      i++;
    }
  p_game->player.sprite.pt = closest;
  sprite_set_state(&p_game->player.sprite, SPRITE_IDLE);

  place_lamps(p_game, p_level);
}
Example #7
0
static void game_init(game_t *p_game)
{
  void *p_buf;

  memset(p_game, 0, sizeof(game_t));

  if ( !(p_buf = (void *)get_resource(SPRITE_FRAMES, SPRITE_FRAMES_SIZE)) )
    error_msg("Alloc space for sprite frames\n");

  p_game->pp_sprites = unpack_sprite_frames(p_buf, SPRITE_FRAMES_UNPACKED_SIZE, N_FRAMES);
  vDisposePtr(p_buf);

  p_game->p_tiles = BG_TILES;
  p_game->player.sprite.dir = RIGHT;

  sprite_set_state(&p_game->player.sprite, SPRITE_IDLE);

  p_game->player.sprite.offset_right = FRAME_BRUCE_FIRST_RIGHT;
  p_game->player.sprite.p_bounds = bruce_bounding;
  p_game->player.sprite.p_anims = bruce_anims;

  if ( !(p_buf = get_resource(BRUCE_MIDI, BRUCE_MIDI_SIZE)) )
    error_msg("Alloc space for midi file failed!\n");
  if ( !(p_game->p_midi_data = unpack_data(p_buf, BRUCE_MIDI_UNPACKED_SIZE)) )
    error_msg("Unpack midi failed!\n");
  vDisposePtr(p_buf);

  store_midifile("bruce.mid", p_game->p_midi_data, BRUCE_MIDI_UNPACKED_SIZE);
  vDisposePtr(p_game->p_midi_data);

  play_midifile("bruce.mid");

  if ( !(p_buf = (void *)get_resource(TITLE_SCREEN, TITLE_SCREEN_SIZE)) )
    error_msg("Alloc space for sprite frames\n");
  if ( !(p_game->p_title_screen = (SPRITE*)unpack_data(p_buf, TITLE_SCREEN_UNPACKED_SIZE)) )
    error_msg("Unpack title screen failed!\n");
  vDisposePtr(p_buf);
}
Example #8
0
void world_init(void) {
    
    world_jump_vy=2*jump_max_h/jump_semi_t;
    world_g=world_jump_vy/jump_semi_t;

    world_walker_v=c_v_slow;
    
    struct tms paraeltimes;
    srand((unsigned int)times(&paraeltimes));
    
    world.t=0.0;
    
    world.waiting_to_start=True;
    
    world.player.is_player=True;
    world.player.x=0.0;
    world.player.y=0.0;
    world.player.vx=c_v_slow*0; // para que empiece parado hasta el go
    world.player.vy=0.0;
    world.player.sprite_class=sprite_classes[SPRCLS_ID_PLAYER];
    sprite_set_state(&world.player, STID_PLAYER_STOP);
   // world.player.anim_state=&(sprite_classes[SPRCLS_ID_PLAYER]->animstate[0]);
    world.player.anim_lastT=world.t;
    
    
    world.player_vida=1.0;
    world.player_deadline=60.0;
    world.player_checkpoint=7000.0;
    world.player_untouchable_time=0.0;
    
    world_zero_sprites();
    item_zero(&world);

//    world_create_walker(200,0);
//    world_create_walker(400,0);
//    world_create_walker(600,0);
//
//    world_create_walker(700,1);
//    world_create_walker(1000,1);
//
//    
//    world_create_walker(900,2);
//
//    world_create_walker(-800,3);
//    
//    world.time_next_piligrim=random_exp_time(10.0);
//    world.time_next_cansado=random_exp_time(5.0);
//    world.time_next_bici=random_exp_time(10.0);
//    printf("primer generado en t=%f\n",world.time_next_piligrim);
    
    render_clean_bocina();
    
    world.cam_x=-c_cam_offset;
    world.cam_v=0;
    // para que el jugador empiece fuera de la pantalla
    c_cam_init_free=1;
    world.cam_x+=c_cam_offset+300;
    // para que empiece dentro y parado esperando al go
    world.cam_x=-2*c_cam_offset;
    world.player.x+=world_pos0;
    
    world.zone_width=1000;
    world.zone_level_factor[0]=1.0;
    world.zone_level_factor[1]=5;
    world.zone[0].i[0]=-100;
    world.zone[1].i[0]=-100;
    world.zone[2].i[0]=-100;
    
    
    generador_init();

    t_itemPtr tumba_item=item_create(&world, ITEM_CLS_TUMBA);
    tumba_item->x=game_last_dead;
    tumba_item->y=0.0;
    tumba_item->tumba_owner=-1;
    int itumba;
    for (itumba=0; itumba<HIGHSCORE_MAX_TUMBAS; itumba++) {
        if ( highscore_table[itumba].score > HIGHSCORE_MINX_TUMBAS ) {
            t_itemPtr tumba_item=item_create(&world, ITEM_CLS_TUMBA);
            tumba_item->x=100.0*highscore_table[itumba].score+world_pos0;
            tumba_item->y=0.0;
            tumba_item->tumba_owner=itumba;
        }
    }
        
//    t_itemPtr aux_item=item_create(&world, ITEM_CLS_COMIDA_1);
//    aux_item->x=1030.0;
//    aux_item->y=150.0;

    
}
Example #9
0
void world_handle_collision(t_spritePtr player, t_spritePtr otro) {
    t_id player_state, otro_id, otro_state;
    player_state=player->anim_state->id;
    otro_state=otro->anim_state->id;
    otro_id=otro->sprite_class->id;
    t_bool invulnerable=(world.player_untouchable_time>world.t);

    if (otro_id==SPRCLS_ID_BICI && player_state<STID_PLAYER_FALL) {  // una bici atropella al player
        //player->vx=c_v_fast;
        if (invulnerable) return;
        if (player_state<STID_PLAYER_JUMP) {
            player->vy=120;
        }
        sprite_set_state(player, STID_PLAYER_FALL);
        world_lose_life(c_life_loss_per_hit);
        audio_play_player_clip(AUDIO_SND_PEREGRINO_OW);
        stats_add_caida();
        achievments_hitbybike();
    } else if (otro_id==SPRCLS_ID_CANSADO && player_state<STID_PLAYER_FALL) {   // choco con un cansado
        if (invulnerable) return;
        if (player_state!=STID_PLAYER_HIT) {
            if (player_state<STID_PLAYER_JUMP) {
                player->vy=100;
            }
            sprite_set_state(player, STID_PLAYER_FALL);
            world_lose_life(c_life_loss_per_hit);
            audio_play_player_clip(AUDIO_SND_PEREGRINO_OW);
            stats_add_caida();
        } else {
            // para hacer si le pegas a un cansado
            sprite_set_state(player, STID_PLAYER_WALK);
        }
    } else if ( (otro_id==SPRCLS_ID_PEREGRINO || otro_id==SPRCLS_ID_BORDONERO) && player_state==STID_PLAYER_HIT ) {   // player pega a un andante
        if (otro_state==STID_WALKER_RECEIVING_HIT) {
            return;
        }
        otro->vx=0;
        otro->n_hits-=1;
        if (otro->n_hits<=0) {
            if (otro_id==SPRCLS_ID_BORDONERO) {
                otro->n_hits+=1;
                sprite_set_state(otro, STID_WALKER_STOP);
                audio_play_golpe_hit();
            } else {
                sprite_set_state(otro, STID_WALKER_RECEIVING_HIT);
                audio_play_golpe_hit();
                stats_add_hit(SPRCLS_ID_PEREGRINO);
            }
        } else {
            sprite_set_state(otro, STID_WALKER_RECEIVING_HIT);
            audio_play_golpe_hit();
            stats_add_hit(SPRCLS_ID_BORDONERO);
        }
        achievments_hitwalker();
    } else if ( (otro_id==SPRCLS_ID_PEREGRINO || otro_id==SPRCLS_ID_BORDONERO) && player_state==STID_PLAYER_JUMP && otro_state<STID_WALKER_STOP) { // choco en vuelo con un andante
        if (invulnerable) return;
        sprite_set_state(player, STID_PLAYER_FALL);
        world_lose_life(c_life_loss_per_hit);
        audio_play_player_clip(AUDIO_SND_PEREGRINO_OW);
        stats_add_caida();
        // el otro tambien se cae
        sprite_set_state(otro, STID_WALKER_STOP);
        otro->vx=0;
    }
    
    

}
Example #10
0
void world_update_dt(void) {
    t_sprite_iterator iter;
    t_spritePtr sprite;
    t_item_iterator iiter;
    t_itemPtr item;
    
//    if (world.t-debug_time_printed > 10.0) {
//        debug_time_printed+=10.0;
//        printf("t= %.1f   %d sprites nexts: %.1f %.1f %.1f\n",debug_time_printed,world.sprites_n,world.time_next_piligrim,world.time_next_cansado,world.time_next_bici);
//    }
    
    
    
    // en la iteracion de actualizacion acumula collision candidates para despues
    collision_candidates_n=0;
    collision_bici_n=0;
    t_pos collision_x=world.player.vx*game_dt;
    // y calcula el walker mas retrasado y su velocidad
    t_pos min_x=500;
    t_v min_v=c_v_fast;
    world_iterate_sprites(&iter);
    // actualiza posicion y velocidad y estado de la animacion
    // tiene que hacerse al final para el player porque su velocidad se usa en el calculo de los demas
    while ( NULL != (sprite=world_iterate_next(&iter)) ) {
        sprite_update(sprite);

        if (sprite->anim_state_id > 100) {
            printf("warning : anim state invalid\n");
        }
        
        if (abs(sprite->x-collision_x)<200.0) { // si estan cerca pueden colisionar
            collision_candidates[collision_candidates_n]=sprite;
            collision_candidates_n+=1;
        }
        if ((sprite->sprite_class->id==SPRCLS_ID_PEREGRINO || sprite->sprite_class->id==SPRCLS_ID_BORDONERO) && (sprite->anim_state_id<STID_WALKER_STOP || sprite->anim_state_id==STID_WALKER_RECEIVED_HIT)) {// || sprite->anim_state_id==STID_WALKER_RECEIVING_HIT)) {
            if (sprite->x<min_x && sprite->x>0) {
                min_x=sprite->x;
                min_v=sprite->vx;
            }
        }
        if (collision_bici_n<1 && sprite->sprite_class->id==SPRCLS_ID_BICI) {
            collision_bici_n+=1;
            collision_bici=sprite;
        }
        
        // posible destruccion de sprites lejanos cuidado no usar despues sprite
        if (sprite->x-world.cam_x < -100 && sprite->sprite_class->id<SPRCLS_ID_BICI ) {
            world_delete_sprite(iter.i);
        }
        if (sprite->x-world.cam_x > 2000 && sprite->sprite_class->id==SPRCLS_ID_BICI ) {
            world_delete_sprite(iter.i);
            audio_una_bici_menos();
        }
    }
    
    //if (collision_candidates_n>0) printf("collision candidates: %d\n",collision_candidates_n);
    
    item_iterate_init(&iiter);
    while ( NULL != (item=item_iterate_next(&world, &iiter)) ) {
        item_update(item);
        
        if (item->x<-800) {
            item_delete(&world,iiter.i);
        }
    }
    

    if (world.player.x>world.player_checkpoint) {
//        printf("checkpoint !!!\n");
        world.player_checkpoint+=7000.0;
        world.player_deadline=world.t+60.0;
    }
    
    if (!world.waiting_to_start) {
        if (world.player.anim_state_id!=STID_PLAYER_FALLEN) {
            world_lose_life(game_dt*c_life_loss_per_second);
        }
    }
        
    if ( ( world.player_deadline<world.t || world.player_vida<=0.0 ) && ( world.player.anim_state_id<STID_PLAYER_STOP || world.player.anim_state_id==STID_PLAYER_SIDE ) ) {
        if (world.player.anim_state_id!=STID_PLAYER_TIRED) {
            audio_stop_music();
            audio_play_player_clip(AUDIO_SND_GAMEOVER);
            stats_end_game_timedistance();
        }
        world.player.vx=0;
        sprite_set_state(&world.player, STID_PLAYER_TIRED);
    }
    
    if (world.player.anim_state_id<STID_PLAYER_STOP && (world.player_untouchable_time<=world.t) ) {
        if(min_x<90.0) {
            world.player.vx=0.5*min_v;
        } else if (min_x<100.0) {
            world.player.vx=min_v;        
        }
    }
    
    sprite_update(&world.player);
    
    // controles
    t_id player_state=world.player.anim_state_id;

    if (!world.waiting_to_start) {
        if (world.player.vx<c_v_fast && player_state<STID_PLAYER_STOP) world.player.vx+=c_v_a*game_dt;
    } else {
        if (world.t>world_waiting_to_start_time) {
            world.waiting_to_start=False;
            sprite_set_state(&world.player, STID_PLAYER_WALK);
        }
    }

    if ( game_salto_pressed && player_state<STID_PLAYER_STOP ) {
        sprite_set_state(&world.player, STID_PLAYER_PREPARINGJUMP);
        audio_play_player_clip(AUDIO_SND_SALTO);
    }
    if ( game_hit_pressed && player_state<STID_PLAYER_STOP ) {
//        audio_play_player_clip(AUDIO_SND_HIT);
        audio_play_golpe_inicio();
        sprite_set_state(&world.player, STID_PLAYER_PREPARINGHIT);
    }
    if ( game_esquivar_pressed && player_state<STID_PLAYER_STOP ) {
        world.player.vx=0;
        sprite_set_state(&world.player, STID_PLAYER_SIDE);
    }
    if ( game_stop_pressed && player_state<STID_PLAYER_STOP ) {
        world.player.vx=c_v_slow*.7+c_v_fast*.3;
        //world_gain_life(1.0);
        //sprite_set_state(&world.player, STID_PLAYER_STOP);
    }
//    if ( !game_stop_pressed && player_state==STID_PLAYER_STOP ) {
//        world.player.vx=c_v_slow;
//        sprite_set_state(&world.player, STID_PLAYER_WALK);
//    }
    if ( !game_esquivar_pressed && player_state==STID_PLAYER_SIDE ) {
        world.player.vx=c_v_slow;
        sprite_set_state(&world.player, STID_PLAYER_WALK);
    }
    
//    // el estado puede haber cambiado por los controles
//    player_state=world.player.anim_state_id;
//    if ( world.player_vida<.1 && player_state<STID_PLAYER_STOP ) {
//        world.player.vx=c_v_slow*1.5;
//    }
//    
//    if ( world.player_vida<.05 && player_state<STID_PLAYER_STOP ) {
//        world.player.vx=c_v_slow*1.0;
//    }
    
    // si tienes poca vida y aun no estas muerto declarate in panic
    if (world.player_vida<.3 &&  world.player.anim_state_id!=STID_PLAYER_TIRED ) {
        float vida=world.player_vida;
        int ifreq=(vida<0.1)?3:((vida<0.2)?2:1);
        ifreq=1; // anular frecuencia variable del warning
        audio_low_warning_play(ifreq);
        achievments_inpanicnow(true);
    } else {
        achievments_inpanicnow(false);
    }
    
    
    // colisiones
    if ( world.player.y <0.001 && world.player.vy<0.001 ) {
        if ( player_state==STID_PLAYER_JUMP ) {
            world.player.y=0;
            world.player.vy=0;
//            world.player.vx=c_v_fast;
            sprite_set_state(&world.player, STID_PLAYER_WALK);
            audio_play_player_clip(AUDIO_SND_CAIDABIEN);
        } else if ( player_state==STID_PLAYER_FALL ) {
            world.player.y=0;
            world.player.vy=0;
            //world.player.vx=c_v_slow*0.5;
            sprite_set_state(&world.player, STID_PLAYER_FALLEN);
            audio_play_player_clip(AUDIO_SND_CAIDA);
        } else if ( player_state==STID_PLAYER_FALLEN ) {
            world.player.vx-=c_v_fast*0.02;
            if (world.player.vx<0) world.player.vx=0;
        }
    }
    
    int i;
    for (i=0; i<collision_candidates_n; i++) {
        sprite=collision_candidates[i];
        // colisionan sprite y player ???
        world_handle_collision_candidate(&world.player, sprite);
    }
    
    item_iterate_init(&iiter);
    while ( NULL != (item=item_iterate_next(&world, &iiter)) ) {
        if (item->item_class->type==ITEM_CLS_TUMBA) continue;
        if (world_item_collision_check(&world.player, item) /* && player_state==STID_PLAYER_STOP */ ) {
            audio_play_player_clip(AUDIO_SND_COMER);
            if (item->item_class->type==ITEM_CLS_POWERUP_LIFE) {
                world_gain_life(1.0);                
            } else {
                world_gain_life(c_life_gain_per_food);
                achievments_eaten(item);
            }
            item_delete(&world,iiter.i);
            stats_add_comida(item->item_class->type);
        }
    }
    
    // vuelta extra para que las bicis arrasen
    // del collision candidates se ha quedado con una bici y prueba si colisiona con otros
    if (collision_bici_n>0 && collision_bici->x<1500.0) {
        world_iterate_sprites(&iter);
        while ( NULL != (sprite=world_iterate_next(&iter)) ) {
            if (sprite!=collision_bici) {
               
                if (world_nonplayer_collision_check(collision_bici,sprite)) {
                    if ( (sprite->sprite_class->id==SPRCLS_ID_PEREGRINO || sprite->sprite_class->id==SPRCLS_ID_BORDONERO) && sprite->anim_state_id<STID_WALKER_STOP) {
                        audio_play_other_clip(AUDIO_SND_CAIDA);
                        sprite_set_state(sprite, STID_WALKER_RECEIVING_HIT);
                        sprite->vx=0.0;
                    }
                }
            }
        }
    }
    
    //int i;
//	int min_i;
//	t_pos min_x=200;
//	t_v min_v=100;
//    for (i=0;i<MAX_N_WALKER;i++) {
//        if (world.walker_list[i].valid) {
//            world.walker_list[i].x+=(world.walker_list[i].v-world.player_vx)*game_dt;
//			if (!world.walker_list[i].apartado) {
//				if (world.walker_list[i].x<min_x) {
//					min_x=world.walker_list[i].x;
//					min_v=world.walker_list[i].v;
//					min_i=i;
//				}
//			}
//            // regenera rapido para probar
//            if (world.walker_list[i].x-world.cam_x<-25.0) {
//               // printf("regenerate now\n");
//                world.walker_list[i].x=480+world.cam_x+24-100*log(rand()*1.0/RAND_MAX);
//                world.walker_list[i].v=c_v_slow+5.0*(int)(4.0*(rand()*1.0/RAND_MAX));
//				world.walker_list[i].apartado=0;
//            }
//            
//        }
//    }
//
//    if (min_x<50) {
//		world.player_vx=min_v;
//		if (!world.player_slow) {
//			world.player_slow=1;
//			world.player_slow_since=world.t;
//		} else {
//			if (world.t-world.player_slow_since>3.0) {
//				world.walker_list[min_i].apartado=1;
//				world.walker_list[min_i].v=0;
//			}
//		}
//	
//	} else {
//		world.player_vx=c_v_fast;
//		world.player_slow=0;
//	}
//	
//	if (world.player.y<0.0001) {
//		world.player.y=0;
////		if (game_salto_pressed) {
////			world.player_vy=100;
////		}
//        }	
//    
    
    //genera
    
//    if (world.t > world.time_next_piligrim) {
//        int tipo=rand()%2;   // de momento 50 - 50 cambiar probabilidades
//        world_create_walker(1000.0, tipo);
//        world.time_next_piligrim=world.t+random_exp_time(20.0);
//    }    
//    if (world.t > world.time_next_cansado) {
//        world_create_walker(1000.0, 2);
//        world.time_next_cansado=world.t+random_exp_time(20.0);
//    }    
//    if (world.t > world.time_next_bici) {
//        world_create_walker(-400.0, 3);
//        world.time_next_bici=world.t+random_exp_time(20.0);
//    }
    
    
    //probando cosas mas creativas para la camara
    // camara on fuerza que proporcional a la distancia al punto deseado
    // y con rozamiento viscoso para que no oscile

    if (cam_type==0) {    
    
        t_pos cam_now,cam_target;
        cam_now=world.cam_x-world.player.vx*game_dt;
        cam_target=-c_cam_offset;
//
    world.cam_v+=250.0*(cam_target-cam_now)*game_dt-1.5*world.cam_v;

//    if (abs(world.cam_v)>500.0) world.cam_v=copysign(500.0,world.cam_v);
//        world.cam_v=copysign(200,(cam_target-cam_now));
    
        float smooth_alfa=0; // 0 no smooth
        
        t_pos cam_smooth=cam_now+world.cam_v*game_dt;
        world.cam_x=world.cam_x*smooth_alfa+cam_smooth*(1-smooth_alfa);
        
    
 //   if (world.cam_x>cam_target) {world.cam_x=cam_target;world.cam_v=world.player.vx;}
//    if (world.cam_x+200<cam_target) {world.cam_x=cam_target;world.cam_v=world.player.vx;}

//    if (world.cam_x<-300) {world.cam_x=-300; world.cam_v=world.player.vx;}
//    if (world.cam_x>-100) {world.cam_x=-100; world.cam_v=world.player.vx;}
    } else if (cam_type==1) {
        
        // camara simple con distancia maxima y minima
        t_pos max_cam_distance=300;
        t_pos min_cam_distance=200-10000*c_cam_init_free;
        t_pos cam_now=world.cam_x-world.player.vx*game_dt+world.cam_v*game_dt;
        if (cam_now<-max_cam_distance) {world.cam_v=world.player.vx;cam_now=-max_cam_distance;}
        if (cam_now>-min_cam_distance) {world.cam_v=0*world.player.vx;cam_now=-min_cam_distance;}
        world.cam_x=cam_now;
        // cuando la camara alcanza al jugador se acaba el estado inicial de camara
        if (c_cam_init_free && cam_now<-200) c_cam_init_free=0;
    
    } else if (cam_type==2) {
        // de momento la camara sigue al jugador siempre a una distancia
        world.cam_x=-c_cam_offset;
    }
    
    
//    if(game_hit_pressed) {
//        world.cam_x+=10;
//    }
//    if(game_esquivar_pressed) {
//        world.cam_x-=10;
//    }
    
    
    world_update_zones();
    generador_update();
    
    world.t+=game_dt;
    
    achievments_check_update();
}
Example #11
0
static void handle_input(game_t *p_game, uint32_t keys)
{
  sprite_t *p_sprite = &p_game->player.sprite;

  if (!keys)
    {
      if (p_sprite->state == SPRITE_CROUCH)
	sprite_set_state(p_sprite, SPRITE_IDLE);
    }

  /* Handle running and jumping */
  if (p_sprite->state == SPRITE_IDLE ||
      p_sprite->state == SPRITE_RUN)
    {
      if (keys & KEY_LEFT)
	{
	  p_sprite->state = SPRITE_RUN;
	  p_sprite->dir = LEFT;
	}
      else if (keys & KEY_RIGHT)
	{
	  p_sprite->state = SPRITE_RUN;
	  p_sprite->dir = RIGHT;
	}
      if (keys & KEY_DOWN)
	sprite_set_state(p_sprite, SPRITE_CROUCH);

      /* Handle jumping */
      if (p_sprite->state == SPRITE_IDLE &&
	  (keys & KEY_UP))
	sprite_set_state(p_sprite, SPRITE_JUMP);
      else if (p_sprite->state == SPRITE_RUN &&
	       (keys & KEY_UP))
	sprite_set_state(p_sprite, SPRITE_JUMP_RUN);
    }
  else if (p_sprite->state == SPRITE_CLIMB)
    {
      anim_table_t *p_cur = p_sprite->p_anims[SPRITE_CLIMB];

      if (!keys)
	p_cur->entries[0].pt_offset = pt(0, 0);
      else
	{
	  if (p_game->player.sprite.move_table_idx == 0)
	    {
	      if (p_cur->entries[0].anim_frame == FRAME_BRUCE_CLIMB)
		{
		  p_cur->entries[0].anim_frame = FRAME_BRUCE_CLIMB+1;
		  p_cur->entries[1].anim_frame = FRAME_BRUCE_CLIMB+1;
		}
	      else
		{
		  p_cur->entries[0].anim_frame = FRAME_BRUCE_CLIMB;
		  p_cur->entries[1].anim_frame = FRAME_BRUCE_CLIMB;
		}
	    }

	  p_sprite->dir = RIGHT;

	  if (keys & KEY_UP)
	    p_cur->entries[0].pt_offset = pt(0, -4);
	  else if (keys & KEY_DOWN)
	    p_cur->entries[0].pt_offset = pt(0, 4);
	  else if (keys & KEY_LEFT)
	    p_cur->entries[0].pt_offset = pt(-4, 0);
	  else if (keys & KEY_RIGHT)
	    p_cur->entries[0].pt_offset = pt(4, 0);
	}

      /* Cannot hit etc when climbing */
      return;
    }

  if (keys & KEY_FIRE)
    {
      if (p_sprite->state == SPRITE_RUN)
	sprite_set_state(p_sprite, SPRITE_KICK);
      else if (p_sprite->state == SPRITE_IDLE &&
	       p_sprite->state != SPRITE_HIT)
	sprite_set_state(p_sprite, SPRITE_HIT);
    }
}