
// Project: Herc 
// Created: 2017-04-18
// By Michael Bigler

// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle( "Herc" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts

SetCameraPosition(1,0,420,-420)
SetCameraRotation(1,50,0,0)

//SetCameraPosition(1,0,120,-420)
//SetCameraRotation(1,20,0,0)

CreatePointLight(1,-250,100,250,500,350,100,100)
CreatePointLight(2,250,100,250,500,350,350,100)
CreatePointLight(3,250,100,-250,500,100,350,100)
CreatePointLight(4,-250,100,-250,500,100,100,350)
SetAmbientColor (180,160,140)


GroundX=-250
GroundZ=250
LoadImage(500,"\media\tile1.png")
For Ground = 500 to 620
CreateObjectBox(Ground,50,1,50)
SetObjectPosition(Ground,GroundX,-30,GroundZ)
SetObjectColor(Ground,100,100,100,0)
SetObjectImage(Ground,500,0)
GroundX=GroundX+50
if GroundX>250
	GroundX=-250
	GroundZ=GroundZ-50
endif
next Ground

Global Player=100
LoadImage(Player,"\media\herc.png")
//LoadObjectWithChildren(Player,"\media\1.fbx")
LoadObjectWithChildren(Player,"\media\herc2.dae")
PlayObjectAnimation(Player,"",0,1.9,1,0.15)
SetObjectAnimationSpeed(Player,1.2)
SetObjectScalePermanent(Player,0.5,0.5,0.5)
RotateObjectLocalY(Player,180)
FixObjectPivot(Player)
SetObjectImage(Player,Player,0)

Shadow=150
LoadImage(Shadow,"\media\shadow.png")
CreateObjectPlane(Shadow,70,70)
SetObjectImage(Shadow,Shadow,0)
RotateObjectLocalX(Shadow,90)
SetObjectTransparency(Shadow,1)
SetObjectColor(Shadow,10,10,10,100)

Shadow_2=151
CreateObjectPlane(Shadow_2,140,140)
SetObjectImage(Shadow_2,Shadow,0)
RotateObjectLocalX(Shadow_2,90)
SetObjectTransparency(Shadow_2,1)
SetObjectColor(Shadow_2,10,10,10,100)



Global Target=200
CreateObjectBox(Target,49,1,49)
SetObjectColor (Target,0,200,0,0)
SetObjectPosition (Target,0,-27.6,0)
SetObjectImage(Target,Shadow,0)
SetObjectTransparency(Target,1)

TargetX=0
TargetZ=0

Block=300
CreateObjectBox(Block,50,50,50)
SetObjectPosition(Block,-100,-5,0)
SetObjectColor(Block,80,80,80,0)
SetObjectImage(Block,500,0)



Global Walk=0
do
    SetObjectPosition (Shadow,GetObjectX(Player),-28.5 , GetObjectZ(Player)-3)
    SetObjectPosition (Shadow_2,GetObjectX(Block),-28.5 , GetObjectZ(Block)-3)

	Click3dObject()
	
	if Walk=1
		PlayObjectAnimation(Player,"",1.95,2.95,1,0.05) //Walking Anim settings
		If GetObjectY(Player)<0 then SetObjectPosition(Player,GetObjectX(Player),0,GetObjectZ(Player))
		Walk=2
	endif
	if Walk=0
		PlayObjectAnimation(Player,"",0,1.9,1,0.15) //Idle Anim Settings
		Walk=3
		If GetObjectY(Player)<0 then SetObjectPosition(Player,GetObjectX(Player),0,GetObjectZ(Player))
	endif	
	if Walk=4
		PlayObjectAnimation(Player,"",3,4.5,1,0.05) //Push Anim Settings
		If GetObjectY(Player)=0 then SetObjectPosition(Player,GetObjectX(Player),-5,GetObjectZ(Player))
		Walk=3
	endif
	
	If GetObjectX(Player) > GetObjectX(Target)+1 or GetObjectX(Player) < GetObjectX(Target)-1 or GetObjectZ(Player) > GetObjectZ(Target)+1 or GetObjectZ(Player) < GetObjectZ(Target)-1 
		if Walk=2 then MoveObjectLocalZ(Player,2.5)
		if Walk=3 then MoveObjectLocalZ(Player,1.5)
	else
		If Walk=2 then Walk=0
		If Walk=3 then Walk=0
	endif
	
	If GetObjectX(Player) > GetObjectX(Block)-49 and GetObjectX(Player) < GetObjectX(Block)+49 and GetObjectZ(Player) > GetObjectZ(Block)-5 and GetObjectZ(Player) < GetObjectZ(Block)+5 
		SetObjectColor (Block,150,100,100,0)
		if Walk <> 3 then Walk=4
		
		If GetObjectX(Player) > GetObjectX(Block) then MoveObjectLocalX(Block,-1.5)
		If GetObjectX(Player) < GetObjectX(Block) then MoveObjectLocalX(Block,1.5)
	else 
		SetObjectColor (Block,80,80,80,0)
		//Walk=2	
	endif
	
	
    Print( ScreenFPS() )
    Sync()
