summaryrefslogtreecommitdiff
path: root/NET/worlds/console/Stair.java
blob: c4380d9ad2f736ee36e82898c8b4128e45d28ef1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package NET.worlds.console;

import NET.worlds.scape.Material;
import NET.worlds.scape.NoSuchPropertyException;
import NET.worlds.scape.Point3;
import NET.worlds.scape.Point3Temp;
import NET.worlds.scape.Portal;
import NET.worlds.scape.Property;
import NET.worlds.scape.Rect;
import NET.worlds.scape.Restorer;
import NET.worlds.scape.Room;
import NET.worlds.scape.RoomEnvironment;
import NET.worlds.scape.Saver;
import NET.worlds.scape.TooNewException;
import NET.worlds.scape.World;
import java.io.IOException;

public class Stair extends Room {
   public Portal bottom;
   public Portal top;
   private int _lengthwise;
   static final int PLUSX = 0;
   static final int PLUSY = 1;
   static final int MINUSX = 2;
   static final int MINUSY = 3;
   private float _length;
   private float _width;
   private float _rise;
   private static Object classCookie = new Object();

   public Stair(
      World world,
      String name,
      float width,
      float length,
      float rise,
      float ceilingz,
      int numsteps,
      Material riser,
      Material tread,
      Material left,
      Material right,
      Material head,
      Material ceiling
   ) {
      super(world, name);
      this._lengthwise = 1;
      this._length = length;
      this._width = width;
      this._rise = rise;
      RoomEnvironment env = this.getEnvironment();
      env.add(Rect.ceiling(0.0F, 0.0F, ceilingz, width, length, ceiling));
      float lowerz = ceilingz - rise;
      env.add(new Rect(-0.1F, 0.0F, 0.0F, -0.1F, length, ceilingz, left));
      env.add(new Rect(width + 0.1F, length, 0.0F, width + 0.1F, 0.0F, ceilingz, right));
      env.add(new Rect(width, 0.0F, lowerz, 0.0F, 0.0F, ceilingz, head));
      this.bottom = new Portal(width, 0.0F, 0.0F, 0.0F, 0.0F, lowerz);
      this.top = new Portal(0.0F, length, rise, width, length, ceilingz);
      float dy = length / numsteps;
      float dz = rise / numsteps;

      for (int i = 0; i < numsteps; i++) {
         Rect w = new Rect(0.0F, i * dy, i * dz, width, i * dy, (i + 1) * dz, riser);
         w.setTileSize(dz, dz);
         env.add(w);
      }

      for (int i = 0; i < numsteps; i++) {
         Rect w = Rect.floor(0.0F, i * dy, (i + 1) * dz, width, (i + 1) * dy, tread);
         w.setTileSize(dy, dy);
         env.add(w);
      }

      env.add(this.bottom);
      env.add(this.top);
   }

   public Stair() {
   }

   void setLength(float len) {
      this._length = len;
   }

   void setRise(float r) {
      this._rise = r;
   }

   void setLengthwise(int lw) {
      this._lengthwise = lw;
   }

   @Override
   public float floorHeight(float x, float y, float z) {
      float dist = 0.0F;
      switch (this._lengthwise) {
         case 0:
            dist = x;
            break;
         case 1:
            dist = y;
            break;
         case 2:
            dist = this._length - x;
            break;
         case 3:
            dist = this._length - y;
            break;
         default:
            assert false;
      }

      return dist / this._length * this._rise;
   }

   public Point3 surfaceNormal(float x, float y) {
      Point3 A = new Point3(this._width, 0.0F, 0.0F);
      Point3Temp B = Point3Temp.make(0.0F, this._length, this._rise);
      A.cross(B);
      A.normalize();
      return A;
   }

   @Override
   public void saveState(Saver s) throws IOException {
      s.saveVersion(1, classCookie);
      super.saveState(s);
      s.saveFloat(this._length);
      s.saveFloat(this._rise);
      s.saveInt(this._lengthwise);
      s.save(this.bottom);
      s.save(this.top);
   }

   @Override
   public void restoreState(Restorer r) throws IOException, TooNewException {
      switch (r.restoreVersion(classCookie)) {
         case 0:
            super.restoreState(r);
            this._length = r.restoreFloat();
            this._rise = r.restoreFloat();
            this.bottom = (Portal)r.restore();
            this.top = (Portal)r.restore();
            this.setLengthwise(1);
            break;
         case 1:
            super.restoreState(r);
            this._length = r.restoreFloat();
            this._rise = r.restoreFloat();
            this._lengthwise = r.restoreInt();
            this.bottom = (Portal)r.restore();
            this.top = (Portal)r.restore();
            break;
         default:
            throw new TooNewException();
      }
   }

   void superRestoreState(Restorer r) throws IOException, TooNewException {
      super.restoreState(r);
   }

   @Override
   public Object properties(int index, int offset, int mode, Object value) throws NoSuchPropertyException {
      Object ret = null;
      switch (index - offset) {
         case 0:
            if (mode == 0) {
               ret = new Property(this, index, "Length");
            } else if (mode == 1) {
               ret = new Float(this._length);
            }
            break;
         case 1:
            if (mode == 0) {
               ret = new Property(this, index, "Rise");
            } else if (mode == 1) {
               ret = new Float(this._rise);
            }
            break;
         case 2:
            if (mode == 0) {
               ret = new Property(this, index, "Direction");
            } else if (mode == 1) {
               ret = new Integer(this._lengthwise);
            }
            break;
         default:
            ret = super.properties(index, offset + 3, mode, value);
      }

      return ret;
   }
}