1 / 24

unit 6

unit 6. object-oriented simulation. 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

sarah
Télécharger la présentation

unit 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. unit 6 object-oriented simulation

  2. 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

  3. 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

  4. [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 OOP in Meta

  5. Adventure games • One of the oldestcomputer game genres • Based on • wandering througha set of rooms • interacting withobjects and characters • Solving puzzlesor mysteries • Subgenres • Point-and-click adventures (Myst, Syberia) • Text adventure games (Zork, Infocom games) • RPGs (Final Fantasy, Everquest, Baldur’s gate)

  6. Ontology of adventure games Things 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 Persons Items Key items Player Monsters Supplies Portals NPCs Equipment Places Inventory Rooms

  7. 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)

  8. 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 Thing class

  9. Places have ContentsA 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]]] The Place class

  10. Trivial example • (Based on a randomly chosen picture from my hard drive) • One room • Two Things in it • The Player • A group of Nerds

  11. 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]]]]

  12. Trivial example Make the Place object [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]]]]

  13. Trivial example Set up its attributes [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]]]]

  14. Trivial example Make Player object [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]]]]

  15. Trivial example Make Nerds object [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]]]]

  16. Open window and start game 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]]]]

  17. 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]] Moving objects

  18. 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]] Adding and removing

  19. 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 objects

  20. 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] “.”]]]]]]

  21. Describing scenes • [describe great-hall] “You are in a big, pink place named the Great Hall. You see a many nerds.”

  22. 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?

  23. 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?

  24. 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]]]] Handling clicks of the mouse

More Related