loop

function Click3dObject()
	
	for Away = 1 to 499
		if GetObjectExists(Away)=1 then SetObjectPosition(Away,GetObjectX(Away),GetObjectY(Away)-200,GetObjectZ(Away))
	next Away
	
	myX as float
	myY as float
	my3dX as float
	my3dY as float
	my3dZ as float
	rayStartX as float
	rayStartY as float
	rayStartZ as float
	rayEndX as float
	rayEndY as float
	rayEndZ as float 
	
	myX=GetPointerX()
	myY=GetPointerY()
	
	my3dX=Get3DVectorXFromScreen(myX,myY)
	my3dY=Get3DVectorYFromScreen(myX,myY)
	my3dZ=Get3DVectorZFromScreen(myX,myY)
	
	rayStartX=my3dX+GetCameraX(1)
	rayStartY=my3dY+GetCameraY(1)
	rayStartZ=my3dZ+GetCameraZ(1)
	
	rayEndX=900*my3dX+GetCameraX(1)
	rayEndY=900*my3dY+GetCameraY(1)
	rayEndZ=900*my3dZ+GetCameraZ(1) 
	
	theObjectHit= ObjectRayCast(0,rayStartX,rayStartY,rayStartZ,rayEndX,rayEndY,rayEndZ)
	
	For Ground = 500 to 620
		
		If GetObjectX(Player)> GetObjectX(Ground)-25 and GetObjectX(Player)< GetObjectX(Ground)+25 or GetObjectZ(Player) > GetObjectZ(Ground)-25 and GetObjectZ(Player) < GetObjectZ(Ground)+25 
			SetObjectColor (Ground,100,120,100,0)
		else 
		SetObjectColor (Ground,100,100,100,0)	
		endif
		
	next Ground
for Away = 1 to 499
		if GetObjectExists(Away)=1 then SetObjectPosition(Away,GetObjectX(Away),GetObjectY(Away)+200,GetObjectZ(Away))
	next Away	
	Print(theObjectHit)
	if theObjectHit>499 	
		If GetObjectX(Player)> GetObjectX(theObjectHit)-10 and GetObjectX(Player)< GetObjectX(theObjectHit)+10 or GetObjectZ(Player) > GetObjectZ(theObjectHit)-10 and GetObjectZ(Player) < GetObjectZ(theObjectHit)+10
			SetObjectColor (theObjectHit,200,0,0,0)
			If GetPointerPressed()=1
				SetObjectPosition (Target,GetObjectX(theObjectHit),-29.5 , GetObjectZ(theObjectHit))
				SetObjectLookAt( Player, GetObjectX(Target),0 , GetObjectZ(Target), 0 )
				Walk=1
			endif
		endif
	endif
	
	
	
	
endfunction
