Strict

Import "soundControl.bmx"			'load all music and sound files
Import "camera.bmx"					'defines the camera the game is viewed through
Import "entityHosted.bmx"			'defines entity extension that allows synching between all machines
Import "gertrude.bmx"				'entity placeholder that contains its physics information
'Import "entity_background.bmx"		'defines background and foreground entities
Import "vararray.bmx"				'an array of variables with names; useful when we want to track only a couple unique variables

Global shotList:TList = New TList
Type Shot Extends hostedEntity
	Field name$						'the name of the gun that fired it
	Field owner:Gertrude			'the ship that fired it
	Field target:Gertrude			'null except for a few fringe cases (where the shot is tracking somebody)
	Field scriptslot				'the script of the gun and component that produced this shot
	Field skipLuaUpdate = True		'should we skip calling the lua update function?
	
	Field lifetimer	= 200			'how long it lasts, in millisecs
	Field lifetimerMax = 200
	Field durable = False			'whether or not it should stick around after hitting a ship
	Field dependent	= False			'whether or not it should stick around after owner dies
	Field damage#					'amount of damage shot does
	Field knockback#				'amount of knockback this shot effects
	Field crit# = 1.0				'the critical hit multiplier
	
	Field maxSpeed = 1500			'if it goes faster than this, lifetimer is just extended
	Field lifetimerMod = 1.0		'via this

	Field skipFortify = False		'does this shot bypass damage resistance?
	Field skipShields = False		'does this shot ignore shields?

	Field fadeTime = 300			'the time at which the shot starts fading out

	Field hit_gfx$ = GFX_EFFECT_PATH+"explode1.png"		'path+filename of the gfx to create when hit something
	Field hit_sfx:TSound
	
	Field doppler = False			'should we add the speed of the owner? if true, we don't (AKA the doppler effect is apparent)
	
	Field vars:VarArray = New VarArray	'an array of variable spaces
	
	Method setClass()
		class = "shot"
	EndMethod
	
	Method init(gfxName$, animated = True, layer = 5)
		setClass()
	
		If owner <> Null
			'can't hit owner
			ignoreList.AddLast(owner)
			'FOR PLAYERS: add the ship's current velocity to the shot; no doppler.
			If owner.behavior <> "player" Then doppler = True 
			
			'if we don't want the doppler effect, add speed of the owner
			If Not doppler Then addSpeed(owner.speed, owner.movrot)
		EndIf
		
		
		
		'small grey box on minimap
		minimap = 0
		
		'- add to the shot list -
		link = shotList.AddLast(Self)
		
		'-SPRITE-
		netSprite:TGNetObject = CreateGNetObject(netHost)
		initNetSprite(gfxName, NET_SPRITE_GFXTYPE_EFFECT, animated, layer)
	EndMethod
	
	Method update()
		
		'shot update script (if applicable)
		If Not skipLuaUpdate
			LuaRegisterObject Self, "shot"
			gunScript.call(scriptslot, "shot_update")
		EndIf
			
		'if the shot tries to go faster than the max speed, cap it and modify lifetimer instead
		If speed > maxSpeed
			lifetimerMod = Max(lifetimerMod, speed / maxSpeed)
			speed = maxSpeed
		EndIf
		
		'dec. lifetimer, kill the shot if it's gone on too long
		lifetimer = lifetimer - (timePass*lifetimerMod)
		If lifetimer < 0 Then death()
		
		'fade out the shot if it's about to run out of life
		If fadeTime > 0
			If lifetimer < fadeTime
				fadeAlpha = Min(1, Float(lifetimer) / Float(fadeTime) * 1.5)
				scale = scale - Max(0, timePass / Float(fadeTime))
			EndIf
		EndIf
		
		'if dependent on the owner, remove on death
		If dependent And owner.dead Then death()
		
		'face in the direction of movement
		rot = movrot
	EndMethod
	
	Method setRange(_range#)
		lifetimerMax = (_range / (speed+1)) * 1000
		lifetimer = lifetimerMax
	EndMethod
	
	Method hasTarget()
		If target = Null Then Return 0 Else Return 1
	EndMethod
	
	Method clearOwner()
		owner = Null
	EndMethod
		
	Method death()
		remove()
	EndMethod
	
	Method remove()
		'the entity class handles the rest of the cleanup
		Super.remove()
	EndMethod

EndType

	
