TGC Codebase Backup



FixSpriteToSprite System by GarBenjamin

21st Aug 2018 19:46
Summary

Implements the basic attachment functionality for Sprites that FixObjectToObject provides for 3D objects.



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    // ****************************************************************************************************
// FixedSpriteToSprite System
//
// Usage: FixSpriteToSprite(childSpriteID, parentSpriteID) to attach child sprite to the parent sprite
//		  This command works much like FixObjectToObject in that it attaches the child sprite based on 
//		  the position and angle of it and its parent at the time this function is called.
//		  FixSpriteToSprite(childSpriteID, 0) to detach child sprite.
//
// 	>>>	  Call FixedSpriteUpdate() each frame to automatically update the position and rotation of attached
//		  sprites based on the position and rotation of their parent.
// ****************************************************************************************************

type TFixedSprite
	sprite as integer
	parentSprite as integer
	ao as float
	r as float
endtype

global fixedsprites as TFixedSprite[]

function FixSpriteToSprite(spriteID as integer, toSpriteID as integer)
	si as integer
	i as integer
	xo as float
	yo as float

	i = -1
	// see if sprite is already fixed to a sprite
	for si=0 to fixedsprites.length
		if fixedsprites[si].sprite = spriteID
			i = si
			exit
		endif
	next si
	
	// if sprite is not currently fixed to a sprite, create a new entry for it in the array and use that as our index to update
	if i < 0 and toSpriteID > 0
		fixedsprites.length = fixedsprites.length + 1
		i = fixedsprites.length
	endif
	
	// handle detaching sprite
	if toSpriteID = 0 
		// cannot remove a child link that does not exist
		if i < 0 then exitfunction

		fixedsprites.remove(i)
		exitfunction
	endif
	
	fixedsprites[i].sprite = spriteID
	fixedsprites[i].parentSprite = toSpriteID
	xo = GetSpriteXByOffset(toSpriteID) - GetSpriteXByOffset(spriteID)
	yo = GetSpriteYByOffset(toSpriteID) - GetSpriteYByOffset(spriteID)
	fixedsprites[i].ao = atan2(yo, xo)
	fixedsprites[i].r = abs(xo + yo)
endfunction

function FixedSpriteUpdate()
	i as integer
	x as float
	y as float
	a as float
	
	if fixedsprites.length < 0 then exitfunction

	for i=0 to fixedsprites.length
		x = GetSpriteXByOffset(fixedsprites[i].parentSprite)
		y = GetSpriteYByOffset(fixedsprites[i].parentSprite)
		a = GetSpriteAngle(fixedsprites[i].parentSprite) + fixedsprites[i].ao
		SetSpritePositionByOffset(fixedsprites[i].sprite, x + (cos(a)*fixedsprites[i].r), y + (sin(a)*fixedsprites[i].r))
		SetSpriteAngle(fixedsprites[i].sprite, GetSpriteAngle(fixedsprites[i].parentSprite))
	next i
endfunction