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

📄 invasion.cpp

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

using namespace llamaworks2d;

#include "Invasion.h"

CREATE_GAME_OBJECT(invasion);

START_MESSAGE_MAP(invasion)
    ON_WMKEYDOWN(OnKeyDown)
END_MESSAGE_MAP(invasion)


invasion::invasion() :
    worldToScreenX(0),
    worldToScreenY(44),
    worldObjects(NULL)
{
}


bool invasion::OnAppLoad()
{
    bool initOK = true;

    //
    // Initialize the window parameters.
    // If fullScreenWindow is 'false', interactive debugging
    //  with Dev-C++ will be easier.
    //
    init_params lw2dInitiParams;
    lw2dInitiParams.openParams.fullScreenWindow = false;
    lw2dInitiParams.winParams.screenMode.AddSupportedResolution(
        LWSR_800X600X32);
    lw2dInitiParams.winParams.screenMode.AddSupportedResolution(
        LWSR_800X600X24);
    lw2dInitiParams.openParams.millisecondsBetweenFrames = 50;
    lw2dInitiParams.openParams.openGLParams.surfaceBackgroundColor =
        color_rgba(0.0f,0.0f,0.0f,0.0f);

    // This call MUST appear in this function.
    theApp.InitApp(lw2dInitiParams);

    return (initOK);
}


bool invasion::InitGame()
{
    bool initOK = true;

    initOK = theCaptain.LoadResources();

    if (initOK==true)
    {
        initOK = InitLevel();
    }

    return (initOK);
}


bool invasion::InitLevel()
{
    bool initOK = true;

    // 'currentLevel' owns the memory. There's
    //  nothing to delete. Let's clear out our
    //  list of worldObjects in preparation for
    //  'currentLevel' creating the list again.
    worldObjects = NULL;

    initOK = 
        currentLevel.Load(
            "Invasion.xml",
            level_file_base::LWLFF_XML);

    if (initOK)
    {
        // Loop continuously.
        initOK = backgroundMusic.LoadWAV(
            "Prog_18_01_back.wav",
            true);
    }
    if (initOK)
    {
        backgroundMusic.Gain( 0.8);
        backgroundMusic.Play();
    }

    return (initOK);
}


// Querying for user input, updating positions,
//  executing artificial intelligence, etc.
//  Everything in world coordinates.
bool invasion::UpdateFrame()
{
    bool updateOK = true;

    GetKeyboardInput();

    world_object *oneObject = worldObjects;
    while( oneObject)
    {
        if (updateOK)
        {
            updateOK = oneObject->Update(this);
        }
        oneObject = oneObject->next;
    }

    if (updateOK)
    {
        updateOK = currentLevel.Update(this);
    }

    return (updateOK);
}


// Transforms from world to screen.
// We render back-to-front.
bool invasion::RenderFrame()
{
    // Now render the objects in the level,
    //  including the background.
    bool renderOK = currentLevel.RenderBackground(this);

    // Now render all of the world objects,
    //  including Captain Chloride.
    world_object *oneObject = worldObjects;
    while( oneObject)
    {
        if (renderOK)
        {
            if (oneObject->Visible())
            {
                renderOK = oneObject->Render(this);
            }
        }
        oneObject = oneObject->next;
    }

    // Now render foreground objects that
    //  must go on top of everything else.
    if (renderOK)
    {
        renderOK = currentLevel.RenderForeground(this);
    }
        
    return (renderOK);
}


bool invasion::OnKeyDown(
    keyboard_input_message &theMessage)
{
    switch (theMessage.keyCode)
    {
        case KC_ESCAPE:
        case KC_Q:
            GameOver(true);
        break;
    }
    return (false);
}


void invasion::GetKeyboardInput()
{
    theCaptain.GetKeyboardInput();
}


//Checks if the moving object has hit anything or has
//  hit the boundaries of the world. The coordinates are
//  in world coordinates.
//The world coordinate system points in the same direction
//  as the screen coordinate system. That is, it has the
//  origin in the upper left corner of the screen.
void invasion::CollisionCheck(
    world_object &movingObject)
{
    // first test against the world limits
    double width = movingObject.CollisionWidth();
    double height = movingObject.CollisionHeight();
    if (movingObject.WorldX() < currentLevel.MinX())
    {
        movingObject.WorldX(currentLevel.MinX());
    }
    else if (movingObject.WorldX() > currentLevel.MaxX() - width)
    {
        movingObject.WorldX(currentLevel.MaxX() - width);
    }
    
    if (movingObject.WorldY() < currentLevel.MinY())
    {
        movingObject.WorldY( currentLevel.MinY());
    }
    else if (movingObject.WorldY() > currentLevel.MaxY() - height)
    {
        movingObject.WorldY(currentLevel.MaxY() - height);
    }

    // now test against all the objects
    world_object *testObject = worldObjects;
    while( testObject)
    {
        if (testObject != &movingObject)
        {
            if (testObject->Intersects( movingObject))
            {
                movingObject.Hit( *testObject);
            }
        }
        testObject = testObject->next;
    }
}


void invasion::WorldToScreen(
    vectorf &worldPos, vector &screenPos)
{
    screenPos.X( (int)worldPos.X() + worldToScreenX);
    screenPos.Y( (int)worldPos.Y() + worldToScreenY);
}


void invasion::SetCamera(
    float worldX)
{
    float halfScreen = (float)theApp.ScreenWidth() * 0.5;
    if (worldX < currentLevel.MinX() + halfScreen)
    {
        worldX = currentLevel.MinX() + halfScreen;
    }
    else if (worldX > currentLevel.MaxX() - halfScreen)
    {
        worldX = currentLevel.MaxX() - halfScreen;
    }

    worldToScreenX = (int)(-worldX + halfScreen);
}

⌨️ 快捷键说明

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