Iacta alea est
Posted: Mon Nov 30, 2015 2:16 pm
Code: Select all
;Small dice example
;By DK_Peter
;------------------
EnableExplicit
InitEngine3D()
InitSprite()
InitMouse()
InitKeyboard()
Declare.i CreateDiceWorld()
Declare.f RandomF(min.f, Max.f, SeedVal.i = #PB_Ignore)
Structure _Object
id.i
ms.i
ma.i
tx.i
EndStructure
Structure _Dice
num._Object[6]
main._Object
EndStructure
Structure _movement
x.f
y.f
z.f
mx.i
my.i
CurrentLookX.f
CurrentLookZ.f
EndStructure
Global dice._Dice, Table._Object, Quit = 0, mo._movement, Ret.i, ev.i
OpenWindow(0, 0, 0, 800, 600, "Iacta alea est", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)
Add3DArchive("texture", #PB_3DArchive_FileSystem)
WorldShadows(#PB_Shadow_Additive)
CreateCamera(0, 0, 0, 100, 100)
CameraRange(0, 0, 200)
MoveCamera(0, 0, 0.5, 1.5)
CreateCamera(1, 0, 0, 20, 20)
MoveCamera(1, 0, 0, 0)
CameraFOV(1, 20)
CameraDirection(1, 0, 1, 0)
CreateLight(0, $6DAEA3, 0, 20, 0, #PB_Light_Directional)
Ret = CreateDiceWorld()
Repeat
Repeat
ev = WindowEvent()
Until ev = 0
ExamineMouse()
mo\mx = -MouseDeltaX() * 0.05
mo\my = -MouseDeltaY() * 0.05
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
mo\x = -0.05
ElseIf KeyboardPushed(#PB_Key_Right)
mo\x = 0.05
Else
mo\x = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
mo\y = -0.05
ElseIf KeyboardPushed(#PB_Key_Down)
mo\y = 0.05
Else
mo\y = 0
EndIf
If KeyboardReleased(#PB_Key_Space)
With dice\main
MoveEntity(\id, 0, 0, 0, #PB_Absolute)
ApplyEntityImpulse(\id, RandomF(-0.2, 0.2), RandomF(0.3, 1), -RandomF(0.5, 2), RandomF(0,0.2), RandomF(0,0.2), RandomF(0,0.3))
EndWith
EndIf
RotateCamera(0, mo\my, mo\mx, 0, #PB_Relative)
MoveCamera(0, mo\x, 0, mo\y)
CameraLookAt(1, mo\CurrentLookX, 0, mo\CurrentLookZ)
If GetEntityAttribute(dice\main\id, #PB_Entity_IsActive) <> 0
MoveCamera(1, EntityX(dice\main\id), 0.2, EntityZ(dice\main\id), #PB_Absolute)
mo\CurrentLookX = EntityX(dice\main\id)
mo\CurrentLookZ = EntityZ(dice\main\id)-0.005
EndIf
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
Procedure.i CreateDiceWorld()
With dice\main
\ms = CreateCube(#PB_Any, 0.1)
\tx = CreateTexture(#PB_Any, 64, 64)
StartDrawing(TextureOutput(\tx))
Box(0, 0, 64, 64, $4444FF)
StopDrawing()
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, 0)
EndWith
With dice\num[0] ; 1
\ms = CreatePlane(#PB_Any, 0.101, 0.1, 1, 1, 1, 1)
\tx = CreateTexture(#PB_Any, 128, 128)
StartDrawing(TextureOutput(\tx))
Box(0, 0, 128, 128, $0000FF)
Circle(65, 65, 10, $333333)
Circle(64, 64, 9, $777777)
StopDrawing()
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
MaterialCullingMode(\ma, #PB_Material_NoCulling)
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0.0505, 0)
EndWith
With dice\num[1] ; 2
\ms = CreatePlane(#PB_Any, 0.101, 0.1, 1, 1, 1, 1)
\tx = CreateTexture(#PB_Any, 128, 128)
StartDrawing(TextureOutput(\tx))
Box(0, 0, 128, 128, $0000FF)
Circle(33, 33, 10, $333333)
Circle(32, 32, 9, $777777)
Circle(97, 97, 10, $333333)
Circle(96, 96, 9, $777777)
StopDrawing()
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, 0.0505)
RotateEntity(\id, 90, 0, 0)
EndWith
With dice\num[2] ; 3
\ms = CreatePlane(#PB_Any, 0.101, 0.1, 1, 1, 1, 1)
\tx = CreateTexture(#PB_Any, 128, 128)
StartDrawing(TextureOutput(\tx))
Box(0, 0, 128, 128, $0000FF)
Circle(33, 33, 10, $333333)
Circle(32, 32, 9, $777777)
Circle(65, 65, 10, $333333)
Circle(64, 64, 9, $777777)
Circle(97, 97, 10, $333333)
Circle(96, 96, 9, $777777)
StopDrawing()
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), -0.0505, 0, 0)
RotateEntity(\id, 0, 0, 90)
EndWith
With dice\num[3] ; 4
\ms = CreatePlane(#PB_Any, 0.101, 0.1, 1, 1, 1, 1)
\tx = CreateTexture(#PB_Any, 128, 128)
StartDrawing(TextureOutput(\tx))
Box(0, 0, 128, 128, $0000FF)
Circle(33, 33, 10, $333333)
Circle(32, 32, 9, $777777)
Circle(33, 97, 10, $333333)
Circle(32, 96, 9, $777777)
Circle(97, 33, 10, $333333)
Circle(96, 32, 9, $777777)
Circle(97, 97, 10, $333333)
Circle(96, 96, 9, $777777)
StopDrawing()
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0.0505, 0, 0)
RotateEntity(\id, 0, 0, -90)
EndWith
With dice\num[4] ; 5
\ms = CreatePlane(#PB_Any, 0.101, 0.1, 1, 1, 1, 1)
\tx = CreateTexture(#PB_Any, 128, 128)
StartDrawing(TextureOutput(\tx))
Box(0, 0, 128, 128, $0000FF)
Circle(33, 33, 10, $333333)
Circle(32, 32, 9, $777777)
Circle(33, 97, 10, $333333)
Circle(32, 96, 9, $777777)
Circle(65, 64, 10, $333333)
Circle(64, 64, 9, $777777)
Circle(97, 33, 10, $333333)
Circle(96, 32, 9, $777777)
Circle(97, 97, 10, $333333)
Circle(96, 96, 9, $777777)
StopDrawing()
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, -0.0505)
RotateEntity(\id, -90, 0, 0)
EndWith
With dice\num[5] ; 6
\ms = CreatePlane(#PB_Any, 0.101, 0.1, 1, 1, 1, 1)
\tx = CreateTexture(#PB_Any, 128, 128)
StartDrawing(TextureOutput(\tx))
Box(0, 0, 128, 128, $0000FF)
Circle(33, 33, 10, $333333)
Circle(32, 32, 9, $777777)
Circle(33, 65, 10, $333333)
Circle(32, 64, 9, $777777)
Circle(33, 97, 10, $333333)
Circle(32, 96, 9, $777777)
Circle(97, 33, 10, $333333)
Circle(96, 32, 9, $777777)
Circle(97, 65, 10, $333333)
Circle(96, 64, 9, $777777)
Circle(97, 97, 10, $333333)
Circle(96, 96, 9, $777777)
StopDrawing()
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, -0.0505, 0)
RotateEntity(\id, 0, 0, -180)
EndWith
With Table
\ms = CreateCube(#PB_Any, 0.5)
\tx = CreateTexture(#PB_Any, 128, 128)
StartDrawing(TextureOutput(\tx))
Box(0, 0, 128, 128, $24631F)
DrawingMode(#PB_2DDrawing_Outlined)
Box(2, 2, 124, 124, $999999)
StopDrawing()
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, -1, -5)
ScaleEntity(\id, 30, 0.2, 40)
CreateEntityBody(\id, #PB_Entity_BoxBody, 0, 1, 55)
EndWith
With dice\main
AttachEntityObject(\id, "", EntityID(dice\num[0]\id))
AttachEntityObject(\id, "", EntityID(dice\num[1]\id))
AttachEntityObject(\id, "", EntityID(dice\num[2]\id))
AttachEntityObject(\id, "", EntityID(dice\num[3]\id))
AttachEntityObject(\id, "", EntityID(dice\num[4]\id))
AttachEntityObject(\id, "", EntityID(dice\num[5]\id))
CreateEntityBody(\id, #PB_Entity_BoxBody, 0.4 , 0.3, 25.5)
EndWith
ProcedureReturn #True
EndProcedure
Procedure.f RandomF(min.f, Max.f, SeedVal.i = #PB_Ignore)
If SeedVal = #PB_Ignore : SeedVal = ElapsedMilliseconds() : EndIf
ProcedureReturn (Min + (Max - Min) * Random(SeedVal) / SeedVal)
EndProcedure