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

📄 bullet.cpp

📁 极限赛车CarGameDemo DirectX9
💻 CPP
字号:
#include "StdAfx.h"
#include "../common/myd3d.h"
#include "mesh.h"
#include "bullet.h"

Bullet::Bullet(void)
{
	
}

Bullet::~Bullet()
{

}

void Bullet::Move(float units)
{
	_pos += D3DXVECTOR3(_look.x, 0.0f, _look.z) * _speed * units;
}

void Bullet::SavePosMatrix()
{
	// 保持自身坐标互相垂直
	D3DXVec3Normalize(&_look, &_look);

	D3DXVec3Cross(&_up, &_look, &_right);
	D3DXVec3Normalize(&_up, &_up);

	D3DXVec3Cross(&_right, &_up, &_look);
	D3DXVec3Normalize(&_right, &_right);

	// 创建位置变换矩阵(注:不同于Camera类的坐标变换矩阵)
	float x = _pos.x;
	float y = _pos.y;
	float z = _pos.z;

	_world(0,0) = _right.x; _world(0, 1) = _right.y; _world(0, 2) = _right.z; _world(0, 3) = 0.0f;
	_world(1,0) = _up.x;	_world(1, 1) = _up.y;    _world(1, 2) = _up.z;    _world(1, 3) = 0.0f;
	_world(2,0) = _look.x;  _world(2, 1) = _look.y;  _world(2, 2) = _look.z;  _world(2, 3) = 0.0f;
	_world(3,0) = x;        _world(3, 1) = y;        _world(3, 2) = z;        _world(3, 3) = 1.0f;
}

void Bullet::Update(float timeDelta)
{
	if(_isAlive)//子弹存在
	{
		SavePosMatrix();
		for(int i=0; i<5; i++)
		{
			D3DXVec3TransformCoord(&_bBox._Nvxz[i], &_bBox._vxz[i], &_world);
		}
		D3DXVec3TransformCoord(&_bSphere._Ncenter, &_bSphere._center, &_world);
	}
	_bulletExp.Update(timeDelta);
}

D3DXVECTOR3 Bullet::GetPosition(D3DXVECTOR3* pos)
{
	if(pos == NULL)
		return _pos;
	else
	{
		*pos = _pos;
		return _pos;
	}
}
//重写Init方法
bool Bullet::Init( LPCSTR bulletFN , LPCSTR expFN )
{
	_isAlive = false;
	_speed = NORMALSPEED;
	_pos   = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	_right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
	_up    = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
	_look  = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
	Mymesh::Init( bulletFN );
	_bulletExp.Init( Explosion::BULLET, expFN );
	return true;
}
//重写Clean方法
void Bullet::Clean()
{
	Mymesh::Clean();
	_bulletExp.Clean();
}
//重写Draw方法
bool Bullet::Draw()
{
	if(_isAlive)
	{
		Mymesh::Draw();
	}
	
	_bulletExp.Rend();
	
	return true;
}

bool Bullet::Kill()
{
	//获取爆炸位置
	D3DXVECTOR3 pos;
	GetPosition( &pos);
	_bulletExp.setPos( pos );
	//获取当前爆炸时间
	_bulletExp.getExpTime();
	_bulletExp._isExp = true;
	//回收盒子
	for(int i=0; i<5; i++)
	{
		_bBox._Nvxz[i] = _bBox._vxz[i];
	}
	_isAlive = false;
	return true;
}



⌨️ 快捷键说明

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