11package engine ;
22
3+ import processing .core .PApplet ;
34import processing .core .PShape ;
45
56public class Platform extends GameObj {
67 private boolean movable ;
8+ private PApplet inst ;
79
8- public Platform (PShape shape , boolean movable , float x , float y ) {
9- super (shape .getWidth (), shape .getHeight (), 0 , x , y , shape , false , false );
10-
10+ public Platform (PApplet inst , float width , float height , float x , float y , boolean movable ) {
11+ super (width , height , 0 , x , y , false , false );
1112 this .movable = movable ;
13+
14+ int [] color = {(int ) random (255 ), (int ) random (255 ), (int ) random (255 )};
15+ this .rend = new Renderable (inst , color , PShape .RECT , width , height );
1216 }
1317
1418 @ Override
@@ -19,5 +23,67 @@ public String getType() {
1923 public boolean isMovable () {
2024 return this .movable ;
2125 }
26+
27+ @ Override
28+ public String toSerial () {
29+ String serial = "{"
30+ + "width:" + this .getObjWidth () + ","
31+ + "height:" + this .getObjHeight () + ","
32+ + "x:" + this .getPy ().getLocation ().x + ","
33+ + "y:" + this .getPy ().getLocation ().y + ","
34+ + "movable:" + this .movable + ","
35+ + "color:" + this .rend .getColorToString ()
36+ + "}" ;
2237
38+ return serial ;
39+ }
40+
41+ public static Platform deSerial (PApplet inst , String serial ) {
42+ float width = 0 ;
43+ float height = 0 ;
44+ float x = 0 ;
45+ float y = 0 ;
46+ boolean movable = false ;
47+ int [] color = new int [3 ];
48+
49+ serial = serial .replace ("{" , "" ).replace ("}" , "" );
50+ String [] data = serial .split ("," );
51+
52+ for (String d : data ) {
53+ String [] subData = d .split (":" );
54+ String key = subData [0 ];
55+ String value = subData [1 ];
56+
57+ switch (key ) {
58+ case ("width" ):
59+ width = Float .parseFloat (value );
60+ break ;
61+ case ("height" ):
62+ height = Float .parseFloat (value );
63+ break ;
64+ case ("x" ):
65+ x = Float .parseFloat (value );
66+ break ;
67+ case ("y" ):
68+ y = Float .parseFloat (value );
69+ break ;
70+ case ("movable" ):
71+ movable = Boolean .parseBoolean (value );
72+ break ;
73+ case ("color" ):
74+ value = value .replace ("[" , "" ).replace ("]" , "" );
75+ int i = 0 ;
76+ for (String c : value .split (" " )) {
77+ color [i ] = Integer .parseInt (c );
78+ i ++;
79+ }
80+ break ;
81+ default :
82+ break ;
83+ }
84+ }
85+ Platform res = new Platform (inst , width , height , x , y , movable );
86+ res .rend .setColor (color );
87+ return res ;
88+ }
2389}
0 commit comments