forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (74 loc) · 2.94 KB
/
index.js
File metadata and controls
85 lines (74 loc) · 2.94 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
// Script example for ScriptAPI
// Author: GlitchyTurtle32 <https://github.com/GlitchyTurtle>
// Project: https://github.com/JaylyDev/ScriptAPI
import { Player } from "@minecraft/server";
/**
* @param {import("@minecraft/server").Vector3} vector
*/
function magnitude(vector) {
return Math.sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
}
/**
* @param {import("@minecraft/server").Vector3} posA
* @param {import("@minecraft/server").Vector3} posB
*/
function calculateDistance(posA, posB) {
let direction = {
x: posA.x - posB.x,
y: posA.y - posB.y,
z: posA.z - posB.z
};
return magnitude(direction);
}
/**
* @param {import("@minecraft/server").Vector3} entityPosition
* @param {import("@minecraft/server").Vector3} pusherPosition
* @param {number} forceMagnitude
*/
function calculateKnockbackVector(entityPosition, pusherPosition, forceMagnitude) {
let direction = {
x: entityPosition.x - pusherPosition.x,
y: entityPosition.y - pusherPosition.y,
z: entityPosition.z - pusherPosition.z
};
let distance = magnitude(direction);
// Normalize the direction vector so it has a magnitude of 1
direction = {
x: direction.x / distance,
y: direction.y / distance,
z: direction.z / distance
};
// Scale the direction vector by the force magnitude to get the final knockback vector
let knockback = {
x: direction.x * forceMagnitude,
y: direction.y * forceMagnitude,
z: direction.z * forceMagnitude
};
return knockback;
}
/**
* @param {Player} player
* @param {any} spawnPos
* @param {number} strength
* @param {any} range
*/
export function createShockwave(player, spawnPos, strength, range, damageFactor = 1) {
// Create the needed variables for kb and pos
const dimension = player.dimension;
const entities = [...dimension.getEntities({ location: spawnPos, maxDistance: range, excludeNames: [player.name], excludeFamilies: ["inanimate"], excludeTypes: ["item"], excludeTags: ["anti_shockwave"] })];
const items = [...dimension.getEntities({ location: spawnPos, maxDistance: range, type: "item" })];
// Loop through all nearby entities (not items though)
entities.forEach(entity => {
// Calculate damage
const kbIntensity = strength / (1 + Math.exp(-5 * (Math.ceil(calculateDistance(entity.location, spawnPos)) - 0.5)));
const kbVector = calculateKnockbackVector(entity.location, spawnPos, kbIntensity/2);
// Apply damage and knockback
entity.applyDamage(damageFactor * Math.min(kbIntensity/2, 4));
try {
entity.applyKnockback(kbVector.x, kbVector.z, kbIntensity/4, kbIntensity/20);
} catch (error) {
entity.applyImpulse(calculateKnockbackVector(entity.location, spawnPos, 2));
}
});
items.forEach(item => { item.applyImpulse(calculateKnockbackVector(item.location, spawnPos, 3)); });
}