Example #1
0
	int8_t Mob::update(const Physics& physics)
	{
		if (!active)
			return phobj.fhlayer;

		bool aniend = animations.at(stance).update();
		if (aniend && stance == DIE)
		{
			dead = true;
		}

		if (fading)
		{
			opacity -= 0.025f;
			if (opacity.last() < 0.025f)
			{
				opacity.set(0.0f);
				fading = false;
				dead = true;
			}
		}
		else if (fadein)
		{
			opacity += 0.025f;
			if (opacity.last() > 0.975f)
			{
				opacity.set(1.0f);
				fadein = false;
			}
		}

		if (dead)
		{
			active = false;
			return -1;
		}

		effects.update();
		showhp.update();

		if (!dying)
		{
			if (!canfly)
			{
				if (phobj.flagnotset(PhysicsObject::TURNATEDGES))
				{
					flip = !flip;
					phobj.setflag(PhysicsObject::TURNATEDGES);

					if (stance == HIT)
						setstance(STAND);
				}
			}

			switch (stance)
			{
			case MOVE:
				if (canfly)
				{
					phobj.hforce = flip ? flyspeed : -flyspeed;
					switch (flydirection)
					{
					case UPWARDS:
						phobj.vforce = -flyspeed;
						break;
					case DOWNWARDS:
						phobj.vforce = flyspeed;
						break;
					}
				}
				else
				{
					phobj.hforce = flip ? speed : -speed;
				}
				break;
			case HIT:
				if (canmove)
				{
					double KBFORCE = phobj.onground ? 0.2 : 0.1;
					phobj.hforce = flip ? -KBFORCE : KBFORCE;
				}
				break;
			case JUMP:
				phobj.vforce = -5.0;
				break;
			}

			physics.moveobject(phobj);

			if (control)
			{
				counter++;

				bool next;
				switch (stance)
				{
				case HIT:
					next = counter > 200;
					break;
				case JUMP:
					next = phobj.onground;
					break;
				default:
					next = aniend && counter > 200;
					break;
				}

				if (next)
				{
					nextmove();
					updatemovement();
					counter = 0;
				}
			}
		}
		else
		{
			phobj.normalize();
			physics.getfht().updatefh(phobj);
		}

		return phobj.fhlayer;
	}
Example #2
0
	int8_t Mob::update(const Physics& physics)
	{
		if (!active)
			return phobj.fhlayer;

		bool aniend = animations.at(stance).update();
		if (aniend && dying)
		{
			dead = true;
		}

		if (fading)
		{
			opacity -= 0.025f;
			if (opacity.last() < 0.025f)
			{
				opacity.set(0.0f);
				fading = false;
				dead = true;
			}
		}
		else if (fadein)
		{
			opacity += 0.025f;
			if (opacity.last() > 0.975f)
			{
				opacity.set(1.0f);
				fadein = false;
			}
		}

		Point<int16_t> head = getheadpos(getposition());
		bulletlist.remove_if([&](pair<Bullet, AttackEffect>& bulletattack) {
			bool apply = bulletattack.first.update(head);
			if (apply)
			{
				applyattack(bulletattack.second);
			}
			return apply;
		});
		bool nobullets = bulletlist.size() == 0;

		singlelist.remove_if([&](SingleEffect& single) {
			if (single.delay <= Constants::TIMESTEP)
			{
				applysingle(single);
				return true;
			}
			else
			{
				single.delay -= Constants::TIMESTEP;
				return false;
			}
		});
		bool nosingle = singlelist.size() == 0;

		size_t remove = 0;
		for (auto& dmg : damagenumbers)
		{
			if (dmg.update())
				remove++;
		}
		for (size_t i = remove; i--;)
		{
			damagenumbers.erase(damagenumbers.begin());
		}
		bool nonumbers = damagenumbers.size() == 0;

		if (dead && nobullets && nosingle && nonumbers)
		{
			active = false;
			return -1;
		}

		effects.update();
		showhp.update();

		if (control && !dying)
		{
			if (!canfly)
			{
				if (phobj.flagnotset(PhysicsObject::TURNATEDGES))
				{
					flip = !flip;
					phobj.setflag(PhysicsObject::TURNATEDGES);

					if (stance == HIT)
						setstance(STAND);
				}
			}

			switch (stance)
			{
			case MOVE:
				if (canfly)
				{
					phobj.hforce = flip ? flyspeed : -flyspeed;
					switch (flydirection)
					{
					case UPWARDS:
						phobj.vforce = -flyspeed;
						break;
					case DOWNWARDS:
						phobj.vforce = flyspeed;
						break;
					}
				}
				else
				{
					phobj.hforce = flip ? speed : -speed;
					if (canjump && phobj.onground && counter > 50 && counter < 150 && randomizer.below(0.005f))
					{
						phobj.vforce = -5.0f;
					}
				}
				break;
			case HIT:
				if (canmove)
				{
					double KBFORCE = phobj.onground ? 0.2 : 0.1;
					phobj.hforce = flip ? -KBFORCE : KBFORCE;
				}
				break;
			}

			counter++;
			if (aniend && counter > 200)
			{
				nextmove();
				sendmovement();
				counter = 0;
			}

			physics.moveobject(phobj);
		}
		else
		{
			phobj.normalize();
			physics.getfht().updatefh(phobj);
		}

		return phobj.fhlayer;
	}