delegation.nut 887 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. PEntity <- {
  2. name="noname"
  3. pos={x=0,y=0,z=0}
  4. type="entity"
  5. //methamethod
  6. _typeof=function()
  7. {
  8. return type;
  9. }
  10. }
  11. function PEntity::PrintPos()
  12. {
  13. ::print("x="+pos.x+" y="+pos.y+" z="+pos.z+"\n");
  14. }
  15. function PEntity::new(name,pos)
  16. {
  17. local newentity=clone ::PEntity;
  18. if(name)
  19. newentity.name=name;
  20. if(pos)
  21. newentity.pos=pos;
  22. return newentity;
  23. }
  24. PPlayer <- {
  25. model="warrior.mdl"
  26. weapon="fist"
  27. health=100
  28. armor=0
  29. //overrides the parent type
  30. type="player"
  31. }
  32. function PPlayer::new(name,pos)
  33. {
  34. local p = clone ::PPlayer;
  35. local newplayer = ::PEntity.new(name,pos);
  36. newplayer.setdelegate(p);
  37. return newplayer;
  38. }
  39. local player=PPlayer.new("godzilla",{x=10,y=20,z=30});
  40. ::print("PLAYER NAME"+player.name+"\n");
  41. ::print("ENTITY TYPE"+typeof player+"\n");
  42. player.PrintPos();
  43. player.pos.x=123;
  44. player.PrintPos();