Advertisement
Guest User

Untitled

a guest
Aug 12th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 1.40 KB | None | 0 0
  1. enum EntityType {
  2.     Player;
  3.     Hagrid;
  4. }
  5.  
  6. class Entity {
  7.     public var position: FastVector3 = new FastVector3();
  8.     public var type: EntityType;
  9.  
  10.     public function new(type: EntityType, x: Float, y: Float, z: Float) {
  11.         this.type = type;
  12.         this.position.x = x;
  13.         this.position.y = y;
  14.         this.position.z = z;
  15.     }
  16. }
  17.  
  18. class Player extends Entity {
  19.     public var playerVar: Bool;
  20.  
  21.     public function new(type: EntityType, playerVar: Bool) {
  22.         super(type, 0.0, 0.0, 0.0);
  23.         this.playerVar = playerVar;
  24.     }
  25.  
  26.     public function attack() {
  27.         trace("Player attac!");
  28.     }
  29. }
  30.  
  31. class Hagrid extends Entity {
  32.     public var text: String;
  33.  
  34.     public function new(type: EntityType, text: String) {
  35.         super(type, 0.0, 0.0, 0.0);
  36.         this.text = text;
  37.     }
  38.  
  39.     public function attack() {
  40.         trace("Hagrid slapped you with his dustbin lid sized hand.");
  41.     }
  42.  
  43.     public function battleCry() {
  44.         trace(text);
  45.     }
  46. }
  47.  
  48. // in some other class:
  49. public function new() {
  50.  
  51.         var e1: Entity = new Player(EntityType.Player, true);
  52.         var e2: Entity = new Hagrid(EntityType.Hagrid, "Yer're wizard Harry!");
  53.  
  54.         var entities:Array<Entity> =  [e1, e2];
  55.  
  56.         for (e in entities) {
  57.             switch (e.type) {
  58.                 case EntityType.Player: {
  59.                     var e = cast(e, Player);
  60.                     e.attack();
  61.                     trace(e.playerVar);
  62.                 }
  63.  
  64.                 case EntityType.Hagrid: {
  65.                     var e = cast(e, Hagrid);
  66.                     e.attack();
  67.                     e.battleCry();
  68.                 }
  69.             }
  70.         }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement