//zxq 加入云层代码
#ifndef CLOUD_H_
#define CLOUD_H_
#pragma once
#include "hge.h"
#include "hgesprite.h"
#include "hgeanim.h"
class Cloud
{
public:
Cloud();
~Cloud();
bool OnFrame(float delta_time);
bool OnRender(float offset_x, float offset_y, float hscale, float vscale);
bool LoadGameResource();
private:
static const int kCloudFPS = 120; //zxq 修改云层播放帧率 16
static const int kCloudFrames = 20; //zxq 修改云层播放帧率 16
private:
HGE* hge_;
float _cloud_x1;//给两张云图的x坐标
float _cloud_x2;
float _cloud_y;
hgeAnimation* spr_cloud_1;//2张云图重复播放
hgeAnimation* spr_cloud_2;
HTEXTURE tex_cloud_;
float since_last_frame_;
};
#endif // WATER_H_
//zxq 加入云层代码
#include "StdAfx.h"#include "cloud.h"
#include "game_manager.h"
#define start_width 1366 //设置云层宽度
Cloud::Cloud() :_cloud_x1(0),since_last_frame_(-1),_cloud_y(0){
hge_ = hgeCreate(HGE_VERSION);
}
Cloud::~Cloud() {
hge_->Release();
}
//zxq 变更云层的X轴坐标
bool Cloud::OnFrame(float delta_time) {
if (since_last_frame_ == -1.0f) since_last_frame_ = 0.0f;
else since_last_frame_ += delta_time;
static const float kSpeed = 1.0f / kCloudFrames;
float screen_width = static_cast<float>(hge_->System_GetState(HGE_SCREENWIDTH));
while (since_last_frame_ >= kSpeed)
{
since_last_frame_ -= kSpeed;
_cloud_x1 += screen_width / (kCloudFPS * 6);
_cloud_x2 += screen_width / (kCloudFPS * 6);
}
return false;
}
//zxq 绘制云层
bool Cloud::OnRender(float offset_x, float offset_y, float hscale, float vscale) {
float screen_width = static_cast<float>(hge_->System_GetState(HGE_SCREENWIDTH));
float screen_height = static_cast<float>(hge_->System_GetState(HGE_SCREENHEIGHT));
spr_cloud_1->RenderEx(offset_x+_cloud_x1-screen_width,_cloud_y, 0.0f, hscale, vscale);
spr_cloud_2->RenderEx(offset_x+_cloud_x2-screen_width,_cloud_y, 0.0f, hscale, vscale);
if (_cloud_x2-screen_width>=screen_width)//当云2 图片已经过场 重新设置云1 和 云2 的X坐标
{
// _cloud_y=rand()%600-300; 设置绘制高度
_cloud_x1=screen_width;
_cloud_x2=0;
since_last_frame_ = -1.0f;
}
if (_cloud_x1-screen_width>=screen_width)//当云1 图片已经过场 重新设置云1 和 云2 的X坐标
{
// _cloud_y=rand()%600-300; 设置绘制高度
_cloud_x2=screen_width;
_cloud_x1=0;
since_last_frame_ = -1.0f;
}
return false;
}
//zxq 加载云层图片
bool Cloud::LoadGameResource() {
hgeResourceManager* resource_manager = GameManager::GetInstance().GetResourceManager();
spr_cloud_1 = resource_manager->GetAnimation("scene_cloud1");
spr_cloud_2= resource_manager->GetAnimation("scene_cloud2");
_cloud_x2=-static_cast<float>(hge_->System_GetState(HGE_SCREENWIDTH)) ;//初始化云2的x坐标
return true;
}