-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevent.hpp
113 lines (91 loc) · 2.65 KB
/
event.hpp
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
#ifndef _EVENT_HPP_
#define _EVENT_HPP_
#include "particle.hpp"
/*
* There're three events we use in the simulation:
* - Refresh event: stands for refreshing the screen
* - Wall collision event: represents the moment a
* particle collides with wall
* - Particle collision event: represents the moment
* a particle collides another particle
*/
enum class EventType {Refresh, WallCollision, ParticleCollision};
// Basic abstract class for all events
class Event {
protected:
double time; // the time the event will happen
EventType ev_type; // the type of the event
public:
explicit Event(double time, EventType type) : time(time), ev_type(type) {};
virtual ~Event() {};
virtual double getTime() const {
return time;
}
virtual EventType getType() const {
return ev_type;
}
// returns true if the event is outdated/cancelled
virtual bool isStale() const = 0;
};
class RefreshEvent : public Event {
public:
explicit RefreshEvent(double time) : Event(time, EventType::Refresh) {};
virtual ~RefreshEvent() {};
// this event can not be cancelled
bool isStale() const { return false; }
};
class WallCollisionEvent : public Event {
protected:
Particle *p; // a particle
WallType wtype; // the type of wall particle will collide
int p_rev;
public:
WallCollisionEvent(double time, Particle &p, WallType wtype)
: Event(time, EventType::WallCollision) {
this->p = &p;
this->wtype = wtype;
p_rev = p.getRevision();
};
virtual ~WallCollisionEvent() {};
// the event is considered to be stale if
// the particle collides something before
bool isStale() const {
return (p_rev != p->getRevision());
}
WallType getWallType() const {
return wtype;
}
Particle& getParticle() const {
return *p;
}
};
class ParticleCollisionEvent : public Event {
protected:
Particle *pa;
Particle *pb;
int pa_rev;
int pb_rev;
public:
ParticleCollisionEvent(double time, Particle &pa, Particle &pb)
: Event(time, EventType::ParticleCollision) {
this->pa = &pa;
this->pb = &pb;
pa_rev = pa.getRevision();
pb_rev = pb.getRevision();
}
virtual ~ParticleCollisionEvent() {};
// the event is considered to be stale if
// one of particles collides somethign
// before.
bool isStale() const {
return ((pa_rev != pa->getRevision()) ||
(pb_rev != pb->getRevision()));
}
Particle &getFirstParticle() const {
return *pa;
}
Particle &getSecondParticle() const {
return *pb;
}
};
#endif /* _EVENT_HPP_ */