mirror of
https://github.com/AquariaOSE/Aquaria.git
synced 2025-02-25 15:33:57 +00:00
This commit introduces pointer checks to various functions; so that entity_* will no longer crash or produce weird results if passed a Node pointer, etc. The checks are disabled by default, but can be enabled in ScriptInterface.cpp. Fixed possible crashes in a few more functions due to missing NULL-checks. There was a "feature" in the single Lua state that it would keep globals intact until the game was quit. That made any globals from mods "leak" into the game or other mods. Now it resets the Lua state when a mod is loaded or closed.
187 lines
3.6 KiB
C++
187 lines
3.6 KiB
C++
/*
|
|
Copyright (C) 2007, 2010 - Bit-Blot
|
|
|
|
This file is part of Aquaria.
|
|
|
|
Aquaria is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU General Public License
|
|
as published by the Free Software Foundation; either version 2
|
|
of the License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
*/
|
|
#include "Game.h"
|
|
#include "Avatar.h"
|
|
|
|
|
|
int IngredientData::getIndex() const
|
|
{
|
|
return dsq->continuity.indexOfIngredientData(this);
|
|
}
|
|
|
|
bool IngredientData::hasIET(IngredientEffectType iet)
|
|
{
|
|
for (IngredientEffects::iterator i = effects.begin(); i != effects.end(); i++)
|
|
{
|
|
if ((*i).type == iet)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
Ingredient::Ingredient(const Vector &pos, IngredientData *data, int amount)
|
|
: Entity(), data(data), amount(amount), gone(false), used(false)
|
|
{
|
|
addType(SCO_INGREDIENT);
|
|
entityType = ET_INGREDIENT;
|
|
position = pos;
|
|
lifeSpan = 30;
|
|
if (data)
|
|
{
|
|
setTexture("Ingredients/"+data->gfx);
|
|
}
|
|
int mag = 600;
|
|
if (isRotKind())
|
|
velocity = randVector(mag)*0.5f + Vector(0, -mag)*0.5f;
|
|
else
|
|
velocity = Vector(0,-mag*0.5f);
|
|
gravity = Vector(0, 250); //300
|
|
scale = Vector(0.2,0.2);
|
|
scale.interpolateTo(Vector(1, 1), 0.75);
|
|
|
|
if (isRotKind())
|
|
rotation.z = randAngle360();
|
|
|
|
layer = LR_ENTITIES;
|
|
}
|
|
|
|
bool Ingredient::hasIET(IngredientEffectType iet)
|
|
{
|
|
if (data)
|
|
return data->hasIET(iet);
|
|
return false;
|
|
}
|
|
|
|
void Ingredient::destroy()
|
|
{
|
|
Entity::destroy();
|
|
dsq->game->removeIngredient(this);
|
|
}
|
|
|
|
bool Ingredient::isRotKind()
|
|
{
|
|
if (data)
|
|
{
|
|
if (data->type == IT_OIL || data->type == IT_EGG)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
IngredientData *Ingredient::getIngredientData()
|
|
{
|
|
return data;
|
|
}
|
|
|
|
void Ingredient::eat(Entity *e)
|
|
{
|
|
safeKill();
|
|
|
|
dsq->spawnParticleEffect("IngredientCollect", position);
|
|
}
|
|
|
|
void Ingredient::onUpdate(float dt)
|
|
{
|
|
if (dsq->game->isPaused()) return;
|
|
if (dsq->continuity.getWorldType() == WT_SPIRIT) return;
|
|
|
|
Vector lastPosition = position;
|
|
Entity::onUpdate(dt);
|
|
|
|
if (dsq->game->collideCircleWithGrid(position, 24))
|
|
{
|
|
position = lastPosition;
|
|
/*
|
|
if (velocity.x < velocity.y)
|
|
velocity.x = -velocity.x;
|
|
else
|
|
velocity.y = -velocity.y;
|
|
*/
|
|
velocity = 0;
|
|
}
|
|
|
|
if (lifeSpan > 0)
|
|
{
|
|
lifeSpan -= dt;
|
|
if (lifeSpan <= 0)
|
|
{
|
|
lifeSpan = 0;
|
|
gone = true;
|
|
this->scale.interpolateTo(Vector(0,0),1);
|
|
setLife(1);
|
|
setDecayRate(1);
|
|
}
|
|
}
|
|
|
|
Vector diff = (dsq->game->avatar->position - position);
|
|
if (diff.isLength2DIn(64))
|
|
{
|
|
if (scale.x == 1)
|
|
{
|
|
// got
|
|
safeKill();
|
|
|
|
dsq->continuity.pickupIngredient(data, 1);
|
|
|
|
dsq->game->pickupIngredientEffects(data);
|
|
|
|
dsq->spawnParticleEffect("IngredientCollect", position);
|
|
|
|
dsq->sound->playSfx("pickup-ingredient");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
float len = 1024;
|
|
if (dsq->game->avatar->isRolling() && diff.isLength2DIn(len))
|
|
{
|
|
float maxSpeed = 1500;
|
|
Vector maxV = diff;
|
|
maxV.setLength2D(len);
|
|
diff = maxV - diff;
|
|
diff *= maxSpeed/len;
|
|
velocity += diff * 1.5f * dt;
|
|
}
|
|
}
|
|
|
|
velocity.capLength2D(1000);
|
|
|
|
if (isRotKind() && !velocity.isZero())
|
|
{
|
|
int mag = velocity.getLength2D();
|
|
if (velocity.x > 0)
|
|
rotation.z += mag*0.01f;
|
|
else
|
|
rotation.z -= mag*0.01f;
|
|
}
|
|
|
|
|
|
Vector sub = velocity;
|
|
if (!sub.isZero())
|
|
{
|
|
sub.setLength2D(100*dt);
|
|
velocity -= sub;
|
|
}
|
|
// collision?
|
|
}
|