Professional Documents
Culture Documents
Castle Game Engine Tutorial Slides
Castle Game Engine Tutorial Slides
net/publication/303313651
CITATIONS READS
0 571
1 author:
Michalis Kamburelis
10 PUBLICATIONS 7 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Michalis Kamburelis on 18 May 2016.
Outline
1 Basics
Introduction
Let’s Run Some Stuff
5-minute Programming Crash Course
2 Creating Games
First 3D Application
Creating FPS 3D Game
Creating 2D Game
3 More!
Conclusion
Android
Questions?
Basics Creating Games More!
Outline
1 Basics
Introduction
Let’s Run Some Stuff
5-minute Programming Crash Course
2 Creating Games
First 3D Application
Creating FPS 3D Game
Creating 2D Game
3 More!
Conclusion
Android
Questions?
Basics Creating Games More!
Outline
1 Basics
Introduction
Let’s Run Some Stuff
5-minute Programming Crash Course
2 Creating Games
First 3D Application
Creating FPS 3D Game
Creating 2D Game
3 More!
Conclusion
Android
Questions?
Basics Creating Games More!
Rendering
If you have a laptop, follow us and create your own games right
now too!
Download:
Lazarus http://www.lazarus-ide.org/
Castle Game Engine http:
//castle-engine.sourceforge.net/engine.php
Example data https:
//github.com/michaliskambi/cge-tutorial
Basics Creating Games More!
Game Data
Engine API
Engine Docs
Lazarus
Outline
1 Basics
Introduction
Let’s Run Some Stuff
5-minute Programming Crash Course
2 Creating Games
First 3D Application
Creating FPS 3D Game
Creating 2D Game
3 More!
Conclusion
Android
Questions?
Basics Creating Games More!
view3dscene
Outline
1 Basics
Introduction
Let’s Run Some Stuff
5-minute Programming Crash Course
2 Creating Games
First 3D Application
Creating FPS 3D Game
Creating 2D Game
3 More!
Conclusion
Android
Questions?
Basics Creating Games More!
Example Program
program Example1 ;
begin
Foo ( ’ aa ’ ) ;
end .
Basics Creating Games More!
var
My : TMyClass ;
begin
My : = TMyClass . Create ;
try
My . F i e l d : = ’ b l a h ’ ;
My . MyMethod ; { o r My . MyMethod ( ) ; }
f i n a l l y FreeAndNil (My) end ;
end .
Basics Creating Games More!
Example Unit
{ example u n i t . pas }
u n i t ExampleUnit ;
interface
implementation
{ example3 . l p r }
program Example3 ;
uses ExampleUnit ;
begin
Foo ( ’ aa ’ ) ; { o r ExampleUnit . Foo ( ’ aa ’ ) ; }
end .
Basics Creating Games More!
Outline
1 Basics
Introduction
Let’s Run Some Stuff
5-minute Programming Crash Course
2 Creating Games
First 3D Application
Creating FPS 3D Game
Creating 2D Game
3 More!
Conclusion
Android
Questions?
Basics Creating Games More!
Outline
1 Basics
Introduction
Let’s Run Some Stuff
5-minute Programming Crash Course
2 Creating Games
First 3D Application
Creating FPS 3D Game
Creating 2D Game
3 More!
Conclusion
Android
Questions?
Basics Creating Games More!
If you’re not sure how, just get the ZIP file and extract it:
https://github.com/michaliskambi/cge-tutorial/
archive/master.zip
Basics Creating Games More!
Data
Code
Pick TCastleControl from the component palette (tab
Castle) and drop it on a regular Lazarus form.
uses C a s t l e F i l e s U t i l s , CastleScene ;
Outline
1 Basics
Introduction
Let’s Run Some Stuff
5-minute Programming Crash Course
2 Creating Games
First 3D Application
Creating FPS 3D Game
Creating 2D Game
3 More!
Conclusion
Android
Questions?
Basics Creating Games More!
Level
Level: 3D Data
Create a 3D model for your level.
From our example data
https://github.com/michaliskambi/
cge-tutorial/archive/master.zip copy the data
from fps_game subdirectory.
Test your level by opening it in view3dscene.
Note that we use X3D Inline mechanism there:
bridge.x3d is exported from Blender,
bridge_final.x3dv is written by hand and inlines
bridge.x3d.
Basics Creating Games More!
Level: Code
Drop TCastleControl on Lazarus form.
uses C a s t l e L e v e l s ;
{ add a t t h e b e g i n n i n g o f CastleControl1Open }
Resources . LoadFromFiles ;
Basics Creating Games More!
Placeholders in Blender
Basics Creating Games More!
Player
There is no player yet (only a default camera, that does not
have inventory or other game features).
{ add t o implementation uses c l a u s e }
uses . . . , , C a s t l e P l a y e r ;
{ d e c l a r e i n class p r i v a t e s e c t i o n }
P l a y e r : TPlayer ;
{ add a t t h e b e g i n n i n g o f CastleControl1Open }
P l a y e r : = TPlayer . Create (
C a s t l e C o n t r o l 1 . SceneManager ) ;
C a s t l e C o n t r o l 1 . SceneManager . Items . Add ( P l a y e r ) ;
C a s t l e C o n t r o l 1 . SceneManager . P l a y e r : = P l a y e r ;
Basics Creating Games More!
HUD I
Items are pickable now, they are added to player’s inventory.
But the inventory is not visualized in any way. Add a simple 2D
HUD:
{ add t o implementation uses c l a u s e }
uses . . . , , C a s t l e U I C o n t r o l s ;
{ add t o implementation }
type
TGame2DControls = class ( T U I C o n t r o l )
public
procedure Render ; o v e r r i d e ;
end ;
HUD II
var
P l a y e r : TPlayer ;
I , J , X: Integer ;
begin
P l a y e r : = Form1 . P l a y e r ;
X := 0;
f o r I :=0 to P l a y e r . I n v e n t o r y . Count−1 do
f o r J :=0 to P l a y e r . I n v e n t o r y [ I ] . Q u a n t i t y −1 do
begin
P l a y e r . I n v e n t o r y [ I ] . Resource . GLImage .
Draw ( X , 0 ) ;
X += 100;
end ;
end ;
Basics Creating Games More!
HUD III
{ add t o CastleControl1Open }
var
Game2DControls : TGame2DControls ;
begin
...
Game2DControls : = TGame2DControls . Create (
Application ) ;
CastleControl1 . Controls . InsertFront (
Game2DControls ) ;
end ;
Basics Creating Games More!
Creatures
Adding a creature with a predefined AI does not require any
code at all.
We already load resources (items and creatures) by
Resources.LoadFromFiles.
This detects knight_creature/resource.xml file
inside as resource named Knight using a standard AI
called WalkAttack.
We already load the level using LoadLevel facility.
This allows to place initial creatures on level by placing
CasResKnight from Blender.
Basics Creating Games More!
Spawn Creatures I
Add a button on a form to spawn creatures. Best to use
TSpeedButton, to not capture focus.
{ add t o implementation uses c l a u s e }
uses . . . , C a s t l e V e c t o r s , C a s t l e C r e a t u r e s ;
Spawn Creatures II
CreatureResource : =
Resources . FindName ( ’ K n i g h t ’ )
as TCreatureResource ;
CreatureResource . C r e a t e C r e a t u r e (
C a s t l e C o n t r o l 1 . SceneManager . Items ,
P, Direction ) ;
end ;
Basics Creating Games More!
Kill Creatures I
Add a button to kill creature.
{ add t o implementation uses c l a u s e }
uses . . . , Castle3D ;
Kill Creatures II
i f H i t <> n i l then
begin
f o r I : = 0 to H i t . Count − 1 do
i f H i t [ I ] . Item i s T3DAlive then
begin
( H i t [ I ] . Item as T3DAlive ) . H u r t (
100 , P l a y e r . D i r e c t i o n , 1 , P l a y e r ) ;
Break ;
end ;
FreeAndNil ( H i t ) ;
end ;
end ;
Basics Creating Games More!
Shadows
Since all our 3D data is in X3D, we can do a lot of fun stuff with
X3D coding. Use Castle Game Engine X3D extensions to get
shadows, screen effects, shader effects, mirrors, and more.
For a simplest demo, enable shadows by „shadows TRUE”
on a light source.
In our sample data from
https://github.com/michaliskambi/
cge-tutorial/archive/master.zip just uncomment
relevant lines at the end of bridge_final.x3dv.
Basics Creating Games More!
{ d e c l a r e i n class p r i v a t e s e c t i o n }
Root : TX3DRootNode ;
Sun : TSpotLightNode ;
{ add t o CastleControl1Open }
Root : = C a s t l e C o n t r o l 1 . SceneManager .
MainScene . RootNode ;
Sun : = Root . FindNodeByName ( TSpotLightNode ,
’ Sun ’ , t r u e ) as TSpotLightNode ;
Basics Creating Games More!
Outline
1 Basics
Introduction
Let’s Run Some Stuff
5-minute Programming Crash Course
2 Creating Games
First 3D Application
Creating FPS 3D Game
Creating 2D Game
3 More!
Conclusion
Android
Questions?
Basics Creating Games More!
Data
Spine Peek
Basics Creating Games More!
Code I
Drop TCastle2DControl on a form.
{ add t o i n t e r f a c e uses c l a u s e }
uses . . . , C a s t l e F i l e s U t i l s , Castle2DSceneManager ;
{ d e c l a r e as form p r i v a t e f i e l d }
Background : T2DScene ;
Code II
A p p l i c a t i o n D a t a ( ’ background . x3dv ’ ) ) ;
SM : = Ca st le 2D C o n t r o l 1 . SceneManager ;
SM. Items . Add ( Background ) ;
SM. MainScene : = Background ;
SM. P r o j e c t i o n A u t o S i z e : = f a l s e ;
SM. P r o j e c t i o n H e i g h t : = 721;
end ;
Basics Creating Games More!
Dragon
Load a dragon and add it to the world. Start flying animation
immediately.
{ d e c l a r e as form p r i v a t e f i e l d }
Dragon : T2DScene ;
{ add t o FormCreate }
Dragon : = T2DScene . Create ( A p p l i c a t i o n ) ;
Dragon . Load (
A p p l i c a t i o n D a t a ( ’ dragon / dragon . j s o n ’ ) ) ;
Dragon . ProcessEvents : = t r u e ;
Dragon . Pl a yA n im a t i o n ( ’ f l y i n g ’ , paForceLooping ) ;
C a s t l e2 DC on tr o l1 . SceneManager . Items . Add ( Dragon ) ;
Basics Creating Games More!
{ d e c l a r e as p r i v a t e f i e l d i n TForm1 }
DragonTransform : T3DTransform ;
{ add t o FormCreate }
DragonTransform : =
T3DTransform . Create ( A p p l i c a t i o n ) ;
DragonTransform . Scale : =
Basics Creating Games More!
Capturing Mouse I
Let’s capture mouse clicks to know where user wants to move
the dragon.
{ d e c l a r e as p r i v a t e f i e l d i n TForm1 }
FlyingTarget : TVector2Single ;
{ i n FormCreate add }
Background . S p a t i a l : = [ ssRendering ,
ssDynamicCollisions ] ;
Background . ProcessEvents : = t r u e ;
Capturing Mouse II
begin
i f Event . IsMouseButton ( mbLeft ) and
( Background .
P o i n t i n g D e v i c e O v e r I t e m <> n i l ) then
FlyingTarget := Vector2Single (
Background . P o i n t i n g D e v i c e O v e r P o i n t [ 0 ] ,
Background . P o i n t i n g D e v i c e O v e r P o i n t [ 1 ] ) ;
end ;
Note: this was one point where we have to remember that
viewport presents the world, and world coordinates (in
Background.PointingDeviceOverPoint) are not simple mouse
position (in Event.Position).
Basics Creating Games More!
Outline
1 Basics
Introduction
Let’s Run Some Stuff
5-minute Programming Crash Course
2 Creating Games
First 3D Application
Creating FPS 3D Game
Creating 2D Game
3 More!
Conclusion
Android
Questions?
Basics Creating Games More!
Outline
1 Basics
Introduction
Let’s Run Some Stuff
5-minute Programming Crash Course
2 Creating Games
First 3D Application
Creating FPS 3D Game
Creating 2D Game
3 More!
Conclusion
Android
Questions?
Basics Creating Games More!
Outline
1 Basics
Introduction
Let’s Run Some Stuff
5-minute Programming Crash Course
2 Creating Games
First 3D Application
Creating FPS 3D Game
Creating 2D Game
3 More!
Conclusion
Android
Questions?
Basics Creating Games More!
Android
Build tool
Outline
1 Basics
Introduction
Let’s Run Some Stuff
5-minute Programming Crash Course
2 Creating Games
First 3D Application
Creating FPS 3D Game
Creating 2D Game
3 More!
Conclusion
Android
Questions?
Basics Creating Games More!
Visit us:
http://castle-engine.sourceforge.net/
Questions?