KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > thewayout > two > SpaceShip


1 /*
2     Copyright 2001, 2002 Andreas Braig, ObjectFab GmbH
3  
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU Lesser General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
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 Lesser 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 de.thewayout.two;
20
21
22 public class SpaceShip
23         implements SteeringConstants
24 {
25
26     private int direction = 0;
27     private long speed = 0, power = 0;
28     private long[] position = new long[]{ 0, 0, 0 };
29     private Steering steering = null;
30
31     public Steering getSteering()
32     {
33         return steering;
34     }
35
36     public void setSteering(Steering theSteering)
37     {
38         steering = theSteering;
39     }
40
41     public int getDirection()
42     {
43         return direction;
44     }
45
46     public void setDirection(int newDirection)
47     {
48         direction = newDirection;
49     }
50
51     public long getSpeed()
52     {
53         return speed;
54     }
55
56     public void setSpeed(long newSpeed)
57     {
58         speed = newSpeed;
59     }
60
61     public long[] getPosition()
62     {
63         return position;
64     }
65
66     public void setPosition(long[] newPosition)
67     {
68         position[0] = newPosition[0];
69         position[1] = newPosition[1];
70         position[2] = newPosition[2];
71     }
72
73     public long getPowerConsumed()
74     {
75         return power;
76     }
77
78     public void consumePower(long value, String JavaDoc cause)
79     {
80         power += value;
81
82         System.out.println(power + " : " + cause);
83     }
84
85     public void makeStep()
86     {
87
88         long fieldsToGo = getSpeed();
89
90         getSteering().calculateStep(this);
91
92         while (fieldsToGo > 0)
93         {
94             goAhead();
95
96             fieldsToGo--;
97         }
98
99         consumePower(POWER_LIFE_SUPPORT, CAUSE_LIFE_SUPPORT);
100     }
101
102     protected void goAhead()
103     {
104
105         long[] pos = getPosition();
106
107         switch (getDirection())
108         {
109
110             case DIRECTION_X_PLUS :
111                 pos[0]++;
112                 break;
113
114             case DIRECTION_X_MINUS :
115                 pos[0]--;
116                 break;
117
118             case DIRECTION_Y_PLUS :
119                 pos[1]++;
120                 break;
121
122             case DIRECTION_Y_MINUS :
123                 pos[1]--;
124                 break;
125
126             case DIRECTION_Z_PLUS :
127                 pos[2]++;
128                 break;
129
130             case DIRECTION_Z_MINUS :
131                 pos[2]--;
132                 break;
133
134             default :
135         }
136
137         setPosition(pos);
138     }
139 }
140
Popular Tags