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.86
34   */
35  public class ObjectColorModifier extends ColorModifier {
36  
37  	/***
38  	 * Creates a new instance of ObjectColorModifier <br>
39  	 * 
40  	 * @param context
41  	 *          the modifier context.
42  	 * @param idColor
43  	 *          the color to add/remove/...
44  	 * @param op
45  	 *          the operation applied to previous value with the value of this
46  	 *          modifier.
47  	 * @param main
48  	 *          is this object modifier is the main modifier.
49  	 */
50  	public ObjectColorModifier(ModifierContext context, Expression idColor,
51  			Operation op, boolean main) {
52  		super(context, idColor, op);
53  		objectPicture = ObjectFactory.getObjectPicture(name);
54  		this.main = main;
55  	}
56  
57  	@Override
58  	public Modifier removeObject(String objectName, Test objectTest) {
59  		if (name.equals(objectName) && objectTest.test(ability, to)) {
60  			StackManager.postRefreshColor(to);
61  			return next;
62  		}
63  		return super.removeObject(objectName, objectTest);
64  	}
65  
66  	@Override
67  	public int paintObject(Graphics g, int startX, int startY) {
68  		if (main) {
69  			if (startX + 13 > CardFactory.cardWidth) {
70  				return paintObject(g, 3, startY - 16);
71  			}
72  			g.drawImage(objectPicture, startX, startY, 13, 15, null);
73  			return super.paintObject(g, startX + 3, startY);
74  		}
75  		return super.paintObject(g, startX, startY);
76  	}
77  
78  	@Override
79  	public int getNbObjects(String objectName, Test objectTest) {
80  		if (main && objectName.equals(name) && objectTest.test(ability, to)) {
81  			if (next == null) {
82  				return 1;
83  			}
84  			return 1 + next.getNbObjects(objectName, objectTest);
85  		}
86  		return super.getNbObjects(objectName, objectTest);
87  	}
88  
89  	/***
90  	 * Picture representing this object
91  	 */
92  	private Image objectPicture;
93  
94  	/***
95  	 * is this object modifier is the main modifier
96  	 */
97  	private boolean main;
98  }