tanema / gamebox

A game template for building and distributing Gosu apps.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Gamebox

Gamebox Build Status Gamebox Deps Status

  • A game template for building and distributing Gosu games.
  • Quickly generate a game and have it up and running.
  • Provide conventions and DSL for building your game.
  • Facilitate quickly building distributable artifacts.
  • http://shawn42.github.com/gamebox/
  • see gamebox on rubygems.org for the list of requirements

Getting started with Gamebox

Purpose

Why use Gamebox? Gamebox was designed to spring board game development. It allows the developer to define business rules about a game very quickly without having to worry about resource loading, sound/music management, creating windows, or messing with viewports. Gamebox allows you to define a game's business rules within minutes, using the metaphor of a 5th grade play.

The driving idea behind Gamebox is to provide the ability to have as many of the basic common pieces of a 2D game at the developers disposal from the beginning.

The reason I wrote Gamebox is twofold: first, to aid in 48 hour game writing competitions and second, to allow me to write simple educational games for my kids.

Installation

  • gem install gamebox
  • or tar
  • or git clone git://github.com/shawn42/gamebox.git && bundle && rake install

Game Creation

  1. gamebox new zapper

  2. this will create the directory structure and needed files to get a basic actor up on the screen:

     zapper  
     ├── Gemfile  
     ├── README.rdoc  
     ├── Rakefile  
     ├── config  
     │   ├── boot.rb  
     │   ├── environment.rb  
     │   └── game.yml  
     ├── data  
     │   ├── fonts  
     │   ├── graphics  
     │   ├── music  
     │   └── sounds   
     ├── spec  
     │   └── helper.rb  
     └── src  
         ├── actors  
         │   └── player.rb  
         ├── app.rb  
         └── demo_stage.rb  
    
  3. you now have a runnable Gamebox game

     cd zapper  
     bundle
     rake
    

Stages

A stage is where all the magic happens. Each new play type in your game will use a different stage. An example game may have a number of stages such as: main_menu, play, or credits. A demo_stage.rb is created for you by using the gamebox new command.

    define_stage :demo do
      curtain_up do
       ...
      end
    end

Actors

Actors are the basic building blocks of games in Gamebox. Everything in the game is an actor: the player, an alien, the bullet, even the score on the screen. Internally, actors are just named collections of behaviors and observable attributes.

    define_actor :player do
        has_behaviors do
            shooter recharge_time: 4_000, shot_power: 15, kickback: 0.7
            shielded
            bomber kickback: 5
        
            die_by_sword
            die_by_bullet
            die_by_bomb
            blasted_by_bomb
        
            jumper
        end
        
    end

Behaviors

Behaviors are what bring life to actors. They interact interact with the actor's internal data, input, audio, etc.

    define_behavior :projectile do
    
      requires :director
      requires_behaviors :positioned
      setup do
        actor.has_attributes vel_x: 0,
                             vel_y: 0
    
        director.when :update do |delta_ms, delta_secs|
          actor.x += (actor.vel_x * delta_secs)
          actor.y += (actor.vel_y * delta_secs)
        end
      end
    
    end

Views

Actor views are the mechanism for drawing an actor in Gamebox. When an actor is created, Gamebox will see if there is a matching actor view by name. It will register the view to be drawn by the renderer. The draw callback is called with the rendering target, the x and y offsets based on the viewport, and the z layer to be used for drawing this actor (see the section on parallax layers for more on z layers).

    define_actor_view :label_view do
      draw do |target, x_off, y_off, z|          
        target.print actor.text, actor.x, actor.y, z, actor.font_style
      end
    end

Getting actors on stage

To get an actor on the stage, use the create_actor method on stage. This can be done directly from a stage or from a behavior that has required stage via requires :stage.

    curtain_up do
      @player = create_actor :label, x: 20, y: 30, text: "Hello World!"
    end
    
    # or
    stage.create_actor ..

Input

Input comes from the InputManager. The stage has direct access via the input_manager method. Behaviors can request that they get the input_manager via requires :input_manager. The preferred way of getting input to your actors is via the actor's input method. It returns an InputMapper that can be built with a hash of events. Behaviors then subscribe for those events from the actor's input, or ask for it during updates.

    actor.input.map_input '+space' => :shoot,
                          '+w' => :jump,
                          '+a' => :walk_left
                          '+s' => :duck
                          '+d' => :walk_right
                          
    actor.input.when :shoot do
      # shoot code
    end
    
    # or
    if actor.input.walk_left?
      # walk left
    end

Updates

Updates all come from the Director. Again, the stage has direct access via director and behaviors must requires :director.

    director.when :update do |t_ms, t_sec|
      # update something
    end

Sound and Music

SoundManager handles the autoloading of sounds from data/sounds and data/music. The stage has direct access via sound_manager. To allow an actor to emit sounds or music, give them the audible behavior. See Reactions below for usage from actors.

 	# music
	sound_manager.play_music :overworld
	
	# sounds
	sound_manager.play_sound :death

Reactions

To ask to react to something we use the react_to method. It sends your message to all of the actors behaviors, giving them a chance to (you guessed it), react.

    actor.react_to :play_sound, :jump

If the actor has an audible behavior listening, he'll play the jump sound. But what if something else wants to know about playing sounds. Maybe the actor triggers an effect by making sound. If the actor had a noise_alert behavior, it too would be notified of the :play_sound event.

    define_behavior :noise_alert do
      setup do
        reacts_with :play_sound
      end
      
      helpers do
        def play_sound(*args)
          # react here
        end
      end
    end

The reacts_with helper takes a list of events that your behavior is interested in, and maps them to helper methods.

TODO (finish)

Configuration

  1. Gamebox.configuration

Resources

  1. load paths
  2. fonts
  3. graphics (caching)
  4. sounds

Stages

  1. configuring stages
  2. changing stages

Physics

Intra-Links

[Link to the FAQ] (https://github.com/shawn42/gamebox/wiki/Frequently-Asked-Questions)

LICENSE:

(MIT)

Copyright © 2012 Shawn Anderson

About

A game template for building and distributing Gosu apps.


Languages

Language:Ruby 100.0%