Advertisement
weberc2

No polymorphism?

Apr 17th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 1.61 KB | None | 0 0
  1. namespace Game {
  2.     class Stage {
  3.         const int UNIT_H = 101;
  4.         const int UNIT_V = 82;
  5.         private Clutter.Stage _clStage;
  6.  
  7.         public Stage () {
  8.             _clStage = Clutter.Stage.get_default ();
  9.             AddTree (5,3);
  10.  
  11.             _clStage.color = Clutter.Color () { alpha = 255 };
  12.             _clStage.show_all ();
  13.         }
  14.  
  15.         public void AddDrawable (ref Drawable drawable) {
  16.             _clStage.add_actor (drawable);
  17.         }
  18.  
  19.         public void AddTree (int x, int y) {
  20.             Tree t = new Tree (x*UNIT_H, y*UNIT_V+UNIT_V/2);
  21.             AddDrawable (ref t);
  22.             /* Error: Cannot convert from `Game.Drawable' to `Game.Tree?'
  23.                     AddDrawable (ref t);
  24.                                  ^^^^^ */
  25.         }
  26.     }
  27.  
  28.  
  29.     class Drawable : Clutter.Actor {
  30.         private Cairo.ImageSurface _surface;
  31.  
  32.         public Drawable (float x, float y, float width, float height, string filepath) {
  33.             this.x = x;
  34.             this.y = y;
  35.             this.width = width;
  36.             this.height = height;
  37.  
  38.             var canvas = new Clutter.Canvas ();
  39.             set_content (canvas);
  40.             canvas.set_size ((int)width, (int)height);
  41.             canvas.draw.connect (drawme);
  42.  
  43.             _surface = new Cairo.ImageSurface.from_png (filepath);
  44.             canvas.invalidate ();
  45.         }
  46.        
  47.         private bool drawme (Cairo.Context ctx, int w, int h) {
  48.             ctx.set_source_surface (_surface, 0, 0);
  49.             ctx.paint ();
  50.             return true;
  51.         }
  52.     }
  53.  
  54.     class GameObject : Drawable {
  55.         const int IMG_WIDTH = 101;
  56.         const int IMG_HEIGHT = 171;
  57.  
  58.         public GameObject (float x, float y, string filepath) {
  59.             base (x, y, IMG_WIDTH, IMG_HEIGHT, filepath);
  60.         }
  61.     }
  62.    
  63.     class Tree : GameObject {
  64.         const string IMG_PATH = "imgs/Tree Tall.png";
  65.         public Tree (float x, float y) {
  66.             base (x, y, IMG_PATH);
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement