Item Inventory Position

Blitz3D Forums/Blitz3D Programming/Item Inventory Position

Zach3D(Posted 2006) [#1]
I fixed the old error, but im wondering whenever i Click
on an item to use it, most of the time i have to click it twice to four times.

AppTitle "Game Test"

Const width=900,height=700

Graphics3D width,height,32,2

Global T = LoadTerrain("Terrain.png")
Ttex = LoadTexture("TerrainTex.jpg")
Global Panel = LoadImage("Panel.png")
Global goldicon = LoadImage("goldicon.png")
Global poticon = LoadImage("poticon.png")
Global deathsound = LoadSound("death.wav")
Global losesound = LoadSound("lose.wav")
Global zapsound = LoadSound("zap.wav")
Global lvlupsound = LoadSound("lvlup.wav")
Global hitsound = LoadSound("hit.wav")
Global goldsound = LoadSound("gold.wav")
Global RESETR = True
SeedRnd MilliSecs()
ScaleTexture Ttex,256,256
EntityTexture T,Ttex
ScaleEntity T,2,100,2
Sky = CreateSphere(44)
ScaleEntity Sky,700,700,700
FlipMesh Sky
EntityColor Sky,180,180,245
;Setup Camera
Global Camera = CreateCamera()
CameraViewport Camera,0,0,width,height
CameraRange Camera,1,9000


;======================================
;Setup Player
;======================================



Type Player
	Field model,hp,attack,maxhp,experience,nextlvl,lvl,maxattack,cooled,Current#,maxtime#,gold
End Type

player.Player = New Player
player\lvl = 1
player\nextlvl = 100
player\experience = 0
player\model = CreateSphere(44)
EntityColor player\model,255,105,0

MoveEntity player\model,50,0,50
player\hp = 15
player\maxhp = 15
player\attack = 1
player\maxattack = 2
player\cooled = True
player\maxtime# = 3
player\current# = 3.0
player\gold = 0


;======================================
;Update Hostile
;======================================
Global IDLE,CONFRONT,DEAD,FLEE
IDLE = 1
CONFRONT = 2
DEAD = 3
FLEE = 4

Type Hostile
	Field model,hp,attack,attackrange,state,target.player,ox,oz,fleepivot,cooled,Current#,maxtime#
End Type

Function CreateHostile.hostile(spec,x,z)
		A.hostile = New hostile
		
		Select spec
			Case 1
				A\model = CreateSphere(22)
				A\hp = 5
				A\attack = 2
				A\attackrange = 20
				A\state = IDLE
				A\maxtime# = 5.0
				A\cooled = True
				A\current# = 0.0
		End Select
		
		A\ox = x
		A\oz = z
		A\fleepivot = CreatePivot()
		PositionEntity A\fleepivot,x,TerrainY(t,x,0,z),z
		PositionEntity A\model,x,0,z
		Return A
				
End Function

Function UpdateHostile()
	
	For A.hostile = Each hostile
		A\current# = A\current# + 0.1
		If A\maxtime# <= A\current#
			A\cooled = True
			A\current# = 0.0
		EndIf
		PositionEntity A\model,EntityX(A\model),3+TerrainY(T,EntityX(A\model),0,EntityZ(a\model)),EntityZ(A\model)
		If A\hp < 1
			A\state = DEAD
			For P.player = Each player
				P\experience = P\experience + 15
			Next
		EndIf
		Select A\state
			Case IDLE
				For P.player = Each player
					If EntityDistance(A\model,P\model) < A\attackrange And P\hp > 0
						A\target.player = P
						A\state = CONFRONT
					EndIf
				Next
			Case CONFRONT
				If EntityDistance(A\model,A\target\model) < 4
					If A\cooled = True
						A\target\hp = A\target\hp - A\attack
						A\cooled = False
					
							If A\target\hp < 1
								A\state = flee
							EndIf
					EndIf
				Else
					PointEntity A\model,A\target\model
					MoveEntity A\model,0,0,1
				EndIf
				
				If (Abs(EntityX(A\model) + EntityZ(A\model)- A\ox-A\oz)) > 120
					A\state = FLEE
				EndIf
			Case DEAD
				Display("Gray Ball has died.",500,35,255,0,0,5)
				PlaySound deathsound
				CreateItem(Rand(1,2),EntityX(A\model),EntityZ(A\model))
				FreeEntity A\model
				Delete A
			Case FLEE
				PointEntity A\model,A\fleepivot
				MoveEntity A\model,0,0,1
				
				If EntityDistance(A\model,A\fleepivot) < 3
					A\state = IDLE
				EndIf
		End Select
		

		
		
	Next
	
End Function
;======================================
;Setup Items
;======================================
Type Item
	Field model,amount,name$
End Type

Function CreateItem.Item(spec,x,z)
	
	A.Item = New Item
	Select spec
		Case 1
			A\model = CreateCone()
			EntityColor A\model,255,0,0
			A\amount = 5
			A\name$ = "Potion I"
		Case 2
			A\model = CreateCylinder()
			EntityColor A\model,205,160,0
			ScaleEntity A\model,1,.01,1
			RotateEntity A\model,90,0,0
			A\amount = Rand(1,4)
			A\name$ = "Gold coins"
	End Select
	
	PositionEntity A\model,x,3+TerrainY(T,x,0,z),z
	EntityShininess A\model,1
	Return A
End Function

Function UpdateItem()
	
	For A.Item = Each Item
		
		For P.player = Each player
			If EntityDistance(P\model,A\model) < 2
				PickUpItem(A)
			EndIf
		Next
		
		
	Next
	.skip
End Function	

;======================================
For X = 1 To 9
	I.item_v = New item_v
	I\slot = x:I\state = Empty()
	I\img = CreateImage(25,25)
Next



;======================================
;======================================
;======================================
;======================================
;======================================
;======================================
;======================================

While Not KeyDown(1)
	
	Cls
	
	If RESETR = True
		RESETR = False
		CreateHostile(1,25,25)
		CreateItem(1,50,25)
		CreateItem(1,25,50)

		For X = 1 To 25
			CreateHostile(1,65+Rand(0,200),65+(Rand(0,200)))
		Next
	EndIf
	
	UpdateCamera(Camera,player\model)
	UpdateHostile()
	UpdateItem()
	UpdatePlayer()
	
	If MouseHit(1)
		DebugLog MouseX() + "," + MouseY()
	EndIf
	
	
	UpdateWorld
	RenderWorld
	UpdateText()
	
	Color 0,0,0
	Rect 495,32,150,15
	Color 100,100,100
	Rect 500,35,150,13
	
	Color 0,0,0
	Rect 24,24,102,7
	Color 255,0,0
	Rect 25,25,player\hp*(100.0/player\maxhp),5
	
	Text 25,10,"Health"
	Text 25,55,"Press A while next to an enemy (gray ball) to attack."
	Text 25,65,"The red cones restore health."
	DrawImage Panel,750,0
	UpdateInventory()
	Text 807,418,player\gold
	Color 0,0,0
	Rect 0,100,150,500
	Color 255,0,0
	Text 5,100,"        Level " + player\lvl
	Text 5,115,"HP: " + player\hp + "/" + player\maxhp
	Text 5,130,"Dmg: " + player\attack + " - " + player\maxattack
	Text 5,150,"Exp: " + player\experience + "/" + player\nextlvl
	Flip
Wend

End
;======================================
;======================================
;======================================
;======================================
;======================================
;======================================

Function UpdateCamera(cam,pID)

		If KeyDown(200)
			MoveEntity pID,0,0,0.45
		EndIf
		
		If KeyDown(203)
			TurnEntity pID,0,1,0
		EndIf
		
		If KeyDown(208)
			MoveEntity pID,0,0,-0.45
		EndIf
		
		If KeyDown(205)
			TurnEntity pID,0,-1,0
		EndIf
		
		PositionEntity pID,EntityX(pID),3+TerrainY(T,EntityX(pID),0,EntityZ(pID)),EntityZ(pID)
		PositionEntity cam,EntityX(pID),EntityY(pID),EntityZ(pID)
		RotateEntity cam,EntityRoll(pID),EntityYaw(pID),EntityPitch(pID)
		MoveEntity cam,0,8,-16

End Function
;================================================
;================================================
;================================================
Function UpdatePlayer()

	For P.player = Each player
		P\current# = P\current# + 0.1
		If P\maxtime# <= P\current#
			P\cooled = True
			P\current# = 0.0
		EndIf
		If P\hp > P\maxhp
			P\hp = P\maxhp
		EndIf
		
		If P\hp < 1
			For A.Item = Each Item
				FreeEntity A\model
				Delete A
			Next
			For H.hostile = Each hostile
				FreeEntity H\model
				Delete H
			Next
			
			RESETR = True
			Cls
			Flip
			Color 255,0,0
			Text 200,200,"You have died!"
			PlaySound losesound
			Flip
			Delay(500)
			Text 200,250,"Reloading the game.."
			Flip
			P\hp = P\maxhp
			PositionEntity P\model,50,0,50
			RotateEntity P\model,0,0,0
			Delay(2000)
		EndIf
		
		If KeyHit(30)
			For H.hostile = Each hostile
				If EntityDistance(H\model,P\model) < 5 And P\cooled = True
					PlaySound hitsound
					P\cooled = False
					H\hp = H\hp - Rand(P\attack,P\maxattack)
				EndIf
			Next
		EndIf
		If P\experience >= P\nextlvl
			LevelUpPlayer(P)
		EndIf
	Next
End Function

Function LevelUpPlayer(p.player)

	Display("Level up!.",500,35,255,0,0,5)
	PlaySound lvlupsound
	p\nextlvl = p\nextlvl + (30*p\lvl)
	p\maxhp = p\maxhp + (5*p\lvl)
	p\attack = p\attack + 1
	p\maxattack = p\maxattack + 1
	p\lvl = p\lvl + 1
	p\experience = 0

End Function

;================================================
;Text Display
;================================================


Type DisplayText
	Field x,y,r,g,b,tex$,current#,max#
End Type

Function Display.DisplayText(tex$,x,y,r,g,b,time#)
	
	Te.DisplayText = New DisplayText
	Te\x = x
	Te\y = y
	Te\r = r
	Te\g = g
	Te\b = b
	Te\current# = 0.0
	Te\max# = time#
	Te\tex$ = tex$
	Return Te
	
End Function


Function UpdateText()
	
	For Te.DisplayText = Each DisplayText
		Te\current# = Te\current# + .1
		R = ColorRed()
		G = ColorGreen()
		B = ColorBlue()
		Color Te\r,Te\g,Te\b
		Text Te\x,Te\y,Te\tex$
		Color R,G,B
		If Te\current# >= Te\max#
			Delete Te
		EndIf
	Next
	
End Function


;=================================================
;Inventory
;=================================================

Type Item_V
	Field slot,img,x,y,name$,tooltip$,state,increase$,amount
End Type

;Type Inventory
;	Field I1.item_v,I2.item_v,I3.item_v,i4.item_v,i5.item_v,i6.item_v,i7.item_v,i8.item_v,i9.item_v
;End Type

Function Empty():Return False:End Function
Function Full():Return True:End Function

Function MouseOverItem_V(I.Item_V)

	If MouseX() > I\x And MouseY() > I\y
		If MouseX() < I\x + 50 And MouseY() < I\y + 50
			Return True
		Else
			Return False
		EndIf
	EndIf
	
End Function


Function UpdateInventory()

	For A.Item_V = Each Item_V
		If A\state = Full()
			DrawImage A\img,A\x,A\y
			If MouseOverItem_V(A)
				Text 763,465,A\name$
				Text 763,480,A\tooltip$
	  		EndIf
		EndIf
    Next	
	
	If MouseHit(1)
		For A.Item_V = Each Item_V
			If MouseOverItem_V(A)
				UseItem(slot)
			EndIf
		Next
	EndIf
	
End Function


Function PickupItem(A.item)

Done = False
For I.item_v = Each item_v
	If I\state = Empty() And Done = False
		Done = True
		Select A\name$
			Case "Potion I"
				I\increase$ = "health"
				I\amount = 5
				I\img = poticon
				I\name$ = "Potion I"
				I\tooltip$ = "Heals for 5 health."
			Case "Gold coins"
				I\increase$ = "gold"
				I\amount = Rand(1,4)
				I\img = goldicon			
				I\name$ = "Gold coins"
				I\tooltip$ = I\amount + " gold coins."
		End Select
		I\state = Full()
		I\x = GetItem_VX(I\slot)
		I\y = GetItem_VY(I\slot)
		FreeEntity A\model
		Delete A
	EndIf
Next

End Function 

Function UseItem(slot)

For C.Item_V = Each Item_V
	If MouseOverItem_V(C) And C\state = Full()
		Select C\name$
			Case "Gold coins"
				For P.player = Each player
					p\gold = p\gold + C\amount
				Next
			Case "Potion I"
				For P.player = Each player
						P\hp = P\hp + C\amount
				Next
		End Select
		C\state = Empty()
		C\increase$ = ""
		C\amount = 0
		C\img = 0
		C\name$ = ""
		C\tooltip$ = ""
	EndIf
Next

End Function
		
		
		
Function GetItem_VX(slot)

Select Slot
	Case 1
		Return 759
	Case 2
		Return 813
	Case 3
		Return 864
	Case 4
		Return 759
	Case 5
		Return 811
	Case 6
		Return 863
	Case 7
		Return 758
	Case 8
		Return 809
	Case 9
		Return 858
End Select

End Function


Function GetItem_VY(slot)

Select Slot
	Case 1
		Return 549
	Case 2
		Return 549
	Case 3
		Return 549
	Case 4
		Return 599
	Case 5
		Return 599
	Case 6
		Return 599
	Case 7
		Return 649
	Case 8
		Return 649
	Case 9
		Return 649
End Select

End Function



b32(Posted 2006) [#2]
It could be that there is two mousehits pro loop, one is for debugging the mouse position, the other one is for using the items.


Ross C(Posted 2006) [#3]
I thought you could only effectively use MouseHit() once per loop effectively? I usually use MouseDown() with a timer to stop it thinking your constantly clicking.


Zach3D(Posted 2006) [#4]
Ok I fixed it anyways .. it seems that every time I post a topic, i figure out whats wrong a minute later, before anyone replies.