📄 chloride.cpp
字号:
#include "LlamaWorks2D.h"
using namespace llamaworks2d;
#include "Invasion.h"
#include "Door.h"
#include "Key.h"
#include "Slug.h"
chloride::chloride() :
velocity(0.0f,0.0f),
currState(STATE_STAND),
lastWalkState(STATE_WALK_LEFT),
lastStepIndex(0),
haveKey(false)
{
}
bool
chloride::LoadResources()
{
bool loadOK = true;
animation *theAnimation = new animation;
if (theAnimation!=NULL)
{
theAnimation->BitmapTransparentColor(
color_rgb(0.0f,1.0f,0.0f));
loadOK = theAnimation->LoadImage(
"ChlorideLeft1.bmp",
image_file_base::LWIFF_WINDOWS_BMP);
}
else
{
loadOK = false;
theApp.AppError(LWES_OUT_OF_MEMORY);
}
if (loadOK)
{
loadOK = theAnimation->LoadImage(
"ChlorideLeft2.bmp",
image_file_base::LWIFF_WINDOWS_BMP);
}
if (loadOK)
{
loadOK = theAnimation->LoadImage(
"ChlorideLeft3.bmp",
image_file_base::LWIFF_WINDOWS_BMP);
}
if (loadOK)
{
loadOK = theAnimation->LoadImage(
"ChlorideLeft4.bmp",
image_file_base::LWIFF_WINDOWS_BMP);
}
if (loadOK)
{
loadOK = theAnimation->LoadImage(
"ChlorideLeft5.bmp",
image_file_base::LWIFF_WINDOWS_BMP);
}
if (loadOK)
{
theAnimation->LoopAnimation(
true,
animation::FORWARD);
// Add the animation to the animated sprite.
theCaptain.Animation(theAnimation);
}
if (loadOK)
{
theAnimation = new animation;
if (theAnimation!=NULL)
{
theAnimation->BitmapTransparentColor(
color_rgb(0.0f,1.0f,0.0f));
loadOK = theAnimation->LoadImage(
"ChlorideRight1.bmp",
image_file_base::LWIFF_WINDOWS_BMP);
}
else
{
loadOK = false;
theApp.AppError(LWES_OUT_OF_MEMORY);
}
}
if (loadOK)
{
loadOK = theAnimation->LoadImage(
"ChlorideRight2.bmp",
image_file_base::LWIFF_WINDOWS_BMP);
}
if (loadOK)
{
loadOK = theAnimation->LoadImage(
"ChlorideRight3.bmp",
image_file_base::LWIFF_WINDOWS_BMP);
}
if (loadOK)
{
loadOK = theAnimation->LoadImage(
"ChlorideRight4.bmp",
image_file_base::LWIFF_WINDOWS_BMP);
}
if (loadOK)
{
loadOK = theAnimation->LoadImage(
"ChlorideRight5.bmp",
image_file_base::LWIFF_WINDOWS_BMP);
}
if (loadOK)
{
theAnimation->LoopAnimation(
true,
animation::FORWARD);
// Add the animation to the animated sprite.
theCaptain.Animation(theAnimation);
}
if (loadOK)
{
loadOK = leftWalkSound.LoadWAV(
"Walk1.wav");
}
if (loadOK)
{
leftWalkSound.Gain(1.0f);
}
if (loadOK)
{
loadOK = rightWalkSound.LoadWAV(
"Walk2.wav");
}
if (loadOK)
{
rightWalkSound.Gain(1.0f);
}
if (loadOK)
{
loadOK = bonkSound.LoadWAV(
"Bunk.wav");
}
if (loadOK)
{
loadOK = pickupSound.LoadWAV(
"Blip.wav");
}
if (loadOK)
{
// the art is extra wide to make it next
// power-of-2.
CollisionWidth( (float)67.0);
CollisionHeight( (float)BitmapHeight());
}
return (loadOK);
}
void chloride::GetKeyboardInput()
{
vectorf captainDirection;
// for now, Chloride isn't allowed to move
// up and down.
/*
if (theApp.IsKeyPressed(KC_UP_ARROW))
{
captainDirection.Y(-1);
}
else if (theApp.IsKeyPressed(KC_DOWN_ARROW))
{
captainDirection.Y( 1);
}
*/
if (theApp.IsKeyPressed(KC_LEFT_ARROW))
{
captainDirection.X(-1);
}
else if (theApp.IsKeyPressed(KC_RIGHT_ARROW))
{
captainDirection.X( 1);
}
if (captainDirection.MagnitudeSquared() == 0.0)
{
currState = STATE_STAND;
}
else
{
// Make the direction be a unit vector, in
// case he is moving diagonally, then scale
// it by the speed.
captainDirection = captainDirection.Normalize();
captainDirection *= 15.0;
if (captainDirection.X() < 0.0)
{
currState = STATE_WALK_LEFT;
}
else if (captainDirection.X() > 0.0)
{
currState = STATE_WALK_RIGHT;
}
else
{
currState = lastWalkState;
}
}
Movement(captainDirection);
}
void chloride::Movement(
vectorf new_velocity)
{
velocity = new_velocity;
}
bool
chloride::Update(
invasion *theGame)
{
// update the world position.
worldPos += velocity;
// correct the world position if necessary.
if (velocity.MagnitudeSquared() > 0.0)
theGame->CollisionCheck( *this);
// update the "camera".
// this is the world position that we would like
// to be in the center of the screen if the world
// had no boundaries.
float cameraWorldX =
WorldX() + 0.5 * CollisionWidth();
theGame->SetCamera(cameraWorldX);
return (true);
}
bool
chloride::Render(
invasion *theGame)
{
bool renderOK = true;
int currentAnimation;
switch( currState)
{
case STATE_STAND:
// pause the animation
currentAnimation = theCaptain.CurrentAnimation();
theCaptain.Animation(
currentAnimation)->CurrentFrame(
lastStepIndex);
break;
case STATE_WALK_LEFT:
lastWalkState = STATE_WALK_LEFT;
theCaptain.CurrentAnimation(ANIM_WALK_LEFT);
lastStepIndex =
theCaptain.Animation(ANIM_WALK_LEFT)->CurrentFrame();
// When the foot touches the ground, play sound.
if (lastStepIndex == 3)
{
leftWalkSound.Play();
}
break;
case STATE_WALK_RIGHT:
lastWalkState = STATE_WALK_RIGHT;
theCaptain.CurrentAnimation(ANIM_WALK_RIGHT);
lastStepIndex =
theCaptain.Animation(ANIM_WALK_RIGHT)->CurrentFrame();
// when the foot touches the ground, play sound.
if (lastStepIndex == 3)
{
rightWalkSound.Play();
}
break;
default:
ASSERT( 0); //Unhandled state!
break;
}
// Transform from world to screen and render.
vector screenPos;
theGame->WorldToScreen(worldPos,screenPos);
theCaptain.X(screenPos.X());
theCaptain.Y(screenPos.Y());
theCaptain.Render();
return (renderOK);
}
void chloride::Hit( world_object &stationary)
{
if (stationary.Type() == "door")
{
door *theDoor = (door*)&stationary;
if (haveKey)
{
theDoor->Open( this);
// Hold on to the key; the door will close
// automatically and we might still need it.
}
// We hit a door. Go back to previous position
// and play a sound.
worldPos -= velocity;
// Need a way to know if this is still playing
// and to not play it again until the current
// instance has finished.
bonkSound.Play();
}
else if (stationary.Type() == "key")
{
key *theKey = (key*)&stationary;
theKey->Visible(false);
theKey->Collidable(false);
pickupSound.Play();
haveKey = true;
}
else if (stationary.Type() == "slug")
{
// We hit a slug. Go back to previous position.
worldPos -= velocity;
slug *theSlug = (slug*)&stationary;
theSlug->WakeUp( this);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -