Advertisement
Andrew_Manu

Wollok game

Jun 23rd, 2019
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.68 KB | None | 0 0
  1. import wollok.vm.runtime
  2.  
  3. /**
  4.   * Wollok Game main object
  5.   */
  6. object game {
  7.    
  8.     /**
  9.      * Adds an object to the board for drawing it.
  10.      * Object should understand a position property
  11.      * (implemented by a reference or getter method).
  12.      *
  13.      * Example:
  14.      *     game.addVisual(pepita) ==> pepita should have a position property
  15.      */
  16.     method addVisual(positionable) native
  17.  
  18.     /**
  19.      * Adds an object to the board for drawing it on a specific position.
  20.      *
  21.      * Example:
  22.      *     game.addVisual(pepita, game.origin()) ==> no need for pepita to have a position property
  23.      *     game.addVisual(pepita, game.at(2, 2))
  24.      */
  25.     method addVisualIn(element, position) native
  26.  
  27.    
  28.     /**
  29.      * Adds an object to the board for drawing it. It can be moved with arrow keys.
  30.      * That object should understand a position property
  31.      * (implemented by a reference or getter method).
  32.      *
  33.      * Example:
  34.      *     game.addVisualCharacter(pepita) ==> pepita should have a position property
  35.      */
  36.     method addVisualCharacter(positionable) native
  37.  
  38.     /**
  39.      * Adds an object to the board for drawing it on a specific position. It can be moved with arrow keys.
  40.      *
  41.      * Example:
  42.      *     game.addVisualCharacterIn(pepita, game.origin()) ==> no need for pepita to have a position property
  43.      */
  44.     method addVisualCharacterIn(element, position) native
  45.  
  46.     /**
  47.      * Removes an object from the board for stop drawing it.
  48.      *
  49.      * Example:
  50.      *     game.removeVisual(pepita)
  51.      */
  52.     method removeVisual(visual) native
  53.    
  54.     /**
  55.      * Adds a block that will be executed each time a specific key is pressed
  56.      * @see keyboard.onPressDo()
  57.      */
  58.     method whenKeyPressedDo(key, action) native
  59.  
  60.     /**
  61.      * Adds a block that will be executed when the given object collides with other.
  62.      * Two objects collide when are in the same position.
  63.      *
  64.      * The block should expect the other object as parameter.
  65.      *
  66.      * Example:
  67.      *     game.whenCollideDo(pepita, { comida => pepita.comer(comida) })
  68.      */
  69.     method whenCollideDo(visual, action) native
  70.  
  71.     /**
  72.      * Adds a block with a specific name that will be executed every n milliseconds.
  73.      * Block expects no argument.
  74.      * Be careful not to set it too often :)
  75.      *
  76.      * Example:
  77.      *      game.onTick(5000, "pepitaMoving", { => pepita.position().x(0.randomUpTo(4)) })
  78.      */
  79.     method onTick(milliseconds, name, action) native
  80.      
  81.     /**
  82.      * Remove a tick event created with onTick message
  83.      *
  84.      * Example:
  85.      *      game.removeTickEvent("pepitaMoving")
  86.      */
  87.     method removeTickEvent(name) native
  88.    
  89.     /**
  90.      * Returns all objects in given position.
  91.      *
  92.      * Example:
  93.      *     game.getObjectsIn(game.origin())
  94.      */
  95.     method getObjectsIn(position) native
  96.  
  97.     /**
  98.      * Draws a dialog balloon with given message in given visual object position.
  99.      *
  100.      * Example:
  101.      *     game.say(pepita, "hola!")
  102.      */
  103.     method say(visual, message) native
  104.  
  105.     /**
  106.      * Removes all visual objects on board and configurations (colliders, keys, etc).
  107.      */
  108.     method clear() native
  109.  
  110.     /**
  111.      * Returns all objects that are in same position of given object.
  112.      */
  113.     method colliders(visual) native
  114.  
  115.     /**
  116.      * Stops render the board and finish the game.
  117.      */
  118.     method stop() native
  119.    
  120.     /**
  121.      * Starts render the board in a new windows.
  122.      */
  123.     method start() {
  124.         self.doStart(runtime.isInteractive())
  125.     }
  126.    
  127.     /**
  128.      * Returns a position for given coordinates.
  129.      */
  130.     method at(x, y) {
  131.         return new Position(x, y)
  132.     }
  133.  
  134.     /**
  135.      * Returns the position (0,0).
  136.      */
  137.     method origin() = self.at(0, 0)
  138.  
  139.     /**
  140.      * Returns the center board position (rounded down).
  141.      */
  142.     method center() = self.at(self.width().div(2), self.height().div(2))
  143.  
  144.     /**
  145.      * Sets game title.
  146.      */    
  147.     method title(title) native
  148.  
  149.     /**
  150.      * Returns game title.
  151.      */    
  152.     method title() native
  153.    
  154.     /**
  155.      * Sets board width (in cells).
  156.      */        
  157.     method width(width) native
  158.  
  159.     /**
  160.      * Returns board width (in cells).
  161.      */    
  162.     method width() native
  163.  
  164.     /**
  165.      * Sets board height (in cells).
  166.      */        
  167.     method height(height) native
  168.  
  169.     /**
  170.      * Returns board height (in cells).
  171.      */    
  172.     method height() native
  173.  
  174.     /**
  175.      * Sets cells background image.
  176.      */        
  177.     method ground(image) native
  178.    
  179.     /**
  180.      * Sets full background image.
  181.      */        
  182.     method boardGround(image) native
  183.    
  184.     /**
  185.      * Attributes will not show when user mouse over a visual component.
  186.      * Default behavior is to show them.
  187.      */
  188.     method hideAttributes(visual) native
  189.    
  190.     /**
  191.      * Attributes will appear again when user mouse over a visual component.
  192.      * Default behavior is to show them, so this is not necessary.
  193.      */
  194.     method showAttributes(visual) native
  195.    
  196.     /**
  197.      * Allows to configure a visual component as "error reporter".
  198.      * Then every error in game board will be reported by this visual component,
  199.      * in a balloon message form.
  200.      */
  201.     method errorReporter(visual) native
  202.      
  203.     /**
  204.      * Plays once a .mp3, .ogg or .wav audio file
  205.      */
  206.     method sound(audioFile) native
  207.    
  208.     /**
  209.     * @private
  210.     */
  211.     method doStart(isRepl) native
  212. }
  213.  
  214. /**
  215.  * Represents a position in a two-dimensional gameboard.
  216.  * It is an immutable object since Wollok 1.8.0
  217.  */
  218. class Position {
  219.     const property x
  220.     const property y
  221.    
  222.     /**
  223.      * Returns the position at origin: (0,0).
  224.      */    
  225.     constructor() = self(0, 0)
  226.            
  227.     /**
  228.      * Returns a position with given x and y coordinates.
  229.      * From now on, position is immutable.
  230.      */
  231.     constructor(_x, _y) {
  232.         x = _x
  233.         y = _y
  234.     }
  235.    
  236.     /**
  237.      * Returns a new Position n steps right from this one.
  238.      */    
  239.     method right(n) = new Position(x + n, y)
  240.    
  241.     /**
  242.      * Returns a new Position n steps left from this one.
  243.      */    
  244.     method left(n) = new Position(x - n, y)
  245.    
  246.     /**
  247.      * Returns a new Position n steps up from this one.
  248.      */    
  249.     method up(n) = new Position(x, y + n)
  250.    
  251.     /**
  252.      * Returns a new Position, n steps down from this one.
  253.      */    
  254.     method down(n) = new Position(x, y - n)
  255.  
  256.     /**
  257.      * Adds an object to the board for drawing it in self.
  258.      */
  259.     method drawElement(element) { game.addVisualIn(element, self) } //TODO: Implement native
  260.    
  261.     /**
  262.      * Adds an object to the board for drawing it in self. It can be moved with arrow keys.
  263.      */
  264.     method drawCharacter(element) { game.addVisualCharacterIn(element, self) } //TODO: Implement native
  265.  
  266.     /**
  267.      * Draw a dialog balloon with given message in given visual object position.
  268.      */
  269.     method say(element, message) { game.say(element, message) } //TODO: Implement native
  270.  
  271.     /**
  272.      * Returns all objects in self.
  273.      */
  274.     method allElements() = game.getObjectsIn(self) //TODO: Implement native
  275.    
  276.     /**
  277.      * Returns a new position with same coordinates.
  278.      */
  279.     method clone() = new Position(x, y)
  280.  
  281.     /**
  282.      * Returns the distance between given position and self.
  283.      */
  284.     method distance(position) {
  285.         const deltaX = x - position.x()
  286.         const deltaY = y - position.y()
  287.         return (deltaX.square() + deltaY.square()).squareRoot()
  288.     }
  289.  
  290.     /**
  291.      * Removes all objects in self from the board for stop drawing it.
  292.      */
  293.     method clear() {
  294.         self.allElements().forEach{it => game.removeVisual(it)}
  295.     }
  296.    
  297.     /**
  298.      * Two positions are equals if they have same coordinates.
  299.      */
  300.     override method ==(other) = x == other.x() && y == other.y()
  301.    
  302.     /**
  303.      * String representation of a position
  304.      */
  305.     override method toString() = "(" + x + "," + y + ")"
  306.    
  307. }
  308.  
  309. /**
  310.  * Keyboard object handles all keys movements. There is a method for each key.
  311.  *
  312.  * Examples:
  313.  *     keyboard.i().onPressDo { game.say(pepita, "hola!") }
  314.  *         => when user hits "i" key, pepita will say "hola!"
  315.  *
  316.  *     keyboard.any().onPressDo { game.say(pepita, "you pressed a key!") }
  317.  *         => any key pressed will activate its closure
  318.  */
  319. object keyboard {
  320.  
  321.     method any() = new Key(-1)
  322.  
  323.     method num(n) = new Key(n + 7, n + 144)
  324.    
  325.     method num0() = self.num(0)
  326.  
  327.     method num1() = self.num(1)
  328.  
  329.     method num2() = self.num(2)
  330.  
  331.     method num3() = self.num(3)
  332.  
  333.     method num4() = self.num(4)
  334.  
  335.     method num5() = self.num(5)
  336.  
  337.     method num6() = self.num(6)
  338.  
  339.     method num7() = self.num(7)
  340.  
  341.     method num8() = self.num(8)
  342.  
  343.     method num9() = self.num(9)
  344.  
  345.     method a() = new Key(29)
  346.  
  347.     method alt() = new Key(57, 58)
  348.  
  349.     method b() = new Key(30)
  350.  
  351.     method backspace() = new Key(67)
  352.  
  353.     method c() = new Key(31)
  354.  
  355.     method control() = new Key(129, 130)
  356.  
  357.     method d() = new Key(32)
  358.  
  359.     method del() = new Key(67)
  360.  
  361.     method center() = new Key(23)
  362.  
  363.     method down() = new Key(20)
  364.  
  365.     method left() = new Key(21)
  366.  
  367.     method right() = new Key(22)
  368.  
  369.     method up() = new Key(19)
  370.  
  371.     method e() = new Key(33)
  372.  
  373.     method enter() = new Key(66)
  374.  
  375.     method f() = new Key(34)
  376.  
  377.     method g() = new Key(35)
  378.  
  379.     method h() = new Key(36)
  380.  
  381.     method i() = new Key(37)
  382.  
  383.     method j() = new Key(38)
  384.  
  385.     method k() = new Key(39)
  386.  
  387.     method l() = new Key(40)
  388.  
  389.     method m() = new Key(41)
  390.  
  391.     method minusKey() = new Key(69)
  392.  
  393.     method n() = new Key(42)
  394.  
  395.     method o() = new Key(43)
  396.  
  397.     method p() = new Key(44)
  398.  
  399.     method plusKey() = new Key(81)
  400.  
  401.     method q() = new Key(45)
  402.  
  403.     method r() = new Key(46)
  404.  
  405.     method s() = new Key(47)
  406.  
  407.     method shift() = new Key(59, 60)
  408.  
  409.     method slash() = new Key(76)
  410.  
  411.     method space() = new Key(62)
  412.  
  413.     method t() = new Key(48)
  414.  
  415.     method u() = new Key(49)
  416.  
  417.     method v() = new Key(50)
  418.  
  419.     method w() = new Key(51)
  420.  
  421.     method x() = new Key(52)
  422.  
  423.     method y() = new Key(53)
  424.  
  425.     method z() = new Key(54)
  426.  
  427. }
  428.  
  429.  
  430. class Key {
  431.     var keyCodes
  432.    
  433.     constructor(_keyCodes...) {
  434.         keyCodes = _keyCodes
  435.     }
  436.  
  437.     /**
  438.      * Adds a block that will be executed always self is pressed.
  439.      *
  440.      * Examples:
  441.      *     keyboard.i().onPressDo { game.say(pepita, "hola!") }
  442.      *         => when user hits "i" key, pepita will say "hola!"
  443.      */
  444.     method onPressDo(action) {
  445.         keyCodes.forEach{ key => game.whenKeyPressedDo(key, action) } //TODO: Implement native
  446.     }
  447. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement