2024-08-19 01:56:12 -05:00
|
|
|
|
#pragma region License
|
|
|
|
|
/*
|
|
|
|
|
License (OLC-3)
|
|
|
|
|
~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Copyright 2024 Joshua Sigona <sigonasr2@gmail.com>
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
|
|
1. Redistributions or derivations of source code must retain the above copyright
|
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
|
|
2. Redistributions or derivative works in binary form must reproduce the above
|
|
|
|
|
copyright notice. This list of conditions and the following disclaimer must be
|
|
|
|
|
reproduced in the documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
|
|
3. Neither the name of the copyright holder nor the names of its contributors may
|
|
|
|
|
be used to endorse or promote products derived from this software without specific
|
|
|
|
|
prior written permission.
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
|
|
|
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
|
|
|
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
|
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
|
|
|
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
|
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
Portions of this software are copyright <EFBFBD> 2024 The FreeType
|
|
|
|
|
Project (www.freetype.org). Please see LICENSE_FT.txt for more information.
|
|
|
|
|
All rights reserved.
|
|
|
|
|
*/
|
|
|
|
|
#pragma endregion
|
|
|
|
|
|
|
|
|
|
#include "HamsterJet.h"
|
|
|
|
|
#include "Hamster.h"
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
|
HamsterJet::HamsterJet(Hamster&hamster)
|
|
|
|
|
:hamster(hamster),hamsterOriginalPos(hamster.GetPos()),pos({hamster.GetPos().x-128.f,hamster.GetPos().y+32.f}),z(3.f),state(SWOOP_DOWN),timer(3.f){
|
|
|
|
|
jet.Initialize("hamster_jet.png",{78,223,208},{79,81,128});
|
|
|
|
|
lights.Initialize("hamster_jet.png",{245,233,130},{245,233,130});
|
|
|
|
|
}
|
|
|
|
|
void HamsterJet::Update(const float fElapsedTime){
|
|
|
|
|
jet.Update(fElapsedTime);
|
|
|
|
|
lights.Update(fElapsedTime);
|
|
|
|
|
timer=std::max(0.f,timer-fElapsedTime);
|
2024-08-19 14:57:12 -05:00
|
|
|
|
easeInTimer=std::max(0.f,easeInTimer-fElapsedTime);
|
2024-08-19 14:13:50 -05:00
|
|
|
|
lastTappedSpace+=fElapsedTime;
|
2024-08-19 01:56:12 -05:00
|
|
|
|
switch(state){
|
|
|
|
|
case SWOOP_DOWN:{
|
|
|
|
|
HamsterGame::Game().SetZoom(1.5f);
|
|
|
|
|
z=util::lerp(0.f,3.f,std::pow(timer/3.f,2));
|
|
|
|
|
vf2d originalPos{hamster.GetPos().x-128.f,hamster.GetPos().y+32.f};
|
|
|
|
|
if(timer<=0.4f){
|
|
|
|
|
hamster.SetPos(hamsterOriginalPos-vf2d{0.f,sin(float(geom2d::pi)*timer/0.4f)*8.f});
|
2024-08-19 14:13:50 -05:00
|
|
|
|
hamster.SetZ(sin(float(geom2d::pi)*timer/0.4f)*0.2f);
|
2024-08-19 14:57:12 -05:00
|
|
|
|
jetState[TOP_LEFT]=jetState[BOTTOM_LEFT]=jetState[BOTTOM_RIGHT]=jetState[TOP_RIGHT]=OFF;
|
2024-08-19 14:13:50 -05:00
|
|
|
|
}else{
|
|
|
|
|
jetState[TOP_LEFT]=jetState[BOTTOM_LEFT]=jetState[BOTTOM_RIGHT]=jetState[TOP_RIGHT]=ON;
|
|
|
|
|
pos=hamster.GetPos().lerp(originalPos,std::pow(timer/3.f,4));
|
2024-08-19 01:56:12 -05:00
|
|
|
|
}
|
|
|
|
|
if(timer<=0.f){
|
|
|
|
|
state=RISE_UP;
|
|
|
|
|
hamster.SetPos(pos);
|
|
|
|
|
this->originalPos=pos;
|
|
|
|
|
targetPos=pos+vf2d{128.f,32};
|
|
|
|
|
targetZ=8.f;
|
|
|
|
|
timer=3.f;
|
|
|
|
|
}
|
|
|
|
|
}break;
|
|
|
|
|
case RISE_UP:{
|
2024-08-19 14:13:50 -05:00
|
|
|
|
jetState[TOP_LEFT]=jetState[BOTTOM_LEFT]=jetState[BOTTOM_RIGHT]=jetState[TOP_RIGHT]=ON;
|
2024-08-19 01:56:12 -05:00
|
|
|
|
pos=targetPos.lerp(originalPos,std::sqrt(timer/3.f));
|
|
|
|
|
z=util::lerp(targetZ,0.f,timer/3.f);
|
|
|
|
|
hamster.SetPos(pos);
|
2024-08-19 14:13:50 -05:00
|
|
|
|
hamster.SetZ(z+0.03f);
|
2024-08-19 01:56:12 -05:00
|
|
|
|
if(timer<=0.f){
|
|
|
|
|
state=PLAYER_CONTROL;
|
2024-08-19 14:57:12 -05:00
|
|
|
|
HamsterGame::Game().SetZoom(0.6f);
|
|
|
|
|
easeInTimer=0.6f;
|
2024-08-19 01:56:12 -05:00
|
|
|
|
}
|
|
|
|
|
}break;
|
2024-08-19 14:13:50 -05:00
|
|
|
|
case PLAYER_CONTROL:{
|
|
|
|
|
jetState[TOP_LEFT]=jetState[BOTTOM_LEFT]=jetState[BOTTOM_RIGHT]=jetState[TOP_RIGHT]=OFF;
|
|
|
|
|
HandleJetControls();
|
|
|
|
|
pos=hamster.GetPos();
|
|
|
|
|
}break;
|
2024-08-19 01:56:12 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void HamsterJet::Draw(){
|
2024-08-19 14:57:12 -05:00
|
|
|
|
float drawingOffsetY{0.f};
|
|
|
|
|
hamster.SetDrawingOffsetY(0.f);
|
|
|
|
|
if((state==PLAYER_CONTROL||state==LANDING)&&z>2.f){
|
|
|
|
|
HamsterGame::Game().SetZ(z/2.f);
|
|
|
|
|
HamsterGame::Game().tv.DrawRotatedDecal(pos,HamsterGame::GetGFX("aimingTarget.png").Decal(),0.f,HamsterGame::GetGFX("aimingTarget.png").Sprite()->Size()/2);
|
|
|
|
|
}
|
|
|
|
|
if(state==PLAYER_CONTROL){
|
|
|
|
|
drawingOffsetY=util::lerp(48.f,0.f,easeInTimer/0.6f);
|
|
|
|
|
hamster.SetDrawingOffsetY(util::lerp(48.f,0.f,easeInTimer/0.6f));
|
|
|
|
|
}
|
2024-08-19 01:56:12 -05:00
|
|
|
|
HamsterGame::Game().SetZ(z);
|
2024-08-19 14:57:12 -05:00
|
|
|
|
HamsterGame::Game().tv.DrawPartialRotatedDecal(pos+vf2d{0,drawingOffsetY},jet.Decal(),0.f,{24,24},{},{48,48});
|
2024-08-19 14:13:50 -05:00
|
|
|
|
const Animate2D::FrameSequence&flameAnim{HamsterGame::Game().GetAnimation("hamster_jet.png",HamsterGame::AnimationState::JET_FLAMES)};
|
|
|
|
|
const Animate2D::Frame&flameFrame{flameAnim.GetFrame(HamsterGame::Game().GetRuntime())};
|
|
|
|
|
HamsterGame::Game().SetZ(z+0.01f);
|
2024-08-19 14:57:12 -05:00
|
|
|
|
if(jetState[TOP_LEFT])HamsterGame::Game().tv.DrawPartialRotatedDecal(pos+vf2d{0,drawingOffsetY},flameFrame.GetSourceImage()->Decal(),0.f,flameFrame.GetSourceRect().size/2,flameFrame.GetSourceRect().pos+vf2d{0,0},flameFrame.GetSourceRect().size/2);
|
|
|
|
|
if(jetState[BOTTOM_LEFT])HamsterGame::Game().tv.DrawPartialRotatedDecal(pos+vf2d{0,drawingOffsetY},flameFrame.GetSourceImage()->Decal(),0.f,{24,0},flameFrame.GetSourceRect().pos+vf2d{0,24},flameFrame.GetSourceRect().size/2);
|
|
|
|
|
if(jetState[BOTTOM_RIGHT])HamsterGame::Game().tv.DrawPartialRotatedDecal(pos+vf2d{0,drawingOffsetY},flameFrame.GetSourceImage()->Decal(),0.f,{0,0},flameFrame.GetSourceRect().pos+vf2d{24,24},flameFrame.GetSourceRect().size/2);
|
|
|
|
|
if(jetState[TOP_RIGHT])HamsterGame::Game().tv.DrawPartialRotatedDecal(pos+vf2d{0,drawingOffsetY},flameFrame.GetSourceImage()->Decal(),0.f,{0,24},flameFrame.GetSourceRect().pos+vf2d{24,0},flameFrame.GetSourceRect().size/2);
|
2024-08-19 01:56:12 -05:00
|
|
|
|
const Animate2D::FrameSequence&lightAnim{HamsterGame::Game().GetAnimation("hamster_jet.png",HamsterGame::AnimationState::JET_LIGHTS)};
|
|
|
|
|
const Animate2D::Frame&lightFrame{lightAnim.GetFrame(HamsterGame::Game().GetRuntime())};
|
2024-08-19 14:13:50 -05:00
|
|
|
|
HamsterGame::Game().SetZ(z+0.02f);
|
2024-08-19 14:57:12 -05:00
|
|
|
|
HamsterGame::Game().tv.DrawPartialRotatedDecal(pos+vf2d{0,drawingOffsetY},lights.Decal(),0.f,lightFrame.GetSourceRect().size/2.f,lightFrame.GetSourceRect().pos,lightFrame.GetSourceRect().size);
|
2024-08-19 01:56:12 -05:00
|
|
|
|
HamsterGame::Game().SetZ(0.f);
|
2024-08-19 14:13:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HamsterJet::HandleJetControls(){
|
|
|
|
|
lastTappedSpace+=HamsterGame::Game().GetElapsedTime();
|
|
|
|
|
vf2d aimingDir{};
|
|
|
|
|
if(HamsterGame::Game().GetKey(W).bHeld){
|
|
|
|
|
aimingDir+=vf2d{0,-1};
|
|
|
|
|
jetState[BOTTOM_RIGHT]=ON;
|
|
|
|
|
jetState[BOTTOM_LEFT]=ON;
|
|
|
|
|
}
|
|
|
|
|
if(HamsterGame::Game().GetKey(D).bHeld){
|
|
|
|
|
aimingDir+=vf2d{1,0};
|
|
|
|
|
jetState[BOTTOM_LEFT]=ON;
|
|
|
|
|
jetState[TOP_LEFT]=ON;
|
|
|
|
|
}
|
|
|
|
|
if(HamsterGame::Game().GetKey(S).bHeld){
|
|
|
|
|
aimingDir+=vf2d{0,1};
|
|
|
|
|
jetState[TOP_LEFT]=ON;
|
|
|
|
|
jetState[TOP_RIGHT]=ON;
|
|
|
|
|
}
|
|
|
|
|
if(HamsterGame::Game().GetKey(A).bHeld){
|
|
|
|
|
aimingDir+=vf2d{-1,0};
|
|
|
|
|
jetState[BOTTOM_RIGHT]=ON;
|
|
|
|
|
jetState[TOP_RIGHT]=ON;
|
|
|
|
|
}
|
|
|
|
|
if(aimingDir!=vf2d{}){
|
|
|
|
|
hamster.targetRot=aimingDir.norm().polar().y;
|
|
|
|
|
const vf2d currentVel{hamster.vel};
|
|
|
|
|
hamster.vel+=vf2d{currentVel.polar().x+(hamster.GetMaxSpeed()*HamsterGame::Game().GetElapsedTime())/hamster.GetTimeToMaxSpeed(),hamster.rot}.cart();
|
|
|
|
|
hamster.vel=vf2d{std::min(hamster.GetMaxSpeed(),hamster.vel.polar().x),hamster.vel.polar().y}.cart();
|
|
|
|
|
hamster.frictionEnabled=false;
|
|
|
|
|
}
|
|
|
|
|
if(HamsterGame::Game().GetKey(SPACE).bPressed){
|
|
|
|
|
if(lastTappedSpace<=0.6f){
|
|
|
|
|
state=LANDING;
|
|
|
|
|
}
|
|
|
|
|
lastTappedSpace=0.f;
|
|
|
|
|
}
|
2024-08-19 01:56:12 -05:00
|
|
|
|
}
|