Source code: jdstar/Actor.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 javax.swing.JLabel;
21
22 abstract class Actor
23 implements InputListener
24 {
25 private static final boolean _ = false;
26
27 /** How much space should actor travel between game loop updates. */
28 protected static final double ACTOR_STEP = 1.0 / (double)GameLoop.FRAMES_PER_TILE;
29 static
30 {
31 if(_)System.out.println("ACTOR_STEP: "+ ACTOR_STEP);
32 }
33
34 public static final int PLAYER = 0;
35 public static final int BUDDY = 1;
36
37 private int whoami = 0;
38 protected Board board = null;
39 protected JLabel status = null;
40 protected Actor(int who, Input input, Board board, JLabel status)
41 {
42 whoami = who;
43 input.addInputListener(this);
44 this.board = board;
45 this.status = status;
46 }
47
48 void refreshBoard()
49 {
50 //if(_)System.out.println("Looking for "+ (whoami == PLAYER ? "player" : "buddy") +" on board...");
51 for (int x = board.w - 1; x >= 0; x--)
52 for (int y = board.h - 1; y >= 0; y--)
53 {
54 //if(_)System.out.println("Checking at "+ x +", "+ y);
55 if (board.board[y][x] == whoami)
56 {
57 //if(_)System.out.println("Found!");
58 this.x = x;
59 this.X = x + 0.5;
60 this.y = y;
61 this.Y = y + 0.5;
62 return;
63 }
64 }
65 throw new ActorNotFoundException((whoami == PLAYER ? "Player" : "Buddy") +" not found on board.");
66 }
67
68 /*
69 Movement is started here but stopped by game logic when actor reaches the
70 wall.
71 */
72 protected int action = InputEvent.IDLE;
73 private boolean active = false;
74 private int eventCode = 0;
75 public void inputEvent(InputEvent evt)
76 {
77 eventCode = evt.getEventCode();
78 if (eventCode == whoami)
79 {
80 active = true;
81 status.setText("Controlling "+ (whoami == PLAYER ? "player" : "buddy"));
82 status.revalidate();
83 return;
84 }
85 if ((whoami == PLAYER && eventCode == InputEvent.SELECT_BUDDY) ||
86 (whoami == BUDDY && eventCode == InputEvent.SELECT_PLAYER))
87 {
88 active = false;
89 return;
90 }
91 if (active && action == InputEvent.IDLE)
92 action = eventCode;
93 }
94
95 // position on board
96 protected int x = 0;
97 protected int y = 0;
98 protected double X = 0.0;
99 protected double Y = 0.0;
100
101 abstract void go();
102 }
103