1 /* |
|
2 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
|
3 * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com> |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation; version 2 of the License |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program; if not, write to the Free Software |
|
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 */ |
|
18 |
|
19 package org.hedgewars.hedgeroid.Datastructures; |
|
20 |
|
21 import java.io.File; |
|
22 import java.io.IOException; |
|
23 |
|
24 import org.hedgewars.hedgeroid.EngineProtocol.EngineProtocolNetwork; |
|
25 |
|
26 |
|
27 import android.content.Context; |
|
28 import android.graphics.drawable.Drawable; |
|
29 import android.os.Parcel; |
|
30 import android.os.Parcelable; |
|
31 |
|
32 public class Map implements Comparable<Map>, Parcelable{ |
|
33 |
|
34 private static final String MISSION_PREFIX = "Mission: "; |
|
35 |
|
36 private String name; |
|
37 private String path; |
|
38 private String previewPath; |
|
39 private MapType type; |
|
40 |
|
41 public Map(File mapDir, MapType _type, Context c){ |
|
42 type = _type; |
|
43 |
|
44 name = mapDir.getName(); |
|
45 path = mapDir.getAbsolutePath(); |
|
46 previewPath = path + "/preview.png"; |
|
47 |
|
48 /*switch(type){ |
|
49 case TYPE_DEFAULT: |
|
50 |
|
51 break; |
|
52 case TYPE_GENERATED: |
|
53 //TODO |
|
54 break; |
|
55 case TYPE_MISSION: |
|
56 name = MISSION_PREFIX + mapDir.getName(); |
|
57 path = mapDir.getAbsolutePath(); |
|
58 break; |
|
59 }*/ |
|
60 |
|
61 |
|
62 } |
|
63 |
|
64 public Map(Parcel in){ |
|
65 readFromParcel(in); |
|
66 } |
|
67 |
|
68 public String toString(){ |
|
69 switch(type){ |
|
70 default: |
|
71 case TYPE_DEFAULT: |
|
72 return name; |
|
73 case TYPE_GENERATED: |
|
74 return "bla"; |
|
75 case TYPE_MISSION: |
|
76 return MISSION_PREFIX + name; |
|
77 } |
|
78 } |
|
79 |
|
80 public void sendToEngine(EngineProtocolNetwork epn) throws IOException{ |
|
81 epn.sendToEngine(String.format("emap %s",name)); |
|
82 } |
|
83 |
|
84 public MapType getType(){ |
|
85 return type; |
|
86 } |
|
87 |
|
88 public Drawable getDrawable(){ |
|
89 switch(type){ |
|
90 case TYPE_MISSION: |
|
91 case TYPE_DEFAULT: |
|
92 return Drawable.createFromPath(previewPath); |
|
93 case TYPE_GENERATED: |
|
94 |
|
95 default: |
|
96 return null; |
|
97 } |
|
98 } |
|
99 |
|
100 public int compareTo(Map another) { |
|
101 switch(type){ |
|
102 case TYPE_GENERATED: |
|
103 switch(another.getType()){ |
|
104 case TYPE_GENERATED: |
|
105 return name.compareTo(another.name); |
|
106 case TYPE_MISSION: |
|
107 return -1; |
|
108 case TYPE_DEFAULT: |
|
109 return -1; |
|
110 } |
|
111 case TYPE_MISSION: |
|
112 switch(another.getType()){ |
|
113 case TYPE_GENERATED: |
|
114 return 1; |
|
115 case TYPE_MISSION: |
|
116 return name.compareTo(another.name); |
|
117 case TYPE_DEFAULT: |
|
118 return -1; |
|
119 } |
|
120 case TYPE_DEFAULT: |
|
121 switch(another.getType()){ |
|
122 case TYPE_GENERATED: |
|
123 return 1; |
|
124 case TYPE_MISSION: |
|
125 return 1; |
|
126 case TYPE_DEFAULT: |
|
127 return name.compareTo(another.name); |
|
128 } |
|
129 } |
|
130 return 0;//default case this should never happen |
|
131 } |
|
132 |
|
133 public enum MapType{ |
|
134 TYPE_DEFAULT, TYPE_MISSION, TYPE_GENERATED |
|
135 } |
|
136 |
|
137 public int describeContents() { |
|
138 return 0; |
|
139 } |
|
140 |
|
141 public void writeToParcel(Parcel dest, int flags) { |
|
142 dest.writeString(name); |
|
143 dest.writeString(path); |
|
144 dest.writeString(previewPath); |
|
145 dest.writeString(type.name()); |
|
146 } |
|
147 |
|
148 private void readFromParcel(Parcel src){ |
|
149 name = src.readString(); |
|
150 path = src.readString(); |
|
151 previewPath = src.readString(); |
|
152 type = MapType.valueOf(src.readString()); |
|
153 } |
|
154 public static final Parcelable.Creator<Map> CREATOR = new Parcelable.Creator<Map>() { |
|
155 public Map createFromParcel(Parcel source) { |
|
156 return new Map(source); |
|
157 } |
|
158 public Map[] newArray(int size) { |
|
159 return new Map[size]; |
|
160 } |
|
161 |
|
162 }; |
|
163 } |
|