-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim_test.cpp
More file actions
83 lines (69 loc) · 1.75 KB
/
sim_test.cpp
File metadata and controls
83 lines (69 loc) · 1.75 KB
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
#include <t_simulaltion.hpp>
#include <iostream>
#include <math.h>
#include <map>
using namespace tcx;
class Car: public sim::Object{
public:
double pos = 0;
double speed = 0;
double acceleration = 0;
bool forward = true;
Car(){}
void move(){
speed+=acceleration;
if(speed < 0) {
speed = 0;
acceleration = 0;
}
double dist = speed*1;
if(forward) pos+=dist;
else pos-=dist;
}
sim::Coro start(){
env->log("%X start on pos: %d forward: %d",this,pos,forward);
co_await env->sleep(this,3);
acceleration = 3;
env->log("%X start drive",this);
co_await env->sleep(this,5);
acceleration = 0;
env->log("%X reach highest speed",this);
co_await env->sleep(this,10);
acceleration = -1;
env->log("%X slowing down",this);
co_await env->sleep(this,10);
env->log("%X keep same speed",this);
acceleration = 0;
}
void stop(){}
void tick(size_t step_){
move();
env->log("%X at pos:%0.f",this,pos);
}
};
class Collision:public sim::Object{
public:
Car* cars[2];
sim::Coro start(){co_await std::suspend_never{};};
void stop(){};
void tick(size_t step){
if(std::abs(cars[0]->pos - cars[1]->pos) <=20){
env->log("collided");
cars[0]->speed = 0;
cars[1]->speed = 0;
}
}
};
int main(){
sim::Env env;
Car* car1= env.emplace_obj<Car>();
Car* car2= env.emplace_obj<Car>();
Collision* coll = env.emplace_obj<Collision>();
coll->cars[0]=car1;
coll->cars[1]=car2;
car1->pos = 0;
car1->forward = true;
car2->pos = 1000;
car2->forward = false;
env.run(80);
}