' The entity skeleton for TBGL
' , started on 06-12-2012 by frank brübach (lionheart)
' - part two, 06-14-2012
Uses "TBGL"
Begin Const
' -- Scene IDs
%sScene = 1
' -- Entity IDs
%eCamera = 1
%eLight
%eBox
%eBox2
%eSphere
%ePlayerX
%ePlayerY
End Const
' Game statistics
Dim Score As Long, Life As Long, GameOver As Long
Dim PlayerX, PlayerY As Single
' Speed control
Dim FrameRate As Double
Life = 100
%PLAYERSHIP = 1
' Lasers
Type tLasers
X As Double
Y As Double
Z As Double
S As Double
End Type
%NUMLASERS = 10
Dim Laser(%NUMLASERS) As tLasers
Dim LaserIndex As Long
' Obstacles to shoot
Type tObstacles
X As Double
Y As Double
Z As Double
S As Double
End Type
%NUMOBSTACLES = 5
Dim Obstacle(%NUMOBSTACLES) As tObstacles
'----------- MAIN ----------
Function TBMain() As Long
Local hWnd As DWord, i As Long
Local FrameRate As Double
MsgBox 0, "shoot with <space> key, rotate <left>, <right> move: <up> <down> key"
' -- Create and show window
hWnd = TBGL_CreateWindowEx("TBGL Shoot script 1b_exp - press ESC to quit", 740, 580, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
' -- Create scene
TBGL_SceneCreate(%sScene)
' -- Create basic entities
' -- Create camera to look from 15, 15, 15 to 0, 0, 0
TBGL_EntityCreateCamera(%sScene, %eCamera,%ePlayerX)
TBGL_EntitySetPos(%sScene, %eCamera, 1, 1, -34)
'TBGL_EntitySetTargetPos(%sScene, %eCamera,0,0,0)
TBGL_EntityCreateDLSlot(%sScene, %ePlayerX, 0, 0)
TBGL_EntitySetPos(%sScene, %ePlayerX, 0, 0, 0)
TBGL_EntityCreateDLSlot(%sScene, %ePlayerY, 0, 0)
TBGL_EntitySetPos(%sScene, %ePlayerY, 2, 1, 0)
' -- Create point light
TBGL_EntityCreateLight(%sScene, %eLight)
TBGL_EntitySetPos(%sScene, %eLight, 4, 10, -35)
' -- Create something to look at
TBGL_EntityCreateBox(%sScene, %eBox, 0, 2, 2, 2, 0, 255, 0, 0)
TBGL_EntitySetPos(%sScene, %eBox, 0, 0, 0)
For i = 1 To %NUMOBSTACLES
initObstacle(i)
Next
' -- Resets status of all keys
TBGL_ResetKeyState()
' -- Main loop
While TBGL_IsWindow(hWnd)
FrameRate = TBGL_GetFrameRate
TBGL_ClearFrame
TBGL_SceneRender(%sScene)
RenderLasers
TBGL_DrawFrame
If TBGL_GetWindowKeyOnce( hWnd, %VK_SPACE) Then Shoot()
If TBGL_GetWindowKeyState( hWnd, %VK_ESCAPE) Or GameOver = 1 Then Exit While
If TBGL_GetWindowKeyState(hWnd, %VK_LEFT) Then
TBGL_EntityTurn(%sScene, %ePlayerX, 0,-90/FrameRate, 0)
TBGL_EntityTurn(%sScene, %ePlayerX, 0,0, -90/FrameRate)
ElseIf TBGL_GetWindowKeyState(hWnd, %VK_RIGHT) Then
TBGL_EntityTurn(%sScene, %ePlayerX, 0,90/FrameRate, 0)
TBGL_EntityTurn(%sScene, %ePlayerX, 0,0,90/FrameRate)
End If
If TBGL_GetWindowKeyState(hWnd, %VK_LEFT) Then
TBGL_EntityTurn(%sScene, %eBox2, 0,-90/FrameRate, 0)
ElseIf TBGL_GetWindowKeyState(hWnd, %VK_RIGHT) Then
TBGL_EntityTurn(%sScene, %eBox2, 0, 90/FrameRate, 0)
End If
If TBGL_GetWindowKeyState( hWnd, %VK_UP) Then
TBGL_EntityPush(%sScene, %eBox2, 5/FrameRate,0, 0)
TBGL_EntityPush(%sScene, %ePlayerX, 5/FrameRate,0, 0)
End If
If TBGL_GetWindowKeyState( hWnd, %VK_DOWN) Then
TBGL_EntityPush(%sScene, %eBox2, -5/FrameRate,0, 0)
TBGL_EntityPush(%sScene, %ePlayerX, -5/FrameRate,0, 0)
End If
Wend
TBGL_DestroyWindow
End Function
' Lasers ' ok -------------------------------------------
Function Shoot() As Long
Incr LaserIndex
If LaserIndex > %NUMLASERS Then LaserIndex = 1
Laser(LaserIndex).x = %ePlayerX-6
Laser(LaserIndex).y = %ePlayerY-7
Laser(LaserIndex).z = 0'-1
Laser(LaserIndex).s = 15
End Function
'render laser ok ? ----------------
Function RenderLasers() As Long
Local i As Long, j As Long
TBGL_Color 0,255,0
TBGL_LineWidth 3
For i = 1 To %NUMLASERS
If Laser(i).s = 0 Then Exit For
Laser(i).z = Laser(i).z + Laser(i).s / 60 'FrameRate ''tbgl_GetFrameRate
TBGL_PushMatrix
TBGL_Translate Laser(i).x, Laser(i).y, Laser(i).z
TBGL_BeginPoly %GL_LINES
TBGL_Vertex 0,0,0
TBGL_Vertex 0,0,-1
TBGL_EndPoly
TBGL_PopMatrix
' Very simple collison detection with the boxes
For j = 1 To %NUMOBSTACLES
If Laser(i).x < Obstacle(j).x+1 And Laser(i).x > Obstacle(j).x-1 And _
Laser(i).z < Obstacle(j).z+1 And Laser(i).z > Obstacle(j).z-1 And _
Laser(i).y < Obstacle(j).y+1 And Laser(i).y > Obstacle(j).y-1 Then
initObstacle(j)
Score = Score + 100
End If
Next
Next
End Function
'############################################################################
'---------------------------------------------------- obstacles not ok ------
'Obstacles ''
Function InitObstacle( index As Long ) As Long
'Obstacle(index).x = Rnd(-2,2)
'Obstacle(index).y = Rnd(-2,2)
'Obstacle(index).z = Rnd(-10,+20)
'Obstacle(index).s = Rnd(5,7)
End Function
Function RenderObstacles() As Long
Local i As Long,hwnd As Long
'TBGL_PolygonLook IIf(TBGL_GetWindowKeyState(hWnd, %VK_TAB), %GL_LINE, %GL_FILL)
'---Script will run on different PCs so we must assure
'---constant speed of movement by scaling movement relative to framerate
'FrameRate = TBGL_GetFrameRate
TBGL_Color 128,0,0
For i = 1 To %NUMOBSTACLES
Obstacle(i).z = Obstacle(i).z + Obstacle(i).s / 60'FrameRate
If Obstacle(i).z > 0 Then initObstacle(i)
' Very simple collison detection with the player
If Obstacle(i).x < PlayerX+1 And Obstacle(i).x > PlayerX-1 And _
Obstacle(i).y < PlayerY+1 And Obstacle(i).y > PlayerY-1 And _
Obstacle(i).z > -2 Then
initObstacle(i)
Life = Life - 10
If Life <= 0 Then GameOver = 1
End If
TBGL_PushMatrix
TBGL_Translate Obstacle(i).x, Obstacle(i).y, Obstacle(i).z
TBGL_Rotate Timer*100,1,1,1
TBGL_Box 1,1,1
TBGL_PopMatrix
Next
End Function
'############################################################################
'---------------------------------------------------- obstacles not ok ------
thanks, frank
Bookmarks