1 /* $Id$ */
2 /***************************************************************************
3  *                      (C) Copyright 2003 - Marauroa                      *
4  ***************************************************************************
5  ***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  ***************************************************************************/
13 package games.stendhal.client.entity;
14 
15 import java.awt.Rectangle;
16 import java.awt.geom.Rectangle2D;
17 
18 import marauroa.common.game.RPObject;
19 
20 /**
21  * A grain field entity.
22  */
23 public class GrainField extends Entity {
24 	/**
25 	 * Ripeness property.
26 	 */
27 	public static final Property PROP_RIPENESS = new Property();
28 
29 	/**
30 	 * The maximum ripeness.
31 	 */
32 	private int maxRipeness;
33 
34 	/**
35 	 * Current ripeness.
36 	 */
37 	private int ripeness;
38 
39 
40 
41 	/**
42 	 * Create a grain field.
43 	 */
GrainField()44 	public GrainField() {
45 	}
46 
47 	//
48 	// GrainField
49 	//
50 
51 	/**
52 	 * Get the maximum ripeness.
53 	 *
54 	 * @return The maximum ripeness.
55 	 */
getMaximumRipeness()56 	public int getMaximumRipeness() {
57 		return maxRipeness;
58 	}
59 
60 	/**
61 	 * Get the ripeness.
62 	 *
63 	 * @return The ripeness.
64 	 */
getRipeness()65 	public int getRipeness() {
66 		return ripeness;
67 	}
68 
69 	//
70 	// Entity
71 	//
72 
73 	/**
74 	 * Get the area the entity occupies.
75 	 *
76 	 * @return A rectange (in world coordinate units).
77 	 */
78 	@Override
getArea()79 	public Rectangle2D getArea() {
80 		return new Rectangle.Double(getX(), getY() + getHeight() - 1,
81 				getWidth(), 1);
82 	}
83 
84 	/**
85 	 * Initialize this entity for an object.
86 	 *
87 	 * @param object
88 	 *            The object.
89 	 *
90 	 * @see #release()
91 	 */
92 	@Override
initialize(final RPObject object)93 	public void initialize(final RPObject object) {
94 		super.initialize(object);
95 
96 		if (object.has("ripeness")) {
97 			ripeness = object.getInt("ripeness");
98 		} else {
99 			ripeness = 0;
100 		}
101 
102 		// default values are for compatibility to server <= 0.56
103 		if (object.has("max_ripeness")) {
104 			maxRipeness = object.getInt("max_ripeness");
105 		} else {
106 			maxRipeness = 5;
107 		}
108 	}
109 
110 	//
111 	// RPObjectChangeListener
112 	//
113 
114 	/**
115 	 * The object added/changed attribute(s).
116 	 *
117 	 * @param object
118 	 *            The base object.
119 	 * @param changes
120 	 *            The changes.
121 	 */
122 	@Override
onChangedAdded(final RPObject object, final RPObject changes)123 	public void onChangedAdded(final RPObject object, final RPObject changes) {
124 		super.onChangedAdded(object, changes);
125 
126 		if (changes.has("ripeness")) {
127 			ripeness = changes.getInt("ripeness");
128 			fireChange(PROP_RIPENESS);
129 		}
130 
131 		if (object.has("max_ripeness")) {
132 			maxRipeness = object.getInt("max_ripeness");
133 		}
134 	}
135 }
136