compile_to_python.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. use strict;
  2. use warnings;
  3. sub compile_to_python {
  4. # get game data
  5. my $game_data = shift;
  6. my $world = "python_engine/world.py";
  7. # copy template engine to current directory
  8. mkdir("python_engine");
  9. copy("target/python/engine.py", "python_engine/engine.py");
  10. copy("target/python/object.py", "python_engine/object.py");
  11. copy("target/python/characteristic.py", "python_engine/characteristic.py");
  12. copy("target/python/transform.py", "python_engine/transform.py");
  13. copy("target/python/__init__.py", "python_engine/__init__.py");
  14. copy("target/python/templates/world.py.template", $world);
  15. my $init = "self.object = Object()\n\t\t";
  16. my $update = "";
  17. my $draw = "";
  18. foreach (@{$game_data->[0]}) {
  19. # currently the struture supports only one object
  20. #my $object = $game_data->[0][0];
  21. my $object = $_;
  22. my $name = $object->[0][1];
  23. # get shape of object and convert it from data in memory to python code
  24. my $shape = $object->[1];
  25. my $python_shape = "";
  26. my $i = 0;
  27. while ($i < @$shape) {
  28. #for (my $i = 0; $i < 18; $i++) {
  29. if ($i == 0 || $i % 3 == 0) {
  30. $python_shape = $python_shape . "(";
  31. }
  32. $python_shape = $python_shape . "$shape->[$i][1], ";
  33. if ( ($i-2) %3 == 0 ) {
  34. $python_shape = $python_shape . "), ";
  35. }
  36. $i++;
  37. }
  38. # get color of object and convert it to python code
  39. my $color = $object->[2];
  40. my $python_color = "(
  41. $color->[0][1],
  42. $color->[1][1],
  43. $color->[2][1]
  44. )";
  45. my $matrix = $object->[3];
  46. my $python_matrix = "(
  47. $matrix->[0][1], $matrix->[4][1], $matrix->[ 8][1], $matrix->[12][1],
  48. $matrix->[1][1], $matrix->[5][1], $matrix->[ 9][1], $matrix->[13][1],
  49. $matrix->[2][1], $matrix->[6][1], $matrix->[10][1], $matrix->[14][1],
  50. $matrix->[3][1], $matrix->[7][1], $matrix->[11][1], $matrix->[15][1])\n\t\t";
  51. # replace data from default world in python engine to the objects found earlier
  52. $init .= "self.$name = Object()\n\t\t";
  53. $init = $init . "self.$name.col = $python_color\n\t\t";
  54. $init = $init . "self.$name.pos = $python_shape\n\t\t";
  55. $init = $init . "self.$name.AddCharacteristic( Transform($python_matrix) )\n\t\t";
  56. my $actions = $object->[4];
  57. foreach (@$actions) {
  58. my $action = $_;
  59. my $action_name = $action->[0][1];
  60. if ($action_name eq "rotate") {
  61. my $action_data = $action->[1];
  62. #$update .= "self.$name.loc = rotate_matrix(self.$name.loc, $action_data->[0][1], '$action_data->[1][1]')";
  63. }
  64. }
  65. # draw
  66. $draw .= "self.$name.draw()\n\t\t";
  67. }
  68. # init
  69. replace_string($world, $world .".bk", "\@OBJECTS_INITIALIZATION@", $init);
  70. copy($world .".bk", $world);
  71. unlink($world .".bk");
  72. # update
  73. replace_string($world, $world .".bk", "\@OBJECTS_UPDATE@", $update);
  74. copy($world .".bk", $world);
  75. unlink($world .".bk");
  76. # draw
  77. replace_string($world, $world .".bk", "\@OBJECTS_DRAW@", $draw);
  78. copy($world .".bk", $world);
  79. unlink($world .".bk");
  80. }
  81. sub replace_string() {
  82. my $in_f = shift;
  83. my $out_f = shift;
  84. my $from = shift;
  85. my $to = shift;
  86. open(my $in, "<", $in_f) or die "$!";
  87. open(my $out, ">", $out_f) or die "$!";
  88. while (<$in>) {
  89. s/$from/$to/;
  90. print $out $_;
  91. }
  92. close $in;
  93. close $out;
  94. }
  95. sub copy() {
  96. my $in_f = shift;
  97. my $out_f = shift;
  98. open(my $in, "<", $in_f) or die "$!";
  99. open(my $out, ">", $out_f) or die "$!";
  100. while (<$in>) {
  101. print $out $_;
  102. }
  103. close $in;
  104. close $out;
  105. }
  106. return 1;