g_weapon.c

来自「著名物理引擎Hawk的源代码」· C语言 代码 · 共 143 行

C
143
字号
/* G_weapon.c, HAWK game engine
 *
 * Copyright 1997-1998 by Phil Frisbie, Jr.
 * for Hawk Software
 *
 */

#include "G_main.h"
#include "G_math.h"

#define SHOT_SPEED	40

void G_objShoot(OBJECT *o) /* sample shoot routine */
{
	OBJECT		*shot;
	VIEW		*view = Level->view[0];
	static int	sound = 0;
	
	if(!sound)
		sound = gi.SoundLoad("sound/weapons/laser2.wav");
	
	shot = gi.ObjectAdd();
	if((o == view->object)&&(!(view->type&V_EXTERNAL))) /* this is the player, so add the view angle */
	{
		VectorCopy(o->origin, shot->origin);
		shot->origin[Z] += (o->maxs[Z] - 20);
		shot->angles[Z] = o->angles[Z] + view->hangle;
	}
	else
	{
		VectorCopy(o->origin, shot->origin);
		shot->origin[Z] += (o->maxs[Z] - 20);
		shot->angles[Z] = o->angles[Z];
	}
	
	shot->type = O_NOFRIC|O_NOSLIDE|O_WALLCOL|O_OBJCOL|O_PROJECTILE;
	shot->height = 0;
	shot->radius = 0;
	shot->mass = 1;
	shot->pitch = 0.0;
	shot->acc = 0.0;
	shot->turn = 0.0;
	shot->speed = cos(RAD(o->pitch)) * SHOT_SPEED;
	shot->maxspeed = 100.0;
	shot->strafeSpeed = 0.0;
	shot->vertSpeed = sin(RAD(o->pitch)) * SHOT_SPEED;
	shot->model = NO_MODEL;
	shot->weapon = NO_MODEL;
	shot->update = G_ShotUpdate;
	shot->param[0] = TotalFrames;/* birth of shot */
	shot->param[1] = TotalFrames + 90;/* death time for shot */
	
	gi.PlaySoundi(sound, CHAN_CENTER, 1.0, FALSE);
}

void G_ShotUpdate(OBJECT *obj)
{
	int			i, frames = Frames;
	vec3_t		move, neworigin;
	vec3_t		color = {1.0, 1.0, 1.0};
	static int	sound1 = 0;
	static int	sound2 = 0;
	
	/* you need to do this for fast 3D cards */
	if(!frames)
		return;
	
	if(!sound1)
		sound1 = gi.SoundLoad("sound/weapons/laser2.wav");
	if(!sound2)
		sound2 = gi.SoundLoad("sound/world/explod2.wav");
	
	if(obj->param[1] < TotalFrames)/* time to kill the shot */
	{
		gi.Splash(neworigin, 30, 1, color);
		gi.PlaySound3Di(sound2, obj, 1.0, FALSE);
		gi.ObjectDelete(obj);
		return;
	}
	
	move[X] = frames * (obj->speed * cos(RAD(obj->angles[Z])));
	move[Y] = frames * (obj->speed * sin(RAD(obj->angles[Z])));
	move[Z] = frames * obj->vertSpeed;
	
	/* apply friction */
	if(!(obj->type & O_NOFRIC))
	{
		for (i = 0; i < frames; ++i)
		{
			obj->speed *= Level->force;
			obj->strafeSpeed *= Level->force;
			obj->vertSpeed *= Level->force;
		}
	}
	
	VectorAdd(move, obj->origin, neworigin);
	
	if(obj->type & (O_WALLCOL|O_OBJCOL))
	{
		TRACE	*tr;
		
		tr = gi.TraceRay(obj->origin, neworigin, obj, MASK_SHOT);
		if(obj->type & O_PROJECTILE)
		{
			if(tr->fraction < 1.0)
			{
				if(!(tr->surface->textype&SURF_SKY))
				{
					gi.Trail(obj->origin, tr->endpos, 2, color);
					gi.Splash(tr->endpos, 30, 1, color);
					gi.PlaySound3Di(sound2, obj, 1.0, FALSE);
				}
				gi.ObjectDelete(obj);
				return;
			}
			else if(tr->startsolid)
			{
				gi.Splash(neworigin, 30, 1, color);
				gi.PlaySound3Di(sound2, obj, 1.0, FALSE);
				gi.ObjectDelete(obj);
				return;
			}
			else
				gi.Trail(obj->origin, neworigin, 2, color);
		}
		else
		{
		}
		/* check for collisions with other objects */
		if(obj->type & O_OBJCOL)
		{
		}
		VectorCopy(neworigin, obj->origin);
	}
	else
	{
		VectorCopy(neworigin, obj->origin);
	}
	if(obj->type & O_OBJCOL)
	{
	}
}

⌨️ 快捷键说明

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