feat:二维向量类,Animation类
This commit is contained in:
@@ -136,6 +136,7 @@
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="animation.h" />
|
||||
<ClInclude Include="config_manager.h" />
|
||||
<ClInclude Include="enemy_type.h" />
|
||||
<ClInclude Include="game_manager.h" />
|
||||
@@ -145,6 +146,7 @@
|
||||
<ClInclude Include="route.h" />
|
||||
<ClInclude Include="tile.h" />
|
||||
<ClInclude Include="timer.h" />
|
||||
<ClInclude Include="vector2.h" />
|
||||
<ClInclude Include="wave.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
|
@@ -62,5 +62,11 @@
|
||||
<ClInclude Include="timer.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="animation.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="vector2.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
109
TdGame/TdGame/animation.h
Normal file
109
TdGame/TdGame/animation.h
Normal file
@@ -0,0 +1,109 @@
|
||||
#pragma once
|
||||
#ifndef _ANIMATION_H_
|
||||
#define _ANIMATION_H_
|
||||
|
||||
#include "timer.h"
|
||||
|
||||
#include <SDL.h>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
|
||||
class Animation
|
||||
{
|
||||
public:
|
||||
//<2F><>ʾһ<CABE><D2BB><EFBFBD><EFBFBD><EFBFBD>Դ洢<D4B4>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD><DEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DEB7><EFBFBD>ֵ<EFBFBD>Ŀɵ<C4BF><C9B5>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͡<EFBFBD>
|
||||
typedef std::function<void()> PlayCallBack;
|
||||
public:
|
||||
Animation()
|
||||
{
|
||||
timer.set_one_shot(false);
|
||||
timer.set_on_timeout
|
||||
(
|
||||
//[&]<5D><>ʾ lambda <20><><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> lambda <20><><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>ʹ<EFBFBD>ò<EFBFBD><C3B2><EFBFBD><DEB8><EFBFBD>Щ<EFBFBD><D0A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
[&]()
|
||||
{
|
||||
idx_frame++;
|
||||
if (idx_frame >= rect_src_list.size())
|
||||
{
|
||||
idx_frame = is_loop ? 0 : rect_src_list.size() - 1;
|
||||
if (!is_loop && on_finished)
|
||||
on_finished();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
~Animation() = default;
|
||||
|
||||
void reset()
|
||||
{
|
||||
timer.restart();
|
||||
|
||||
idx_frame = 0;
|
||||
}
|
||||
|
||||
void set_frame_data(SDL_Texture* texture, int num_x, int num_y, const std::vector<int>& idx_list)
|
||||
{
|
||||
int width_tex, height_tex;
|
||||
|
||||
this->texture = texture;
|
||||
SDL_QueryTexture(texture, nullptr, nullptr, &width_tex, &height_tex);
|
||||
width_frame = width_tex / num_x, height_frame = height_tex / num_y;
|
||||
|
||||
rect_src_list.resize(idx_list.size());
|
||||
for (size_t i = 0; i < idx_list.size(); i++)
|
||||
{
|
||||
int idx = idx_list[i];
|
||||
SDL_Rect& rect_src = rect_src_list[i];
|
||||
|
||||
rect_src.x = (idx % num_x) * width_frame;
|
||||
rect_src.y = (idx / num_x) * height_frame;
|
||||
rect_src.w = width_frame; rect_src.h = height_frame;
|
||||
};
|
||||
}
|
||||
|
||||
void set_loop(bool is_loop)
|
||||
{
|
||||
this->is_loop = is_loop;
|
||||
}
|
||||
|
||||
void set_interval(double interval)
|
||||
{
|
||||
timer.set_wait_time(interval);
|
||||
}
|
||||
|
||||
void set_on_finished(PlayCallBack on_finished)
|
||||
{
|
||||
this->on_finished = on_finished;
|
||||
}
|
||||
|
||||
void on_update(double delta)
|
||||
{
|
||||
timer.on_update(delta);
|
||||
}
|
||||
//<2F><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦλ<C8BE>ã<EFBFBD><C3A3>Ƕȣ<C7B6><C8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>
|
||||
void on_render(SDL_Renderer* renderer, const SDL_Point& pos_dst, double angle = 0)const
|
||||
{
|
||||
static SDL_Rect rect_dst;
|
||||
|
||||
rect_dst.x = pos_dst.x; rect_dst.y = pos_dst.y;
|
||||
rect_dst.w = width_frame; rect_dst.h = height_frame;
|
||||
|
||||
//<2F><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><C3BC><EFBFBD><EFBFBD>Σ<EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD>Σ<EFBFBD><CEA3>Ƕȣ<C7B6><C8A3><EFBFBD>ת<EFBFBD><D7AA><EFBFBD>ĵ<EFBFBD>(Ĭ<><C4AC>1/2)<29><><EFBFBD><EFBFBD>ת
|
||||
SDL_RenderCopyEx(renderer, texture, &rect_src_list[idx_frame], &rect_dst, 0, nullptr, SDL_RendererFlip::SDL_FLIP_NONE);
|
||||
}
|
||||
private:
|
||||
Timer timer;
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD>
|
||||
bool is_loop = true;
|
||||
//<2F><>¼<EFBFBD><C2BC>ǰ<EFBFBD><C7B0><EFBFBD>ŵڼ<C5B5>֡
|
||||
size_t idx_frame = 0;
|
||||
PlayCallBack on_finished;
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
SDL_Texture* texture = nullptr;
|
||||
std::vector<SDL_Rect> rect_src_list;
|
||||
int width_frame = 0, height_frame = 0;
|
||||
};
|
||||
|
||||
|
||||
#endif // ! _ANIMATION_H_
|
@@ -50,6 +50,7 @@ public:
|
||||
{
|
||||
bool can_shot = (!one_shot || (one_shot && !shotted));
|
||||
shotted = true;
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD>
|
||||
if (can_shot && on_timeout)
|
||||
on_timeout;
|
||||
|
||||
@@ -59,16 +60,16 @@ public:
|
||||
private:
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
||||
double pass_time = 0;
|
||||
//<2F>ȴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ǰ<EFBFBD>ȴ<EFBFBD>ʱ<EFBFBD><EFBFBD>
|
||||
double wait_time = 0;
|
||||
//<2F><>ע<EFBFBD><D7A2>ʱ<EFBFBD><CAB1><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ͣ
|
||||
bool paused = false;
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD><C7B5>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٴδ<D9B4><CEB4><EFBFBD>
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD><C7B5>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٴδ<EFBFBD><EFBFBD><EFBFBD>
|
||||
bool shotted = false;
|
||||
//<2F>Ƿ<EFBFBD><C7B7>ǵ<EFBFBD><C7B5>δ<EFBFBD><CEB4><EFBFBD>
|
||||
bool one_shot = false;
|
||||
|
||||
//<2F>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>ʱӦ<CAB1>ô<EFBFBD><C3B4><EFBFBD>ʲô
|
||||
std::function<void()> on_timeout;
|
||||
};
|
||||
|
||||
|
88
TdGame/TdGame/vector2.h
Normal file
88
TdGame/TdGame/vector2.h
Normal file
@@ -0,0 +1,88 @@
|
||||
#pragma once
|
||||
#ifndef _VECTOR2_H_
|
||||
#define _VECTOR2_H_
|
||||
|
||||
#include <cmath>
|
||||
|
||||
class Vector2
|
||||
{
|
||||
public:
|
||||
double x = 0;
|
||||
double y = 0;
|
||||
|
||||
public:
|
||||
Vector2() = default;
|
||||
~Vector2() = default;
|
||||
|
||||
Vector2(double x, double y)
|
||||
:x(x), y(y) {}
|
||||
|
||||
Vector2 operator+(const Vector2& vec) const
|
||||
{
|
||||
return Vector2(x + vec.x, y + vec.y);
|
||||
}
|
||||
|
||||
void operator+=(const Vector2& vec)
|
||||
{
|
||||
x += vec.x; y += vec.y;
|
||||
}
|
||||
|
||||
Vector2 operator-(const Vector2& vec) const
|
||||
{
|
||||
return Vector2(x - vec.x, y - vec.y);
|
||||
}
|
||||
|
||||
void operator-=(const Vector2& vec)
|
||||
{
|
||||
x -= vec.x; y -= vec.y;
|
||||
}
|
||||
|
||||
Vector2 operator*(const Vector2& vec)const
|
||||
{
|
||||
return Vector2(x * vec.x , y* vec.y);
|
||||
}
|
||||
|
||||
void operator*=(double val)
|
||||
{
|
||||
x *= val, y *= val;
|
||||
}
|
||||
|
||||
bool operator==(const Vector2& vec)const
|
||||
{
|
||||
return x == vec.x && y == vec.y;
|
||||
}
|
||||
|
||||
bool operator>(const Vector2& vec)const
|
||||
{
|
||||
return length() > vec.length();
|
||||
}
|
||||
|
||||
bool operator<(const Vector2& vec)const
|
||||
{
|
||||
return length() < vec.length();
|
||||
}
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
double length() const
|
||||
{
|
||||
return sqrt(x * x + y * y);
|
||||
}
|
||||
|
||||
//<2F><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>
|
||||
Vector2 normalize() const
|
||||
{
|
||||
double len = length();
|
||||
|
||||
if (len == 0)
|
||||
return Vector2(0, 0);
|
||||
|
||||
return Vector2(x / len, y / len);
|
||||
}
|
||||
|
||||
bool approx_zero() const
|
||||
{
|
||||
return length() < 0.00001;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user