Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jdstar/Player.java


1   /*
2    * Copyright (C) 1999-2001 Max Gilead <gilead@linart.pl>
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU General Public License
6    * as published by the Free Software Foundation; either version 2
7    * of the License, or (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 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  package jdstar;
19  
20  import huf.math.MMath;
21  import javax.swing.JLabel;
22  
23  public class Player
24    extends Actor
25    {
26    private static final boolean _ = false;
27    
28    public Player(Input input, Board board, JLabel status)
29      {
30      super(PLAYER, input, board, status);
31      }
32    
33    double newX = 0.0;
34    double newY = 0.0;
35    int newx = 0;
36    int newy = 0;
37    double X0 = 0.0;
38    double Y0 = 0.0;
39    public void go()
40      {
41      //if(_)System.out.println("Player: go()");
42      if (action == InputEvent.IDLE)
43        return;
44      newX = X;
45      newY = Y;
46      switch(action)
47        {
48        case InputEvent.LEFT:
49          {
50          newX = newX - ACTOR_STEP; // player is approaching this position
51          
52          newx = (int)Math.floor(newX); // integer value of the position player is approaching
53          //if(_)System.out.println("Player: go(): left x = "+ x +" ("+newX +")");
54          
55          X0 = newX - newx; // delta between float and integer positions
56          
57          // left edge
58          if (newX < 0.0 || newx == 0)
59            {
60            action = InputEvent.IDLE;
61            newX = 0.5;
62            newx = 0;
63            }
64          // taking item
65          else
66            {
67            if (X0 < 0.5 + board.itemRadius)
68              {
69              //if(_)System.out.println("Player: go(): got item?");
70              if (board.board[y][newx] == Board.ITEM)
71                {
72                board.setTile(newx, y, Board.EMPTY);
73                board.itemsLeft--;
74                status.setText(board.itemsLeft +" items left");
75                status.revalidate();
76                if(_)System.out.println("Player: go(): YEA!  "+ board.itemsLeft +" left");
77                }
78              }
79            // approaching wall?
80            if (X0 < 0.5 + Actor.ACTOR_STEP)
81              {
82              //if(_)System.out.println("Player: go(): wall?");
83              if (board.board[y][newx - 1] == Board.WALL ||
84                board.board[y][newx - 1] == Board.BUDDY)
85                {
86                action = InputEvent.IDLE;
87                newX = newx + 0.5; // not going anywhere
88                if(_)System.out.println("Player: go(): WALL");
89                }
90              }
91            }
92          board.board[y][x] = Board.EMPTY;
93          board.board[y][newx] = Board.PLAYER;
94          X = newX;
95          x = (int)Math.floor(newX); // integer value of the position player is approaching
96          break;
97          }
98        case InputEvent.DOWN:
99          {
100         newY = newY - ACTOR_STEP; // player is approaching this position
101         
102         newy = (int)Math.floor(newY); // integer value of the position player is approaching
103         //if(_)System.out.println("Player: go(): left x = "+ x +" ("+newX +")");
104         
105         Y0 = newY - newy; // delta between float and integer positions
106         
107         // bottom edge
108         if (newY < 0.0 || newy == 0)
109           {
110           action = InputEvent.IDLE;
111           newY = 0.5;
112           newy = 0;
113           }
114         // taking item
115         else
116           {
117           if (Y0 < 0.5 + board.itemRadius)
118             {
119             //if(_)System.out.println("Player: go(): got item?");
120             if (board.board[newy][x] == Board.ITEM)
121               {
122               board.setTile(x, newy, Board.EMPTY);
123               board.itemsLeft--;
124               status.setText(board.itemsLeft +" items left");
125               status.revalidate();
126               if(_)System.out.println("Player: go(): YEA!  "+ board.itemsLeft +" left");
127               }
128             }
129           // approaching wall?
130           if (Y0 < 0.5 + Actor.ACTOR_STEP)
131             {
132             //if(_)System.out.println("Player: go(): wall?");
133             if (board.board[newy - 1][x] == Board.WALL ||
134               board.board[newy - 1][x] == Board.BUDDY)
135               {
136               action = InputEvent.IDLE;
137               newY = newy + 0.5; // not going anywhere
138               if(_)System.out.println("Player: go(): WALL");
139               }
140             }
141           }
142         board.board[y][x] = Board.EMPTY;
143         board.board[newy][x] = Board.PLAYER;
144         Y = newY;
145         y = (int)Math.floor(newY); // integer value of the position player is approaching
146         break;
147         }
148       case InputEvent.RIGHT:
149         {
150         newX = newX + ACTOR_STEP; // player is approaching this position
151         
152         newx = (int)Math.floor(newX); // integer value of the position player is approaching
153         //if(_)System.out.println("Player: go(): right x = "+ x +" ("+newX +")");
154         
155         X0 = newX - newx; // delta between float and integer positions
156         
157         // left edge
158         if (newX > board.w || newx == board.w - 1)
159           {
160           action = InputEvent.IDLE;
161           newX = board.w - 0.5;
162           newx = board.w - 1;
163           }
164         // taking item
165         else
166           {
167           if (X0 > 0.5 - board.itemRadius)
168             {
169             //if(_)System.out.println("Player: go(): got item?");
170             if (board.board[y][newx] == Board.ITEM)
171               {
172               board.setTile(newx, y, Board.EMPTY);
173               board.itemsLeft--;
174               status.setText(board.itemsLeft +" items left");
175               status.revalidate();
176               if(_)System.out.println("Player: go(): YEA!  "+ board.itemsLeft +" left");
177               }
178             }
179           // approaching wall?
180           if (X0 > 0.5 - Actor.ACTOR_STEP)
181             {
182             //if(_)System.out.println("Player: go(): wall?");
183             if (board.board[y][newx + 1] == Board.WALL ||
184               board.board[y][newx + 1] == Board.BUDDY)
185               {
186               action = InputEvent.IDLE;
187               newX = newx + 0.5; // not going anywhere
188               if(_)System.out.println("Player: go(): WALL");
189               }
190             }
191           }
192         board.board[y][x] = Board.EMPTY;
193         board.board[y][newx] = Board.PLAYER;
194         X = newX;
195         x = (int)Math.floor(newX); // integer value of the position player is approaching
196         break;
197         }
198       case InputEvent.UP:
199         {
200         newY = newY + ACTOR_STEP; // player is approaching this position
201         
202         newy = (int)Math.floor(newY); // integer value of the position player is approaching
203         //if(_)System.out.println("Player: go(): right x = "+ x +" ("+newX +")");
204         
205         Y0 = newY - newy; // delta between float and integer positions
206         
207         // left edge
208         if (newY > board.h || newy == board.h - 1)
209           {
210           action = InputEvent.IDLE;
211           newY = board.h - 0.5;
212           newy = board.h - 1;
213           }
214         // taking item
215         else
216           {
217           if (Y0 > 0.5 - board.itemRadius)
218             {
219             //if(_)System.out.println("Player: go(): got item?");
220             if (board.board[newy][x] == Board.ITEM)
221               {
222               board.setTile(x, newy, Board.EMPTY);
223               board.itemsLeft--;
224               status.setText(board.itemsLeft +" items left");
225               status.revalidate();
226               if(_)System.out.println("Player: go(): YEA!  "+ board.itemsLeft +" left");
227               }
228             }
229           // approaching wall?
230           if (Y0 > 0.5 - Actor.ACTOR_STEP)
231             {
232             //if(_)System.out.println("Player: go(): wall?");
233             if (board.board[newy + 1][x] == Board.WALL ||
234               board.board[newy + 1][x] == Board.BUDDY)
235               {
236               action = InputEvent.IDLE;
237               newY = newy + 0.5; // not going anywhere
238               if(_)System.out.println("Player: go(): WALL");
239               }
240             }
241           }
242         board.board[y][x] = Board.EMPTY;
243         board.board[newy][x] = Board.PLAYER;
244         Y = newY;
245         y = (int)Math.floor(newY); // integer value of the position player is approaching
246         break;
247         }
248       default:
249         {
250         throw new InternalException("Invalid action: "+ action);
251         }
252       }
253     }
254   }
255