forked from renokun/bball
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.ts
More file actions
134 lines (107 loc) · 3.69 KB
/
shared.ts
File metadata and controls
134 lines (107 loc) · 3.69 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
128
129
130
131
132
133
134
export const maxPlayersPerTeam = 3;
export const tickInterval = 1000 / 20;
export const matchDurationTicks = 20 * 60 * 5;
export const court = {
width: 12,
depth: 8,
border: 1
};
export const shoulderY = 0.9;
export const armLength = 0.6;
export const resetBallDuration = 60;
export const jump = {
durationNoBall: 25,
boostNoBall: 0.5,
durationWithBall: 20,
boostWithBall: 0.4,
gravity: 0.05
};
export const basket = {
y: 3.75,
width: 3,
height: 1.5
};
export const ballPhysics = {
initialY: 2,
radius: 0.3,
catchRadius: 0.5,
drag: 0.98,
bounce: 0.6,
gravity: 0.04
};
export function getArmPosition(avatar: Game.AvatarPub) {
const x = avatar.x + Math.cos(avatar.angleY) * Math.cos(avatar.angleX) * armLength;
const z = avatar.z + Math.sin(avatar.angleY) * Math.cos(avatar.angleX) * armLength;
const y = (shoulderY + getAvatarY(avatar.jump) + Math.sin(avatar.angleX) * armLength);
return { x, y, z };
}
export function getAvatarY(jumpState: Game.AvatarJump) {
if (jumpState.timer === 0) return 0;
const n = (jumpState.withBall ? jump.durationWithBall : jump.durationNoBall) - jumpState.timer;
const boost = (jumpState.withBall ? jump.boostWithBall : jump.boostNoBall);
return Math.max(0, boost * n - jump.gravity * (n - 1) * n / 2);
}
const ballXmin = -court.width / 2 - court.border + ballPhysics.radius;
const ballXmax = court.width / 2 + court.border - ballPhysics.radius;
const ballZmin = -court.depth / 2 - court.border + ballPhysics.radius;
const ballZmax = court.depth / 2 - court.border + ballPhysics.radius;
const basketYmin = basket.y - basket.height / 2;
const basketYmax = basket.y + basket.height / 2;
const basketZmin = -basket.width / 2;
const basketZmax = basket.width / 2;
export function tickBall(ball: Game.BallPub) {
let bounce = 0;
let hitBasketTeamIndex: number = null;
ball.x += ball.vx;
if (ball.x < ballXmin) {
if (ball.y > basketYmin && ball.y < basketYmax &&
ball.z > basketZmin && ball.z < basketZmax) {
hitBasketTeamIndex = 1;
}
ball.x = ballXmin + (ballXmin - ball.x) * ballPhysics.bounce;
bounce = Math.abs(ball.vx);
ball.vx = -ball.vx * ballPhysics.bounce;
} else if (ball.x > ballXmax) {
if (ball.y > basketYmin && ball.y < basketYmax &&
ball.z > basketZmin && ball.z < basketZmax) {
hitBasketTeamIndex = 0;
}
ball.x = ballXmax + (ballXmax - ball.x) * ballPhysics.bounce;
bounce = Math.abs(ball.vx);
ball.vx = -ball.vx * ballPhysics.bounce;
}
ball.z += ball.vz;
if (ball.z < ballZmin) {
ball.z = ballZmin + (ballZmin - ball.z) * ballPhysics.bounce;
bounce = Math.abs(ball.vz);
ball.vz = -ball.vz * ballPhysics.bounce;
} else if (ball.z > ballZmax) {
ball.z = ballZmax + (ballZmax - ball.z) * ballPhysics.bounce;
bounce = Math.abs(ball.vz);
ball.vz = -ball.vz * ballPhysics.bounce;
}
ball.vx *= ballPhysics.drag;
ball.vz *= ballPhysics.drag;
if (ball.y !== ballPhysics.radius || ball.vy !== 0) {
ball.y += ball.vy;
ball.vy -= ballPhysics.gravity;
if (ball.y < ballPhysics.radius) {
bounce = Math.abs(ball.vy);
if (ball.vy > -0.2) {
ball.y = ballPhysics.radius;
ball.vy = 0;
} else {
ball.y = ballPhysics.radius + (ballPhysics.radius - ball.y) * ballPhysics.bounce * ballPhysics.bounce;
ball.vy = -ball.vy * ballPhysics.bounce;
}
}
}
return { bounce, hitBasketTeamIndex };
}
export function resetBall(ball: Game.BallPub) {
ball.x = ball.z = 0;
ball.y = ballPhysics.initialY;
ball.vx = ball.vz = 0;
ball.vy = 0.3;
ball.playerId = null;
}