⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 door.cpp

📁 c++游戏源码 素材文件 需要openAl32.dll可以在网上下载
💻 CPP
字号:
#include "LlamaWorks2D.h"

using namespace llamaworks2d;

#include "Invasion.h"
#include "Door.h"
#include "Slug.h"

// How fast does this door move?
// Must be a positive number.
const float SPEED = 10.0;

// How far away must the person who opened the
//  door be before it starts closing again?
const float SAFETY = 50.0;

// What's the world y coordinate when we're fully
//  open? Closed?
const float OPEN_Y = 72.0;
const float CLOSED_Y = 243.0;

door::door() :
    currState(STATE_CLOSED),
    opener(NULL)
{
}

bool door::LoadResources()
{
    bool loadOK = true;

    if (loadOK)
    {
        loadOK = openingSound.LoadWAV(
            "DoorOpen.wav");
    }

    if (loadOK)
    {
        loadOK = closingSound.LoadWAV(
            "DoorClose.wav");
    }

    return (loadOK);
}

bool door::Update( invasion *theGame)
{
    worldPos += velocity;
    if (currState == STATE_OPENING)
    {
        if (worldPos.Y() <= OPEN_Y)
        {
            currState = STATE_OPEN;
            velocity.Y( 0.0);
        }
    }
    else if (currState == STATE_OPEN)
    {
        // when whoever opened us is out of the way,
        //  we can start closing again. Remember that
        //  the world position is the upper left corner.
        ASSERT( opener);
        if (opener->WorldX() + opener->CollisionWidth() <
            WorldX() - SAFETY
            ||
            opener->WorldX() >
            WorldX() + CollisionWidth() + SAFETY )
        {
            currState = STATE_CLOSING;
            velocity.Y( SPEED);
            closingSound.Play();
        }
    }
    else if (currState == STATE_CLOSING)
    {
        if (worldPos.Y() >= CLOSED_Y)
        {
            worldPos.Y( CLOSED_Y);
            velocity.Y( 0.0);
            currState = STATE_CLOSED;
        }

        // If we hit something, call our Hit function
        //  and open up again.
        theGame->CollisionCheck( *this);
    }
    return (true);
}

bool door::Render( invasion *theGame)
{
    bool renderOK = true;
    vector screenPos;
    theGame->WorldToScreen(worldPos,screenPos);
    mySprite.X(screenPos.X());
    mySprite.Y(screenPos.Y());
    renderOK = mySprite.Render();
    
    return (renderOK);
}

// We have the key, open the door!
void door::Open( world_object *new_opener)
{
    // Whether we're closing or closed, we need to open
    //  up. And stay open until the 'opener' is out of
    //  the way.
    currState = STATE_OPENING;
    velocity.Y( -SPEED);
    opener = new_opener;
    openingSound.Play();
}

// We hit something that was collidable. Reverse direction!
void door::Hit( world_object &stationary)
{
    Open( &stationary);

    // Slugwroths are weak!
    if (stationary.Type() == "slug")
    {
        slug *theSlug = (slug*)&stationary;
        theSlug->Kill();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -