-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll_game.cpp
128 lines (110 loc) · 2.99 KB
/
scroll_game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "pch.h"
#include "scroll_game.h"
#include "game_obj.h"
namespace sf {
scroll_game::scroll_game()
{
init();
}
void scroll_game::init()
{
for(auto&i : objs_)
{
i.reset(nullptr);
}
world_.reset(new b2World(b2Vec2(0.0f,-40.0f)));
{
b2BodyDef bodydef;
/*
bodydef.position.Set(0.0f,10.0f);
ground_body_ = world_.CreateBody(&bodydef);
b2PolygonShape ground_box;
ground_box.SetAsBox(20.0f,2.0f);
ground_fixture_ = ground_body_->CreateFixture(&ground_box,1.0f);
ground_box.SetAsBox(30.0f,2.0f);
bodydef.position.Set(-60.0f,-50.0f);
world_.CreateBody(&bodydef)->CreateFixture(&ground_box,1.0f);
bodydef.position.Set(80.0f,-40.0f);
world_.CreateBody(&bodydef)->CreateFixture(&ground_box,1.0f);
*/
float32 xpos[10] = {0.0f,100.0f,110.0f,150.0f,170.0f,200.0f,1000.0f,2000.0f,2010.0f,2500.0f};
float32 ypos[10] = {-100.0f,-50.0f,-70.0f,-40.0f,-60.0f,-70.0f,-100.0f,-60.0f,-70.0f,-100.0f};
std::vector<b2Vec2> points;
for(int i = 0;i < 8 * 10;++i)
{
b2Vec2 tmp;
tmp.x = i * 100.0f;
tmp.y = ypos[i % 8];
points.push_back(tmp);
}
b2EdgeShape edge;
b2ChainShape chain;
chain.CreateChain(&points[0],points.size());
edge.Set(b2Vec2(0.0f,0.0f),b2Vec2(40.0f,0.0f));
// ground_box.SetAsBox(20.0f,2.0f);
bodydef.position.Set(-70.0f,0.0f);
world_->CreateBody(&bodydef)->CreateFixture(&chain,1.0f);
scroll_offset_ = 0.0f;
}
velocity_iterations_ = 6;
position_iterations_ = 2;
time_step_ = 1.0f /60.0f;
tick_ = 0;
}
void scroll_game::step(float32 delta_time)
{
static int step_count = 0,index = 0;
if(!(step_count & 0x07)){
for(int i = 0;i < objs_.size();++i)
{
if(index == objs_.size())
{
index = 0;
}
if(!objs_[index])
{
objs_[index].reset(new game_obj(*this));
++index;
break;
} else {
++index;
}
}
}
for(int i = 0;i < objs_.size();++i)
{
if(objs_[i])
{
objs_[i]->move(delta_time);
if(!objs_[i]->is_used())
{
objs_[i].reset(nullptr);
}
}
}
world_->Step(delta_time,velocity_iterations_,position_iterations_);
world_->ClearForces();
step_count++;
tick_ = GetTickCount64();
scroll_offset_ += 0.4f;
screen_actual_aabb_ = screen_aabb_;
screen_actual_aabb_.lowerBound.x += scroll_offset_;
screen_actual_aabb_.upperBound.x += scroll_offset_;
world_->ClearForces();
// b2Vec2 pos = body_->GetPosition();
// float32 angle = body_->GetAngle();
// sf::debug_out(boost::wformat(L"posx:%4.2f posy:%4.2f angle:%4.2f\n") % pos.x % pos.y % angle);
}
scroll_game::~scroll_game()
{
world_.reset();
}
void scroll_game::screen_aabb(float width,float height,float scale)
{
screen_aabb_.lowerBound.Set(-width / scale,-height / scale );
screen_aabb_.upperBound.Set(width / scale ,height /scale);
screen_actual_aabb_ = screen_aabb_;
screen_actual_aabb_.lowerBound.x += scroll_offset_;
screen_actual_aabb_.upperBound.x += scroll_offset_;
}
}