-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield.cpp
More file actions
129 lines (108 loc) · 3.64 KB
/
field.cpp
File metadata and controls
129 lines (108 loc) · 3.64 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
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
#include "game.h"
#include "field.h"
// Implementation of all the functions that are declared in field.h
vector3df cubePosition(int x, int y, bool isWall){
f32 vpos = isWall ? fieldsize : 0.f;
return vector3df(fieldsize*y+halffieldsize,vpos,fieldsize*x+halffieldsize);
};
// Constructor of class Field
Field::Field(ISceneManager* smgr,
IVideoDriver* driver,
int x, int y, playground pg)
{
this->smgr=smgr;
this->driver=driver;
this->x=x;
this->y=y;
this->pg=pg;
}
// inquiry functions
int Field::getx(){ return x; }
int Field::gety(){ return y; }
// Constructor of class Wall
Wall::Wall(ISceneManager* smgr,
IVideoDriver* driver,
int x, int y, playground pg)
: Field(smgr,driver,x,y,pg)
// calling constructor of parent class
{
// instances of Wall may use a different texture or even several of them.
this->texture=driver->getTexture(texturepath+"st-blue-sand.png");
// build a block and paint it with texture
block = smgr->addCubeSceneNode(fieldsize);
block->setMaterialTexture(0,texture);
block->setPosition(cubePosition(x,y,true));
block->setMaterialFlag(video::EMF_LIGHTING, false);
}
// Walls are by default reflecting (i.e. pushing the sphere back)
bool Wall::reflects(){ return true; }
void Wall::sphereEnter(Sphere &s){
// do nothing
}
void Wall::sphereExit(Sphere &s){
// do nothing
}
// The handleSphere method should only be called when the sphere has
// entered a field, which should actually not happen for a solid wall.
// However, our collision detection is not perfect and it happens.
// We then just print out a message and play as if it was a Floor---using
// the standardSphereProgress method of the sphere.
void Wall::handleSphere(Sphere &s, position2di mousemove, f32 frameDeltaTime){
cerr << "Bug: sphere has managed to enter a solid wall.\n";
s.standardSphereProgress(mousemove, frameDeltaTime, stdfriction);
};
void Wall::timeProgress(f32 frameDeltaTime){
// do nothing
};
// Overlap of the sphere with this Wall: here happens the standard
// collision detection---reflecting the sphere by two times the amount
// of the overlap
void Wall::sphereOverlap(Sphere &s, f32 xoverlap, f32 yoverlap){
vector3df p=s.getPosition();
vector3df v=s.getVelocity();
if (xoverlap!=0){
v.Z= -v.Z;
p.Z += 2*xoverlap;
}
if (yoverlap!=0){
v.X= -v.X;
p.X += 2*yoverlap;
}
s.setPosition(p);
s.setVelocity(v);
}
// the field type of the parent class is just tWall
fieldtype Wall::getFieldType(){ return tWall; }
//////////////////////////////
// constructor of Floor
Floor::Floor(ISceneManager* smgr,
IVideoDriver* driver,
int x, int y, playground pg)
: Field(smgr,driver,x,y,pg) // calling constructor of parent class
{
texture=driver->getTexture(texturepath+"fl-acblack.png");
block = smgr->addCubeSceneNode(fieldsize);
block->setMaterialTexture(0,texture);
block->setPosition(cubePosition(x,y,false));
block->setMaterialFlag(video::EMF_LIGHTING, false);
}
// Floors are by default not reflecting the Sphere
bool Floor::reflects(){ return false; }
void Floor::sphereEnter(Sphere &s){
// do nothing
}
void Floor::sphereExit(Sphere &s){
// do nothing
}
// this is called when the sphere is on this Floor for the amount frameDeltaTime
// usually, one should just use the standardSphereProgress method of the sphere.
void Floor::handleSphere(Sphere &s, position2di mousemove, f32 frameDeltaTime){
s.standardSphereProgress(mousemove, frameDeltaTime, stdfriction);
};
void Floor::timeProgress(f32 frameDeltaTime){
// do nothing
};
void Floor::sphereOverlap(Sphere &s, f32 xoverlap, f32 yoverlap){
// do nothing
}
fieldtype Floor::getFieldType(){ return tFloor; }