⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game_framemove.cpp

📁 这是一个台球游戏的源代码
💻 CPP
字号:
#include "stdafx.h"
#include "Game.h"
#include "Camera.h"
#include <math.h>

extern CCamera theCamera;
extern bool power;

UINT physics_loop(void *);

void CPoolGame::FrameMove(int mousex0, int mousey0, double time_step)
{
	if (status==GS_STATIONARY || status==GS_MOVING || status==GS_OVER)
	{
		double step_size=0.02;
		if (keymap['W']) 
			theCamera.step_forward(step_size);
		if (keymap['S']) 
			theCamera.step_backward(step_size);
		if (keymap['A']) 
			theCamera.step_left(step_size);
		if (keymap['D']) 
			theCamera.step_right(step_size);
		if (keymap['R']) 
			theCamera.step_up(step_size);
		if (keymap['F']) 
			theCamera.step_down(step_size);

		float a=1.0;
		if (keymap[VK_LEFT]) 
			theCamera.rotate_left_right(a);
		if (keymap[VK_RIGHT]) 
			theCamera.rotate_left_right(-a);
		if (keymap[VK_UP]) 
			theCamera.look_up_down(-a);
		if (keymap[VK_DOWN]) 
			theCamera.look_up_down(a);

		if (rotation_flag)
		{
			int dx=mousex0-mousex;
			int dy=mousey0-mousey;
			if (dx != 0) 
				theCamera.rotate_left_right(dx);
			if (dy!=0) 
				theCamera.look_up_down(dy);
			mousex=mousex0;
			mousey=mousey0;
		}
	}

	if (status==GS_AIMING && power)
	{
		if(hit_speed < max_hit_speed)
			hit_speed+=max_hit_speed*0.01;
		float factor=(hit_speed-min_hit_speed)/(max_hit_speed-min_hit_speed);
		rod_ball_dist=min_rod_ball_dist+factor*(max_rod_ball_dist-min_rod_ball_dist);
	}
	if (status==GS_AIMING && !power)
	{
		float factor=(hit_speed-min_hit_speed)/(max_hit_speed-min_hit_speed);
		rod_ball_dist=min_rod_ball_dist+factor*(max_rod_ball_dist-min_rod_ball_dist);

		if (keymap['W']) 
		{
			float lo=theCamera.m_r;
			theCamera.m_r-=0.01;
			float x0=theCamera.m_x, y0=theCamera.m_y, z0=theCamera.m_z;
			theCamera.get_pos_from_lookat();
			if(!theCamera.pos_valid()) 
			{
				theCamera.m_r=lo;
				theCamera.m_x=x0;
				theCamera.m_y=y0;
				theCamera.m_z=z0;
			}
			if (theCamera.m_r<0.5) 
				theCamera.m_r=lo;
		}
		if (keymap['S']) 
		{
			float lj=theCamera.m_r;
			theCamera.m_r+=0.01;
			float x0=theCamera.m_x, y0=theCamera.m_y, z0=theCamera.m_z;
			theCamera.get_pos_from_lookat();
			if(!theCamera.pos_valid()) 
			{
				theCamera.m_r=lj;
				theCamera.m_x=x0;
				theCamera.m_y=y0;
				theCamera.m_z=z0;
			}
			if(theCamera.m_r>1.8) 
				theCamera.m_r=1.8;
		}

		float dx=0.1*(mousex0-mousex);
		float dy=0.1*(mousey0-mousey);
		if (dx != 0) theCamera.rotate_left_right(-dx);
		if (dy!=0) theCamera.look_up_down(dy);

		mousex=mousex0;
		mousey=mousey0;
		theCamera.get_pos_from_lookat();
	}

	if(status==GS_HITTING)
	{
		rod_ball_dist-=0.5*hit_speed*time_step;
		if (rod_ball_dist<min_rod_ball_dist)
		{
		 status=GS_MOVING;
		 balls[0].v[0]=hit_speed*cos(theCamera.m_theta*3.1416/180.0);
		 balls[0].v[1]=hit_speed*sin(theCamera.m_theta*3.1416/180.0);
		 balls[0].status=BS_MOVING;

		AfxBeginThread(physics_loop, NULL, THREAD_PRIORITY_NORMAL);
		}
	}

	if(status==GS_INIT_REPOSITION)
	{
		status=GS_REPOSITION;
		balls[0].status=BS_STATIONARY;
		balls[0].x[0]=0.8;
		balls[0].x[1]=0.0;
		balls[0].x[2]=balls[0].r;
		reposition_valid=false;

		CPoint p;
		GetCursorPos(&p);
		mousex=p.x;
		mousey=p.y;
	}

	if(status==GS_REPOSITION)
	{
		CPoint p;
		CBall ball;
		GetCursorPos(&p);
		ball = balls[0];
		
		balls[0].x[0]+=0.005*(p.x-mousex);
		balls[0].x[1]-=0.005*(p.y-mousey);
		reposition_valid = Is_white_ball_pos_valid();
		if(!reposition_valid)
			balls[0]=ball;
		if (balls[0].x[0]<0.76) 
			balls[0].x[0]=0.76;
		if (balls[0].x[0]>1.24) 
			balls[0].x[0]=1.24;
		if (balls[0].x[1]<-0.6) 
			balls[0].x[1]=-0.6;
		if (balls[0].x[1]>0.6) 
			balls[0].x[1]=0.6;
		mousex=p.x;
		mousey=p.y;
	}
}

bool CPoolGame::Is_white_ball_pos_valid()
{
	for(int i=1; i<num_balls; ++i)
	{
		float dx=balls[0].x[0]-balls[i].x[0];
		float dy=balls[0].x[1]-balls[i].x[1];
		float dist=dx*dx+dy*dy;

		if ( dist < (balls[0].r+balls[i].r)*(balls[0].r+balls[i].r) ) 
			return false;
	}

	return true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -