NXEngine v1.0.0.5
[NXEngine.git] / floattext.cpp
blobab8872645e9b3e15a01c0cf8be32a2539a74292d
2 #include "nx.h"
3 #include "floattext.fdh"
5 FloatText *FloatText::first = NULL;
6 FloatText *FloatText::last = NULL;
8 /*
9 void c------------------------------() {}
12 FloatText::FloatText(int sprite)
14 prev = NULL;
15 next = first;
17 if (first)
18 first->prev = this;
19 else
20 last = this;
22 first = this;
23 this->sprite = sprite;
25 Reset();
26 ObjectDestroyed = false;
29 FloatText::~FloatText()
31 if (this->next) this->next->prev = this->prev;
32 if (this->prev) this->prev->next = this->next;
34 if (this == first) first = first->next;
35 if (this == last) last = last->prev;
38 void FloatText::Reset()
40 this->state = FT_IDLE;
41 this->shownAmount = 0;
45 void c------------------------------() {}
48 // adds the spec'd amount of damage/energy to the object's point display
49 void FloatText::AddQty(int amt)
51 //stat("FloatText::AddQty(%d)", amt);
52 if (amt == 0) return;
54 // first add the damage to the total
55 if (this->state == FT_IDLE)
57 this->state = FT_RISE;
59 this->shownAmount = amt;
60 this->yoff = FT_Y_START;
61 this->timer = 0;
63 else
65 this->shownAmount += amt;
67 // if we're scrolling away jerk back down
68 if (this->state == FT_SCROLL_AWAY)
70 this->state = FT_HOLD;
71 this->yoff = FT_Y_HOLD;
74 // reset the timer which counts how long we stay in hold state
75 if (this->state != FT_RISE)
76 this->timer = 0;
79 if (this->shownAmount > 9999)
80 this->shownAmount = 9999; // overrun protection for buffer
83 // updates the position of a floattext in respect to it's object
84 void FloatText::UpdatePos(Object *assoc_object)
86 // get the center pixel of the object we're associated with
87 this->objX = (assoc_object->x >> CSF) + (sprites[assoc_object->sprite].w / 2);
88 this->objY = (assoc_object->y >> CSF) + (sprites[assoc_object->sprite].h / 2);
90 // adjust for possible draw point
91 SIFDir *dir = &sprites[assoc_object->sprite].frame[assoc_object->frame].dir[assoc_object->dir];
92 this->objX -= dir->drawpoint.x;
93 this->objY -= dir->drawpoint.y;
97 // moves and draws the given float text if need be
98 void FloatText::Draw()
100 FloatText *ft = this;
101 int x, y, i;
103 switch(ft->state)
105 // rise to top point, moving once every other frame
106 case FT_RISE:
108 ft->timer ^= 1;
109 if (ft->timer)
111 if (--ft->yoff <= FT_Y_HOLD)
113 ft->state = FT_HOLD;
114 ft->timer = 0;
118 break;
120 // hold at top for a moment
121 case FT_HOLD:
123 if (++ft->timer >= 42)
124 ft->state = FT_SCROLL_AWAY;
126 break;
128 // scroll away quickly and disappear
129 case FT_SCROLL_AWAY:
131 if (--ft->yoff <= FT_Y_RISEAWAY)
133 ft->state = FT_IDLE;
134 ft->shownAmount = 0;
135 ft->timer = 0;
136 return;
139 break;
142 // set the SDL clipping region to just above the hold point
143 // so it looks like it "rolls" away.
144 if (ft->state == FT_SCROLL_AWAY)
146 // this formula is confusing until you realize that FT_Y_HOLD is a negative number
147 int y = ((ft->objY - (map.displayed_yscroll >> CSF)) + FT_Y_HOLD);
148 int h = (SCREEN_HEIGHT - y);
150 set_clip_rect(0, y, SCREEN_WIDTH, h);
153 // render the damage amount into a string
154 char text[6] = { 10 };
155 sprintf(&text[1], "%d", ft->shownAmount);
156 for(i=1;text[i];i++) text[i] -= '0';
157 int textlen = i;
159 x = ft->objX - (textlen * (8 / 2)); // center the string on the object
160 y = ft->objY + ft->yoff;
161 // adjust to object's onscreen position
162 x -= (map.displayed_xscroll >> CSF);
163 y -= (map.displayed_yscroll >> CSF);
165 // draw the text char by char
166 for(i=0;i<textlen;i++)
168 draw_sprite(x, y, ft->sprite, text[i], 0);
169 x += 8;
172 if (ft->state == FT_SCROLL_AWAY)
173 clear_clip_rect();
176 bool FloatText::IsScrollingAway()
178 return (this->state == FT_SCROLL_AWAY);
182 void c------------------------------() {}
185 void FloatText::DrawAll(void)
187 FloatText *ft = first;
188 FloatText *nextft;
189 int count = 0;
191 while(ft)
193 nextft = ft->next;
195 if (ft->state != FT_IDLE)
197 ft->Draw();
199 else
201 if (ft->ObjectDestroyed)
202 delete ft;
205 ft = nextft;
206 count++;
210 // do NOT call this to remove all enemy's floattext from the map.
211 // for one thing, it could leave dangling invalid pointers.
212 // for another, it deletes the player->XPText, etc.
213 // instead, call ResetAll and let them clean themselves up.
214 void FloatText::DeleteAll(void)
216 while(first)
217 delete first;
220 void FloatText::ResetAll(void)
222 FloatText *ft = first;
223 while(ft)
225 ft->Reset();
226 ft = ft->next;