コード例 #1
0
void
Player::apply_friction()
{
  if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
    physic.set_velocity_x(0);
    physic.set_acceleration_x(0);
  } else if(physic.get_velocity_x() < 0) {
    physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
  } else {
    physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
  }

#if 0
  // if we're on ice slow down acceleration or deceleration
  if (isice(base.x, base.y + base.height))
  {
    /* the acceleration/deceleration rate on ice is inversely proportional to
     * the current velocity.
     */

    // increasing 1 will increase acceleration/deceleration rate
    // decreasing 1 will decrease acceleration/deceleration rate
    //  must stay above zero, though
    if (ax != 0) ax *= 1 / fabs(vx);
  }
#endif

}
コード例 #2
0
ファイル: tspga.c プロジェクト: stubbscroll/SOLVERS
/* distances are multiplied by 2 to avoid decimal numbers.
   normal floor: 2
   teleports: lynx: 4, ms: 0
   force floor: lynx: 1, ms: 0
*/
void dijkstra(int from) {
	static int dist2[MAXMAP][MAXMAP];
	static char taken[MAXMAP][MAXMAP];
	int i,j,n=chips+2,bestx,besty,best,d,x2,y2,cost,d2,slide;
//	printf("find distances from %d:\n",from);
	for(i=0;i<n;i++) dist[from][i]=(i==from)?0:INF;
	for(j=0;j<y;j++) for(i=0;i<x;i++) dist2[i][j]=(id[i][j]==from)?0:INF;
	for(j=0;j<y;j++) for(i=0;i<x;i++) taken[i][j]=0;
	while(1) {
		best=INF,bestx=besty=-1;
		for(j=0;j<y;j++) for(i=0;i<x;i++) if(!taken[i][j] && best>dist2[i][j]) best=dist2[i][j],bestx=i,besty=j;
		if(bestx<0) break;
		taken[bestx][besty]=1;
		if(id[bestx][besty]>-1) {
			j=id[bestx][besty];
			if(dist[from][j]==INF) {
				dist[from][j]=dist2[bestx][besty];
//				printf("  to %d: %d\n",j,dist2[bestx][besty]);
			}
		}
		if(map[bestx][besty]=='E') continue;
		for(d=0;d<4;d++) {
			x2=bestx+dx[d]; y2=besty+dy[d]; d2=d;
			if(x2<0 || y2<0 || x2>=x || y2>=y || map[x2][y2]=='#') continue;
			if(isicecornerwall(x2,y2,d)) continue;
			cost=0;
			slide=0;
		moveloop:
			if(x2<0) continue;
			if(map[x2][y2]=='T') {
				cost+=(ruleset==LYNX)?2:0;
				teleportendup(&x2,&y2,d2);
				slide=1;
				goto moveloop;
			} else if(isforcefloor(map[x2][y2])) {
				cost+=forcefloorendup(&x2,&y2,&d2);
				if(ruleset==MS) cost--;
				slide=1;
				goto moveloop;
			} else if(isice(map[x2][y2])) {
				cost+=iceendup(&x2,&y2,&d2);
				if(ruleset==MS) cost--;
				slide=1;
				goto moveloop;
			} else if(ruleset==LYNX || (ruleset==MS && !slide)) cost+=2;
			if(x2<0) continue;
			if(map[x2][y2]=='#') continue;
			if(isicecornerwall(x2,y2,d)) continue;
			if(dist2[x2][y2]>dist2[bestx][besty]+cost) dist2[x2][y2]=dist2[bestx][besty]+cost;
		}
	}
	if(from<=chips) for(i=0;i<chips+2;i++) if(dist[from][i]==INF) printf("can't go from %d to %d\n",from,i),exit(1);
}
コード例 #3
0
void
Player::handle_horizontal_input()
{
  float vx = physic.get_velocity_x();
  float vy = physic.get_velocity_y();
  float ax = physic.get_acceleration_x();
  float ay = physic.get_acceleration_y();

  float dirsign = 0;
  if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
      old_dir = dir;
      dir = LEFT;
      dirsign = -1;
  } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
      old_dir = dir;
      dir = RIGHT;
      dirsign = 1;
  }

  if (input.fire == UP) {
      ax = dirsign * WALK_ACCELERATION_X;
      // limit speed
      if(vx >= MAX_WALK_XM && dirsign > 0) {
        vx = MAX_WALK_XM;
        ax = 0;
      } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
        vx = -MAX_WALK_XM;
        ax = 0;
      }
  } else {
      ax = dirsign * RUN_ACCELERATION_X;
      // limit speed
      if(vx >= MAX_RUN_XM && dirsign > 0) {
        vx = MAX_RUN_XM;
        ax = 0;
      } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
        vx = -MAX_RUN_XM;
        ax = 0;
      }
  }

  // we can reach WALK_SPEED without any acceleration
  if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
    vx = dirsign * WALK_SPEED;
  }

  // changing directions?
  if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
      if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
          skidding_timer.start(SKID_TIME);
          play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
          ax *= 2.5;
      } else {
          ax *= 2;
      }
  }

  // we get slower when not pressing any keys
  if(dirsign == 0) {
      if(fabs(vx) < WALK_SPEED) {
          vx = 0;
          ax = 0;
      } else if(vx < 0) {
          ax = WALK_ACCELERATION_X * 1.5;
      } else {
          ax = WALK_ACCELERATION_X * -1.5;
      }
  }

  // if we're on ice slow down acceleration or deceleration
  if (isice(base.x, base.y + base.height))
  {
    /* the acceleration/deceleration rate on ice is inversely proportional to
     * the current velocity.
     */

    // increasing 1 will increase acceleration/deceleration rate
    // decreasing 1 will decrease acceleration/deceleration rate
    //  must stay above zero, though
    if (ax != 0) ax *= 1 / fabs(vx);
  }

  physic.set_velocity(vx, vy);
  physic.set_acceleration(ax, ay);
}