8439b2900aaeb4bfa76b2c49dc70e78cfd725956
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the Do What The Fuck You Want To
5 # Public License, Version 2, as published by Sam Hocevar. See
6 # http://sam.zoy.org/projects/COPYING.WTFPL for more details.
8 # Template for a 2D gamnit game
10 app_name
"gamnit template"
11 app_namespace
"org.gamnit.template"
12 app_version
(0, 1, git_revision
)
16 import gamnit
::flat
# For `Texture, Sprite`, etc.
17 import gamnit
::keys
# For `pressed_keys`
18 import app
::audio
# For `Sound`
22 # Texture, loaded automatically at `on_create`
23 var texture
= new Texture("fighter.png")
25 # Sound effect, lazy loaded at first use
26 var sound
= new Sound("laser.mp3")
28 # Sprite, must be loaded in or after `on_create`
29 var sprite
= new Sprite(texture
, new Point3d[Float](0.0, 0.0, 0.0)) is lazy
35 # Report errors on all loaded textures.
36 # Root textures are associated to pixel data,
37 # whereas other texture may be subtextures of root textures.
38 for tex
in all_root_textures
do
40 if error
!= null then print_error
"Texture '{tex}' failed to load: {error}"
43 # Draw the texture as pixelated, it looks better for such
45 texture
.as(TextureAsset).pixelated
= true
47 # Create the sprite and make it visible.
50 # Make the sprite smaller, by default each pixel corresponds to 1 world unit.
51 # However, it is often preferable to make 1 world unit correspond to
52 # something meaningful in the game world, such as 1 meter.
54 # Scale the ship so it is approximately 5 world units wide.
55 sprite
.scale
= 5.0 / texture
.width
57 # Move the camera to show 10 world units on the Y axis at Z = 0.
58 # The `sprite` should take approximately 1/4 of the height of the screen.
59 world_camera
.reset_height
20.0
61 # Move the near clip wall closer to the camera because our world unit
62 # range is small. Moving the clip wall too close to the camera can
63 # cause glitches on mobiles devices with small depth buffer.
64 world_camera
.near
= 1.0
66 # Make the background blue and opaque.
67 glClearColor
(0.0, 0.0, 1.0, 1.0)
69 # If the first command line argument is an integer, add extra sprites.
70 if args
.not_empty
and args
.first
.is_int
then
71 # It's a performance test, unlock the framerate.
74 # Add `args.first` sprites.
75 for i
in args
.first
.to_i
.times
do
76 var s
= new Sprite(texture
, new Point3d[Float](30.0.rand
- 15.0, 20.0.rand
- 10.0, 0.0 - 30.0.rand
))
85 # Update game logic here.
86 sprite
.rotation
+= 0.1*pi
*dt
88 # Move `sprite` with the keyboard arrows.
89 # Set the speed according to the elapsed time since the last frame `dt`
90 # for a smooth animation.
91 var unit_per_second
= 2.0
92 for key
in pressed_keys
do
94 sprite
.center
.x
-= unit_per_second
*dt
95 else if key
== "right" then
96 sprite
.center
.x
+= unit_per_second
*dt
97 else if key
== "up" then
98 sprite
.center
.y
+= unit_per_second
*dt
99 else if key
== "down" then
100 sprite
.center
.y
-= unit_per_second
*dt
105 redef fun accept_event
(event
)
107 if super then return true
109 if event
isa QuitEvent or
110 (event
isa KeyEvent and event
.name
== "escape" and event
.is_up
) then
111 # When window close button, escape or back key is pressed
112 # show the average FPS over the last few seconds.
113 print
"{current_fps} fps"
118 else if event
isa KeyEvent and event
.is_down
then
119 if event
.name
== "space" then
120 # Play a sound when space bar is pressed.
123 else if event
.name
== "s" then
124 # Remove a random sprite.
125 if sprites
.not_empty
then sprites
.remove sprites
.rand
126 else if event
.name
== "w" then
127 # Add a random sprite.
128 var s
= new Sprite(texture
, new Point3d[Float](30.0.rand
- 15.0, 20.0.rand
- 10.0, 0.0 - 30.0.rand
))