From: Alexis Laferrière Date: Fri, 17 Mar 2017 02:42:38 +0000 (-0400) Subject: gamnit: intro a template project for 2D games X-Git-Url: http://nitlanguage.org gamnit: intro a template project for 2D games Signed-off-by: Alexis Laferrière --- diff --git a/lib/gamnit/examples/template_flat/Makefile b/lib/gamnit/examples/template_flat/Makefile new file mode 100644 index 0000000..61bfc25 --- /dev/null +++ b/lib/gamnit/examples/template_flat/Makefile @@ -0,0 +1,8 @@ +all: bin/template_flat bin/template_flat.apk + +bin/template_flat: $(shell nitls -M src/template_flat.nit linux) + nitc --debug src/template_flat.nit -m linux -o $@ + +android: bin/template_flat.apk +bin/template_flat.apk: $(shell nitls -M src/template_flat.nit android) + nitc src/template_flat.nit -m android -o $@ diff --git a/lib/gamnit/examples/template_flat/README.md b/lib/gamnit/examples/template_flat/README.md new file mode 100644 index 0000000..3ab2dbe --- /dev/null +++ b/lib/gamnit/examples/template_flat/README.md @@ -0,0 +1,6 @@ +Template for a 2D _gamnit_ game + +This project can be copied and modified to use as starting point for a new _gamnit_ 2D game +using the `flat` API. + +Both assets are published under CC0. diff --git a/lib/gamnit/examples/template_flat/assets/fighter.png b/lib/gamnit/examples/template_flat/assets/fighter.png new file mode 100644 index 0000000..d5f3ee4 Binary files /dev/null and b/lib/gamnit/examples/template_flat/assets/fighter.png differ diff --git a/lib/gamnit/examples/template_flat/assets/laser.mp3 b/lib/gamnit/examples/template_flat/assets/laser.mp3 new file mode 100644 index 0000000..d1f2ba2 Binary files /dev/null and b/lib/gamnit/examples/template_flat/assets/laser.mp3 differ diff --git a/lib/gamnit/examples/template_flat/bin/.gitignore b/lib/gamnit/examples/template_flat/bin/.gitignore new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/lib/gamnit/examples/template_flat/bin/.gitignore @@ -0,0 +1 @@ +* diff --git a/lib/gamnit/examples/template_flat/package.ini b/lib/gamnit/examples/template_flat/package.ini new file mode 100644 index 0000000..103ccd7 --- /dev/null +++ b/lib/gamnit/examples/template_flat/package.ini @@ -0,0 +1,11 @@ +[package] +name=template_flat +tags=game,example +maintainer=Alexis Laferrière +license=WTFPL +[upstream] +browse=https://github.com/nitlang/nit/tree/master/lib/gamnit/examples/template_flat/ +git=https://github.com/nitlang/nit.git +git.directory=lib/gamnit/examples/template_flat/ +homepage=http://nitlanguage.org +issues=https://github.com/nitlang/nit/issues diff --git a/lib/gamnit/examples/template_flat/src/template_flat.nit b/lib/gamnit/examples/template_flat/src/template_flat.nit new file mode 100644 index 0000000..b15d6b8 --- /dev/null +++ b/lib/gamnit/examples/template_flat/src/template_flat.nit @@ -0,0 +1,139 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the Do What The Fuck You Want To +# Public License, Version 2, as published by Sam Hocevar. See +# http://sam.zoy.org/projects/COPYING.WTFPL for more details. + +# Template for a 2D gamnit game +module template_flat is + app_name "gamnit 2D Template" + app_namespace "net.xymus.template_flat" + app_version(1, 0, git_revision) + + android_api_target 15 +end + +import gamnit::flat # For `Texture, Sprite`, etc. +import gamnit::keys # For `pressed_keys` +import app::audio # For `Sound` + +redef class App + + # Texture, loaded automatically at `on_create` + var texture = new Texture("fighter.png") + + # Sound effect, lazy loaded at first use + var sound = new Sound("laser.mp3") + + # Sprite, must be loaded in or after `on_create` + var sprite = new Sprite(texture, new Point3d[Float](0.0, 0.0, 0.0)) is lazy + + redef fun on_create + do + super + + # Report errors on all loaded textures. + # Root textures are associated to pixel data, + # whereas other texture may be subtextures of root textures. + for tex in all_root_textures do + var error = tex.error + if error != null then print_error "Texture '{tex}' failed to load: {error}" + end + + # Draw the texture as pixelated, it looks better for such + # a small texture. + texture.as(TextureAsset).pixelated = true + + # Create the sprite and make it visible. + sprites.add sprite + + # Make the sprite smaller, by default each pixel corresponds to 1 world unit. + # However, it is often preferable to make 1 world unit correspond to + # something meaningful in the game world, such as 1 meter. + # + # Scale the ship so it is approximately 5 world units wide. + sprite.scale = 5.0 / texture.width + + # Move the camera to show 10 world units on the Y axis at Z = 0. + # The `sprite` should take approximately 1/4 of the height of the screen. + world_camera.reset_height 20.0 + + # Move the near clip wall closer to the camera because our world unit + # range is small. Moving the clip wall too close to the camera can + # cause glitches on mobiles devices with small depth buffer. + world_camera.near = 1.0 + + # Make the background blue and opaque. + glClearColor(0.0, 0.0, 1.0, 1.0) + + # If the first command line argument is an integer, add extra sprites. + if args.not_empty and args.first.is_int then + # It's a performance test, unlock the framerate. + maximum_fps = -1.0 + + # Add `args.first` sprites. + for i in args.first.to_i.times do + var s = new Sprite(texture, new Point3d[Float](30.0.rand - 15.0, 20.0.rand - 10.0, 0.0 - 30.0.rand)) + s.scale = 0.1 + sprites.add s + end + end + end + + redef fun update(dt) + do + # Update game logic here. + sprite.rotation += 0.1*pi*dt + + # Move `sprite` with the keyboard arrows. + # Set the speed according to the elapsed time since the last frame `dt` + # for a smooth animation. + var unit_per_second = 2.0 + for key in pressed_keys do + if key == "left" then + sprite.center.x -= unit_per_second*dt + else if key == "right" then + sprite.center.x += unit_per_second*dt + else if key == "up" then + sprite.center.y += unit_per_second*dt + else if key == "down" then + sprite.center.y -= unit_per_second*dt + end + end + end + + redef fun accept_event(event) + do + if super then return true + + if event isa QuitEvent or + (event isa KeyEvent and event.name == "escape" and event.is_up) then + # When window close button, escape or back key is pressed + # show the average FPS over the last few seconds. + print "{current_fps} fps" + print sys.perfs + + # Quit abruptly + exit 0 + else if event isa KeyEvent and event.is_down then + if event.name == "space" then + # Play a sound when space bar is pressed. + sound.play + return true + else if event.name == "s" then + # Remove a random sprite. + if sprites.not_empty then sprites.remove sprites.rand + else if event.name == "w" then + # Add a random sprite. + var s = new Sprite(texture, new Point3d[Float](30.0.rand - 15.0, 20.0.rand - 10.0, 0.0 - 30.0.rand)) + s.scale = 0.1 + s.tint[1] = 0.0 + s.tint[2] = 0.0 + sprites.add s + end + end + + return false + end +end diff --git a/lib/gamnit/flat.nit b/lib/gamnit/flat.nit index 6eb8fab..7c2db7e 100644 --- a/lib/gamnit/flat.nit +++ b/lib/gamnit/flat.nit @@ -29,7 +29,8 @@ # and the like. It can be used to standardize the size of the UI across # devices. It is used to position the sprites in `App::ui_sprites`. # -# See the sample game at `contrib/asteronits/`. +# See the sample game at `contrib/asteronits/` and the basic project template +# at `lib/gamnit/examples/template_flat/`. module flat import glesv2