View
244
Download
0
Category
Tags:
Preview:
Citation preview
Object-oriented simulation
Used for simulation of physical systems Chemical plants Air combat Ecologies
Key idea: the physical world consists of objects Objects have their own state Objects act on one another
Make a simulation which has one data object per physical object Give each object fields that mirror the state of the physical objects Give objects methods to implement the forces/actions that change
objects
Object-oriented programming
Means many things to many people
Least common denominator: Divide data into objects with fields Provide some mechanism to allow objects to specify their own methods
for implementing different procedures ( a.k.a. “messages”)
OOP with class-based inheritance Objects are divided into classes (types) Fields and methods determined are by an object’s class Subclasses inherit fields and methods from parent classes But can selectively override particular methods of the parent class
OOP in Meta [class [Name fields …]
Parent more-fields …] Creates a new class named Name With parent class Parent And fields fields … and more-fields …
[new Type args …] Makes a new object of type Type And sets its fields based on args
[define-method [initialize [Type arg]] Defines a method for initializing an
object of type Type before it’s returned by new
[generic-procedure] Makes a procedure that can be
specialized with methods
[define-method [generic [Type arg] …] body …]
Adds/modifies a method for generic procedure generic
Matches only those calls whose args have their corresponding Types
When a match is found, executes body
[call-next-method] When used inside a method, calls the
method of the parent class
Adventure games
One of the oldestcomputer game genres
Based on wandering through
a set of rooms interacting with
objects and characters Solving puzzles
or mysteries
Subgenres Point-and-click adventures (Myst, Syberia) Text adventure games (Zork, Infocom games) RPGs (Final Fantasy, Everquest, Baldur’s gate)
Ontology of adventure games
Game state: Spatiality
Things have locations (places) Places have contents (sets of things)
Inventory Equipment Consumables (health, gold, potions, etc.)
Key items “Unlock” parts of the game Allowing the narrative to progress
Things
Places
Persons
Player
NPCs
Monsters
Portals
Items
Key items
Supplies
Equipment
RoomsInventory
An adventure game in Meta
Supports Pictures (for point-and-click style) Text output Sound
You can decide which of these you want to use
Basic ontology Things (have name, location) Places (have name, contents) Player Portals (have name, location, destination)
The Thing class
Things have: Name
A string describing the object
May be null Location
Place in which the Thing can be found
May be null Adjectives
List of strings describing the object
May be null
[define Thing [class [Thing name location] Object adjectives]]
[define-method [initialize [Thing x]][move x x.location]]
The Place class
Places have Contents
A list of Things inside them Sound
Buffer to play when the user enters the room
Can be null if no sound Sound-loop?
If true, the sound plays continuously
Image Bitmap to display when the user
enters the room Hotspots
A list of places in the image the player can click and handlers (procedures to call) for when they’re clicked
[define Place [class [Place name] Thing contents hotspots sound sound-loop? image]]
[define-method [initialize [Place p]][call-next-method][p.contents ← [new ArrayList]]]
Trivial example
(Based on a randomly chosen picture from my hard drive)
One room Two Things in it
The Player A group of Nerds
Trivial example
[define trivial-example [→ [with great-hall = [new Place “the Great Hall”] [great-hall.adjectives ← [list “big” “pink”]] [great-hall.image ← [image-file “Aaron talk.jpg”]] [great-hall.sound ← [sound-file “dog-bark4.wav”]] [great-hall.hotspots ← [list [list 640 320 760 450 [→ [print “Aaron says Mythica was great until they killed the project.”]]]]] [the-player ← [new Player “Emily” great-hall]] [with nerds = [new Nerds null great-hall] [nerds.adjectives ← [list “many”]]] [start-game “Sample game” 800 600 100 100]]]]
Trivial example
[define trivial-example [→ [with great-hall = [new Place “the Great Hall”] [great-hall.adjectives ← [list “big” “pink”]] [great-hall.image ← [image-file “Aaron talk.jpg”]] [great-hall.sound ← [sound-file “dog-bark4.wav”]] [great-hall.hotspots ← [list [list 640 320 760 450 [→ [print “Aaron says Mythica was great until they killed the project.”]]]]] [the-player ← [new Player “Emily” great-hall]] [with nerds = [new Nerds null great-hall] [nerds.adjectives ← [list “many”]]] [start-game “Sample game” 800 600 100 100]]]]
Make the Place object
Trivial example
[define trivial-example [→ [with great-hall = [new Place “the Great Hall”] [great-hall.adjectives ← [list “big” “pink”]] [great-hall.image ← [image-file “Aaron talk.jpg”]] [great-hall.sound ← [sound-file “dog-bark4.wav”]] [great-hall.hotspots ← [list [list 640 320 760 450 [→ [print “Aaron says Mythica was great until they killed the project.”]]]]] [the-player ← [new Player “Emily” great-hall]] [with nerds = [new Nerds null great-hall] [nerds.adjectives ← [list “many”]]] [start-game “Sample game” 800 600 100 100]]]]
Set up its attributes
Trivial example
[define trivial-example [→ [with great-hall = [new Place “the Great Hall”] [great-hall.adjectives ← [list “big” “pink”]] [great-hall.image ← [image-file “Aaron talk.jpg”]] [great-hall.sound ← [sound-file “dog-bark4.wav”]] [great-hall.hotspots ← [list [list 640 320 760 450 [→ [print “Aaron says Mythica was great until they killed the project.”]]]]] [the-player ← [new Player “Emily” great-hall]] [with nerds = [new Nerds null great-hall] [nerds.adjectives ← [list “many”]]] [start-game “Sample game” 800 600 100 100]]]]
Make Player object
Trivial example
[define trivial-example [→ [with great-hall = [new Place “the Great Hall”] [great-hall.adjectives ← [list “big” “pink”]] [great-hall.image ← [image-file “Aaron talk.jpg”]] [great-hall.sound ← [sound-file “dog-bark4.wav”]] [great-hall.hotspots ← [list [list 640 320 760 450 [→ [print “Aaron says Mythica was great until they killed the project.”]]]]] [the-player ← [new Player “Emily” great-hall]] [with nerds = [new Nerds null great-hall] [nerds.adjectives ← [list “many”]]] [start-game “Sample game” 800 600 100 100]]]]
Make Nerds object
Trivial example
[define trivial-example [→ [with great-hall = [new Place “the Great Hall”] [great-hall.adjectives ← [list “big” “pink”]] [great-hall.image ← [image-file “Aaron talk.jpg”]] [great-hall.sound ← [sound-file “dog-bark4.wav”]] [great-hall.hotspots ← [list [list 640 320 760 450 [→ [print “Aaron says Mythica was great until they killed the project.”]]]]] [the-player ← [new Player “Emily” great-hall]] [with nerds = [new Nerds null great-hall] [nerds.adjectives ← [list “many”]]] [start-game “Sample game” 800 600 100 100]]]]
Open windowand start game
Moving objects
We need to make sure that If a thing T says place P is its
location Then P lists T in its contents And vice-versa
We make generic procedures for Adding and removing objects from
locations Moving objects between locations
This allows us to define special behavior for particular kinds of Places and Things
[define move [generic-procedure]][define remove [generic-procedure]][define add [generic-procedure]]
[define-method [move thing place][remove thing thing.location][add thing place]]
Adding and removing
Some Things can have null as their location
So we need to be careful to specialize the behavior of add and remove
Fortunately, you can specify null as the class of an argument
[define-method [remove thing [null place]]«Do nothing»null]
[define-method [remove thing [Place place]][thing.location ← null][place.contents.Remove thing]]
[define-method [add thing [null place]][thing.location ← null]]
[define-method [add thing [Place place]][thing.location ← place][place.contents.Add thing]]
Describing objects For text adventures
We need a way to generatetext to describe objects
We want to base it on The name of the object (if any) It’s type Its adjectives (if any)
Examples [description [new Cat]]
“a cat” [description [new Cat “Harry” null]]
“a cat named Harry” [with h = [new Cat “Harry” null]
[h.adjectives ← [list “big” “black”]] [describe h]]
“a big, black cat named Harry” Notes
Concat is a procedure that appends strings together Stuff in gray is black magic we haven’t taught you about yet
[define description “Returns a string describing the object”[generic-procedure]]
[define-method [description [Thing t]][with type-name = [[type-of t].Name.ToLower] modifiers = [maybe-pad [comma-separate t.adjectives false]] name-string = [if [null? t.name] “” [concat “ named ” t.name]] [concat “a ” modifiers type-name name-string]]]
Describing scenes
[define-method [view-description [Place p]][with stuff = [filter [t → [≠ t the-player]] «Everything in the room except the player» p.contents] [concat “You are in ” [description p] “. ” [cond [[= [length stuff] 1] [concat “You see ” [description [first stuff]] “.”]] [[empty? stuff] “”] [else [concat “ You see ” [comma-separate [map description stuff] true] “.”]]]]]]
Describing scenes
[describe great-hall]
“You are in a big, pink place named the Great Hall. You see a many nerds.”
Choosing the sound to play
[define background-sound “Returns the sound buffer for the background noise of this Place, if any”
[generic-procedure]]
[define-method [background-sound [Place p]]p.sound]
The window code calls background-sound on the player’s location to choose what sound to play
Why did we do it this way? Why not just have it play p.sound directly?
Choosing the image to show
[define view-image [generic-procedure]]
[define-method [view-image [Place p]]p.image]
Again: why bother with a procedure like this?
Handling clicks of the mouse The hotspots of a Place is a field
with a list of lists: [list left top
right bottom proc]
Where: Left and top give the
coordinates of the upper-left corner of an area of the image
Right and bottom give the coordinates of its lower-right corner
Proc gives a procedure to call when the user clicks there
But again, you can change the behavior of clicking by
Making a new kind of place (i.e. a subclass of Place)
and defining a new method for it
[define click [generic-procedure]]
[define-method [click [Place p] mouseEventArgs][with x = mouseEventArgs.X y = mouseEventArgs.Y [unless [null? p.hotspots] [for-each [hotspot → [when [and [≥ x [first hotspot]] [≥ y [second hotspot]] [≤ x [third hotspot]] [≤ y [fourth hotspot]]] [[fifth hotspot]]]] p.hotspots]]]]
Recommended