-
Notifications
You must be signed in to change notification settings - Fork 0
/
anna-game.anna
executable file
·128 lines (112 loc) · 2.32 KB
/
anna-game.anna
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
#! /usr/bin/env anna
use(annaGame);
use(annaGame.scene);
use(linalg);
use(actor);
use(world);
def Float time()
{
tv := unix.time.TimeVal();
tz := unix.time.TimeZone();
unix.time.gettimeofday(tv, tz);
return tv.sec + 0.000001* tv.usec;
}
def handleInput(World w)
{
use(annaGame.screen);
p := w.player;
expandCode(
p[Action::%action] = keyGet(key.%key),
action: [walkForward, walkBackward, turnLeft, turnRight],
key: [up, down, left, right]);
}
def main()
{
screen.init(640, 480, 0);
render.init();
s := scene.Scene("anna", 1);
w := World(s);
player := Player(
world:w,
data: ActorData(turnSpeed: 3.0, walkSpeed: 8.0, reactionSpeed: 10.0),
name: "Bengt",
pos: Vec(100.0, 100.0, 0.0),
);
w.player = player;
// print("AAA", primitives.ballTypeLoad("anna","ball1"), "\n");
w.scene.cameraZ = 3.0;
w.scene.cameraX = 40.0;
w.scene.cameraY = 40.0;
prev := time();
framerate := 30.0;
i := 1;
while(1)
{
tm := time();
dt := tm -prev;
framerate = 0.95 * framerate + 0.05/dt;
if(i ^mod 300 == 0)
{
print("Framerate is % fps" % [framerate]);
}
use(screen);
use(math);
checkInput();
if(keyGet(key.escape))
{
break;
}
handleInput(w);
w.step(dt);
// w.scene.cameraX += dt;
// w.scene.cameraZ = w.scene.getHeight(w.scene.cameraX, w.scene.cameraY)+3.0;
// w.scene.cameraAngle += 0.05;
ca := s.cameraAngle*pi/180;
if(player[Action::walkForward])
{
pi2 :== 2.0*math.pi;
acs := 1.0 - 0.3*dt;
pa := player.angle;
if(pa > ca+math.pi)
{
ca = acs*ca + (1.0-acs)*(pa-pi2);
if (ca < 0.0)
{
ca = ca + pi2;
}
else
{
if (pa < ca-pi)
{
ca = acs*ca + (1.0-acs)*(pa+pi2);
if (ca > pi2)
{
ca = ca - pi2;
}
}
}
}
else
{
ca = acs*ca + (1.0-acs)*pa;
}
s.cameraAngle = ca*180/math.pi;
}
cameraDistance :== 5.0;
cPos := player.pos.copy();
cPos[0] -= cameraDistance * math.cos(ca);
cPos[1] -= cameraDistance * math.sin(ca);
def max(Float a, Float b)
{
if(a>b){ a } else { b }
}
cPos[2] = max(cPos[2], s.getHeight(cPos[0],cPos[1])) + 8;
w.camera = w.camera*0.9 + cPos*0.1;
w.scene.render();
// print(w.player.pos, "\n");
screen.swapBuffers();
prev = tm;
i++;
}
print("Bye");
}