Zyps

A game library for Ruby

Friday, November 9, 2007

dRuby Tutorial

This tutorial will show you how to connect to a remote Zyps environment, such as that set up by the EnvironmentServer class. We’ll show how to:

  • link to an Environment
  • place Creatures in the Environment
  • create Behaviors and add them to Creatures
  • create Actions for Behaviors to initiate
  • limit when Behaviors occur with Conditions
  • add EnvironmentalFactors such as wind

First, install Zyps:

sudo gem install zyps

Run Ruby in interactive mode:

irb

Load the libraries for remote communication:

require 'rubygems'
require 'zyps/remote'
include Zyps

Set up a proxy to the remote environment:

uri = 'druby://lappy486:8989'
environment = EnvironmentClient.get_environment(uri)

Create a creature:

creature = Creature.new
creature.location = Location.new(200, 100) #x, y
creature.vector = Vector.new(45, 50) #angle, speed
creature.size = 50

Add it to the environment:

environment.objects << creature

Let’s make one that actually does something.
A Behavior has one or more Actions.

chase = Behavior.new
chase.actions << ApproachAction.new(360, Vector.new(50, 0))

Copy our first Creature…

cat = creature.copy

This cat will be orange…

cat.color = Color.new(1, 0.75, 0)

Add the Behavior…

cat.behaviors << chase

And add it to the environment.

environment.objects << cat

Add a few more…

5.times {|i| environment.objects << cat.copy}

Behaviors also have Conditions.
These limit the circumstances under which Actions occur.
Let’s make a timid Creature that flees when others get too close.

run = Behavior.new
run.actions << FleeAction.new(360, Vector.new(60, 0))
run.conditions << ProximityCondition.new(200)

Copy a creature…

mouse = creature.copy
mouse.size = 10

Add the Behavior…

mouse.behaviors << run

And add several to the Environment.

5.times {|i| environment.objects << mouse.copy}

EnvironmentalFactors affect all the objects at once.
Let’s mix it up and add some wind.

environment.environmental_factors << Accelerator.new(Vector.new(200, 0))

There are many other Actions, Conditions, and EnvironmentalFactors to play with. Read about them in the API docs:
http://jay.mcgavren.com/zyps_doc/

posted by jay  

Powered by WordPress