项目作者: ddfreyne

项目描述 :
Crystal framework for making games
高级语言: Crystal
项目地址: git://github.com/ddfreyne/glove.git
创建时间: 2016-10-03T09:45:08Z
项目社区:https://github.com/ddfreyne/glove

开源协议:

下载


Build status

Glove

Glove is a framework for making games. It is implemented in Crystal.

⚠ Caution! ⚠ Glove is experimental. Expect breaking changes. There are few tests. Do not use this for your own projects (yet). (Or do, and contribute back to Glove? That’d be cool. I’ll give you commit access. You can be one of the first people to write a proper game engine in Crystal.)

Usage

To use this shard, add the following lines to your shard.yml:

  1. dependencies:
  2. glove:
  3. git: git@github.com:ddfreyne/glove.git

Glove comes with shaders in its shaders/ directory, which needs to be copied to where the executable is located. For example, the following will create a target/ directory that contains the executable and the shaders directory:

  1. rm -rf target/
  2. mkdir -p target
  3. crystal build -o target/mygame src/mygame.cr
  4. cp -r lib/glove/src/shaders target/shaders

It is useful to let the executable cd to the directory it is located in, before doing anything else, so that it can find the shaders easily:

  1. if full_path = Process.executable_path
  2. Dir.cd(File.dirname(full_path))
  3. end

The target/ directory should also include any assets that the game needs to run; a more complete build script could therefore look as follows:

  1. rm -rf target/
  2. mkdir -p target
  3. crystal build -o target/mygame src/mygame.cr
  4. cp -r lib/glove/src/shaders target/shaders
  5. cp -r assets target/assets # <- added

Example code

Here is a trivial example that renders a card (from assets/card.png):

  1. require "glove"
  2. if full_path = Process.executable_path
  3. Dir.cd(File.dirname(full_path))
  4. end
  5. card =
  6. Glove::Entity.new.tap do |e|
  7. e << Glove::Components::Texture.new("assets/card.png")
  8. e << Glove::Components::Transform.new.tap do |t|
  9. t.width = 140_f32
  10. t.height = 190_f32
  11. t.translate_x = 400_f32
  12. t.translate_y = 300_f32
  13. end
  14. end
  15. scene =
  16. Glove::Scene.new.tap do |scene|
  17. scene.spaces << Glove::Space.new.tap do |space|
  18. space.entities << card
  19. end
  20. end
  21. game = Glove::EntityApp.new(800, 600, "Inari")
  22. game.clear_color = Glove::Color::WHITE
  23. game.replace_scene(scene)
  24. game.run

Architecture

  • Glove::EntityApp is a generic game superclass that provides functionality for handling entities, and everything associated with it. Here is how a typical game would build an instance and run the game:

    1. game = Glove::EntityApp.new(800, 600, "Inari")
    2. game.clear_color = Glove::Color::WHITE
    3. scene = Glove::Scene.new.tap do |scene|
    4. # … build scene here …
    5. end
    6. game.replace_scene(scene)
    7. game.run
  • Glove::Entity is a game object that is visible and/or reacts to user input.

  • Glove::Component is a property of an entity. A common component is Glove::Components::Transform, which adds width, height, rotation, scale, … to an entity. Another common component is Glove::Components::Camera, which marks an entity as being a camera, and defines which part of a space (see below) will be rendered, with what rotation, etc.

  • Glove::Action defines a change to an entity. It can either be instant (e.g. remove entity) or act over time (e.g. move).

  • Glove::Space groups entities in a scene that logically belong together and can interact with each other. Entities in different spaces never interact. For example, one space might contain the game entities, and another space might contain UI elements.

  • Glove::Scene describes a scene (such as the main menu, credits, or in-game screen). It contains one or more spaces.

  • Glove::System describes logic for making changes to a space. A common system is a physics system, which would calculate velocities and update positions.

There are also a handful of simple data classes:

  • Glove::Color
  • Glove::Point
  • Glove::Quad
  • Glove::Rect
  • Glove::Size
  • Glove::Vector

Acknowledgements

This project started out by playing with crystal-gl by Gustavo Giráldez.