OSDN Git Service

Nazghul-0.7.1
[nazghul-jp/nazghul-jp.git] / src / Field.cpp
1 //
2 // nazghul - an old-school RPG engine
3 // Copyright (C) 2002, 2003 Gordon McNutt
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)
8 // any 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
12 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13 // more details.
14 //
15 // You should have received a copy of the GNU General Public License along with
16 // this program; if not, write to the Free Foundation, Inc., 59 Temple Place,
17 // Suite 330, Boston, MA 02111-1307 USA
18 //
19 // Gordon McNutt
20 // gmcnutt@users.sourceforge.net
21 //
22
23 #include "Field.h"
24 #include "common.h"
25 #include "session.h"
26
27 bool FieldType::isType(int classID) 
28 {
29         if (classID == FIELD_TYPE_ID)
30                 return true;
31         return ObjectType::isType(classID);
32 }
33
34 int FieldType::getType()
35 {
36         return FIELD_TYPE_ID;
37 }
38
39 FieldType::FieldType(const char *tag, const char *name, struct sprite *sprite, 
40                      int light_, int duration_, int pclass_, closure_t *clx)
41         : ObjectType(tag, name, sprite, field_layer), 
42           pclass(pclass_), light(light_), duration(duration_)
43 {
44         if (clx) {
45                 closure_ref(clx);
46                 effect = clx;
47         } else {
48                 effect = NULL;
49         }
50 }
51
52 FieldType::~FieldType()
53 {
54         closure_unref_safe(effect);
55 }
56
57 int  FieldType::getLight()
58 {
59         return light;
60 }
61
62 void  FieldType::setLight(int val)
63 {
64         light = val;
65 }
66
67 void  FieldType::setDuration(int val)
68 {
69         duration = val;
70 }
71
72 int  FieldType::getDuration()
73 {
74         return duration;
75 }
76
77 class Object *FieldType::createInstance()
78 {
79         return new Field(this);
80 }
81
82 class FieldType * Field::getObjectType()
83 {
84         return (class FieldType *) Object::getObjectType();
85 }
86
87 bool FieldType::isPermanent()
88 {
89         return (duration < 0);
90 }
91
92 //////////////////////////////////////////////////////////////////////////////
93
94 Field::Field(FieldType *type)
95         : Object(type)
96 {
97         duration = type->getDuration();
98 }
99
100 Field::Field(FieldType *type, int dur)
101         : Object(type)
102 {
103         duration = dur;
104 }
105
106 Field::Field() : duration(0)
107 {        
108 }
109
110 Field::~ Field()
111 {
112 }
113
114 int Field::getLight()
115 {
116         return getObjectType()->getLight();
117 }
118
119 void Field::exec()
120 {
121         startTurn();
122         if (isDestroyed())
123                 return;
124         
125         if (getObjectType()->isPermanent())
126                 return;
127
128         duration--;
129         assert(duration >= 0);
130         if (duration == 0)
131                 destroy();
132 }
133
134 void Field::save(struct save *save)
135 {
136         save->write(save, "(kern-mk-field %s %d)", getObjectType()->getTag(), 
137                     duration);
138 }
139
140 int Field::getPclass()
141 {
142         return getObjectType()->pclass;
143 }