1 /*
2 Copyright (C) 2007, 2010 - Bit-Blot
3
4 This file is part of Aquaria.
5
6 Aquaria is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
15 See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21 #include "Game.h"
22 #include "Avatar.h"
23
IngredientData(const std::string & name,const std::string & gfx,IngredientType type)24 IngredientData::IngredientData(const std::string &name, const std::string &gfx, IngredientType type)
25 : name(name), gfx(gfx), amount(0), maxAmount(MAX_INGREDIENT_AMOUNT), held(0), type(type), marked(0), sorted(false)
26 , displayName(dsq->continuity.getIngredientDisplayName(name))
27 , rotKind(!(type == IT_OIL || type == IT_EGG))
28 {
29 }
30
getIndex() const31 int IngredientData::getIndex() const
32 {
33 return dsq->continuity.indexOfIngredientData(this);
34 }
35
hasIET(IngredientEffectType iet)36 bool IngredientData::hasIET(IngredientEffectType iet)
37 {
38 for (IngredientEffects::iterator i = effects.begin(); i != effects.end(); i++)
39 {
40 if ((*i).type == iet)
41 return true;
42 }
43 return false;
44 }
45
Ingredient(const Vector & pos,IngredientData * data,int amount)46 Ingredient::Ingredient(const Vector &pos, IngredientData *data, int amount)
47 : Entity(), data(data), amount(amount), gone(false), used(false)
48 {
49 addType(SCO_INGREDIENT);
50 entityType = ET_INGREDIENT;
51 position = pos;
52 lifeSpan = 30;
53 if (data)
54 {
55 setTexture("Ingredients/"+data->gfx);
56 }
57 int mag = 600;
58 if (isRotKind())
59 velocity = randVector(mag)*0.5f + Vector(0, -mag)*0.5f;
60 else
61 velocity = Vector(0,-mag*0.5f);
62 gravity = Vector(0, 250); //300
63 scale = Vector(0.2,0.2);
64 scale.interpolateTo(Vector(1, 1), 0.75);
65
66 if (isRotKind())
67 rotation.z = randAngle360();
68
69 layer = LR_ENTITIES;
70 }
71
hasIET(IngredientEffectType iet)72 bool Ingredient::hasIET(IngredientEffectType iet)
73 {
74 if (data)
75 return data->hasIET(iet);
76 return false;
77 }
78
destroy()79 void Ingredient::destroy()
80 {
81 Entity::destroy();
82 dsq->game->removeIngredient(this);
83 }
84
isRotKind()85 bool Ingredient::isRotKind()
86 {
87 return data && data->rotKind;
88 }
89
getIngredientData()90 IngredientData *Ingredient::getIngredientData()
91 {
92 return data;
93 }
94
eat(Entity * e)95 void Ingredient::eat(Entity *e)
96 {
97 safeKill();
98
99 dsq->spawnParticleEffect("IngredientCollect", position);
100 }
101
onUpdate(float dt)102 void Ingredient::onUpdate(float dt)
103 {
104 if (dsq->game->isPaused()) return;
105 if (dsq->game->isWorldPaused()) return;
106
107 Vector lastPosition = position;
108 Entity::onUpdate(dt);
109
110 if (dsq->game->collideCircleWithGrid(position, 24))
111 {
112 position = lastPosition;
113 /*
114 if (velocity.x < velocity.y)
115 velocity.x = -velocity.x;
116 else
117 velocity.y = -velocity.y;
118 */
119 velocity = 0;
120 }
121
122 if (lifeSpan > 0)
123 {
124 lifeSpan -= dt;
125 if (lifeSpan <= 0)
126 {
127 lifeSpan = 0;
128 gone = true;
129 this->scale.interpolateTo(Vector(0,0),1);
130 setLife(1);
131 setDecayRate(1);
132 }
133 }
134
135 Vector diff = (dsq->game->avatar->position - position);
136 if (diff.isLength2DIn(64))
137 {
138 if (scale.x == 1)
139 {
140 // got
141 safeKill();
142
143 dsq->continuity.pickupIngredient(data, 1);
144
145 dsq->game->pickupIngredientEffects(data);
146
147 dsq->spawnParticleEffect("IngredientCollect", position);
148
149 dsq->sound->playSfx("pickup-ingredient");
150 }
151 }
152 else
153 {
154 float len = 1024;
155 if (dsq->game->avatar->isRolling() && diff.isLength2DIn(len))
156 {
157 float maxSpeed = 1500;
158 Vector maxV = diff;
159 maxV.setLength2D(len);
160 diff = maxV - diff;
161 diff *= maxSpeed/len;
162 velocity += diff * 1.5f * dt;
163 }
164 }
165
166 velocity.capLength2D(1000);
167
168 if (isRotKind() && !velocity.isZero())
169 {
170 int mag = velocity.getLength2D();
171 if (velocity.x > 0)
172 rotation.z += mag*0.01f;
173 else
174 rotation.z -= mag*0.01f;
175 }
176
177
178 Vector sub = velocity;
179 if (!sub.isZero())
180 {
181 sub.setLength2D(100*dt);
182 velocity -= sub;
183 }
184 // collision?
185 }
186