View Javadoc

1   /*
2    *   Firemox is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.firemox.modifier;
20  
21  import java.awt.Graphics;
22  import java.awt.Image;
23  
24  import net.sf.firemox.clickable.target.card.CardFactory;
25  import net.sf.firemox.expression.Expression;
26  import net.sf.firemox.modifier.model.ObjectFactory;
27  import net.sf.firemox.operation.Operation;
28  import net.sf.firemox.stack.StackManager;
29  import net.sf.firemox.test.Test;
30  
31  /***
32   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
33   * @since 0.85
34   */
35  public class ObjectRegisterModifier extends RegisterModifier {
36  
37  	/***
38  	 * Creates a new instance of IdCardModifiers <br>
39  	 * 
40  	 * @param context
41  	 *          the modifier context.
42  	 * @param index
43  	 *          the modified index
44  	 * @param rightExpression
45  	 *          the expression used to modify the register
46  	 * @param op
47  	 *          the operation applied to previous value with the value of this
48  	 *          modifier.
49  	 * @param main
50  	 *          is this object modifier is the main modifier.
51  	 */
52  	public ObjectRegisterModifier(ModifierContext context, int index,
53  			Expression rightExpression, Operation op, boolean main) {
54  		super(context, index, rightExpression, op);
55  		objectPicture = ObjectFactory.getObjectPicture(name);
56  		this.main = main;
57  	}
58  
59  	@Override
60  	public Modifier removeObject(String objectName, Test objectTest) {
61  		if (name.equals(objectName) && objectTest.test(ability, to)) {
62  			StackManager.postRefreshRegisters(to, index);
63  			return next;
64  		}
65  		return super.removeObject(objectName, objectTest);
66  	}
67  
68  	@Override
69  	public int paintObject(Graphics g, int startX, int startY) {
70  		if (main) {
71  			if (startX + 13 > CardFactory.cardWidth) {
72  				return paintObject(g, 3, startY - 16);
73  			}
74  			g.drawImage(objectPicture, startX, startY, 13, 15, null);
75  			return super.paintObject(g, startX + 3, startY);
76  		}
77  		return super.paintObject(g, startX, startY);
78  	}
79  
80  	@Override
81  	public int getNbObjects(String objectName, Test objectTest) {
82  		if (main && objectName.equals(name) && objectTest.test(ability, to)) {
83  			if (next == null) {
84  				return 1;
85  			}
86  			return 1 + next.getNbObjects(objectName, objectTest);
87  		}
88  		return super.getNbObjects(objectName, objectTest);
89  	}
90  
91  	/***
92  	 * Picture representing this object
93  	 */
94  	private Image objectPicture;
95  
96  	/***
97  	 * is this object modifier is the main modifier
98  	 */
99  	private boolean main;
100 }