lenix 1.8.14 → 1.8.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ ![NPM Downloads](https://img.shields.io/npm/d18m/lenix)
package/client/cam.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Vec4 } from '../shared/types.ts'
1
+ import type { Vec2, Vec4 } from '../shared/types.ts'
2
2
 
3
3
  const cams = new Set<number>()
4
4
 
@@ -6,35 +6,36 @@ export interface BaseCamDetails {
6
6
  /**
7
7
  * Fade-out duration in milliseconds.
8
8
  */
9
- fadeOut?: number
9
+ fadeOut: number
10
10
  /**
11
11
  * Fade-in duration in milliseconds.
12
12
  */
13
- fadeIn?: number
13
+ fadeIn: number
14
14
  /**
15
15
  * Delay before the camera state changes.
16
16
  */
17
- delay?: number
17
+ delay: number
18
18
  }
19
19
 
20
20
  export interface CreateCamSettings {
21
21
  /**
22
- * Camera coordinates and heading.
22
+ * Camera coordinates and heading (0 - 180)!.
23
23
  */
24
24
  coords: Vec4
25
+ /**
26
+ * Camera offset from coordinates and heading (0 - 180)!.
27
+ */
28
+ offset?: Vec4
25
29
  /**
26
30
  * Camera vertical and horizontal rotation.
27
31
  */
28
- rotation: {
29
- vertical: number,
30
- horizontal: number
31
- }
32
+ rotation?: Vec2
32
33
  /**
33
34
  * Optional camera field-of-view, rotation order, and fade settings.
34
35
  */
35
36
  details?: {
36
- fov?: number,
37
- rotationOrder?: number
37
+ fov: number,
38
+ rotationOrder: number
38
39
  } & BaseCamDetails
39
40
  }
40
41
 
@@ -75,27 +76,31 @@ const toggleCam = ({
75
76
  */
76
77
  const create = ({
77
78
  coords,
78
- rotation: {
79
- vertical,
80
- horizontal
81
- },
82
- details
83
- }: CreateCamSettings): number => {
84
- const fov = details?.fov ?? 40.0,
85
- fadeOut = details?.fadeOut ?? 0,
86
- fadeIn = details?.fadeIn ?? 0,
87
- delay = details?.delay ?? 0,
88
- rotationOrder = details?.rotationOrder ?? 0
89
-
79
+ offset = [0, 0, 0, 0],
80
+ rotation = [0, 0],
81
+ details: {
82
+ fov,
83
+ fadeOut,
84
+ fadeIn,
85
+ delay,
86
+ rotationOrder
87
+ } = {
88
+ fov: 40.0,
89
+ fadeOut: 0,
90
+ fadeIn: 0,
91
+ delay: 0,
92
+ rotationOrder: 0
93
+ }
94
+ }: CreateCamSettings): [() => void, number] => {
90
95
  DoScreenFadeOut(fadeOut)
91
96
  const cam = CreateCamWithParams(
92
97
  'DEFAULT_SCRIPTED_CAMERA',
93
- coords[0],
94
- coords[1],
95
- coords[2],
96
- vertical,
97
- horizontal,
98
- coords[3],
98
+ coords[0] + offset[0],
99
+ coords[1] + offset[1],
100
+ coords[2] + offset[2],
101
+ rotation[0],
102
+ rotation[1],
103
+ coords[3] + offset[3],
99
104
  fov,
100
105
  false,
101
106
  rotationOrder
@@ -110,7 +115,10 @@ const create = ({
110
115
  fadeOut
111
116
  })
112
117
 
113
- return cam
118
+ return [
119
+ () => destroy({ cam, details: { delay, fadeIn, fadeOut } }),
120
+ cam
121
+ ]
114
122
  }
115
123
 
116
124
  /**
@@ -118,11 +126,16 @@ const create = ({
118
126
  */
119
127
  const destroy = ({
120
128
  cam,
121
- details
129
+ details: {
130
+ fadeOut,
131
+ fadeIn,
132
+ delay
133
+ } = {
134
+ fadeOut: 0,
135
+ fadeIn: 0,
136
+ delay: 0
137
+ }
122
138
  }: DestroyCamSettings): void => {
123
- const fadeOut = details?.fadeOut ?? 0,
124
- fadeIn = details?.fadeIn ?? 0,
125
- delay = details?.delay ?? 0
126
139
 
127
140
  toggleCam({
128
141
  cam,
package/client/client.ts CHANGED
@@ -78,14 +78,14 @@ export const entity = {
78
78
  blendIn = 2.0,
79
79
  blendOut = 2.0,
80
80
  duration = -1,
81
- flag = 0,
81
+ flag = 1,
82
82
  playbackFrom = 0.0,
83
83
  x = false,
84
84
  y = false,
85
85
  z = false
86
86
  ) => {
87
87
  RequestAnimDict(dict)
88
- await repeat(() => HasAnimDictLoaded(dict), true)
88
+ await repeat(() => HasAnimDictLoaded(dict) ? 1 : null, 1)
89
89
  TaskPlayAnim(ped, dict, name, blendIn, blendOut, duration, flag, playbackFrom, x, y, z)
90
90
  },
91
91
 
package/client/event.ts CHANGED
@@ -12,4 +12,8 @@ export const onEvent = <
12
12
  >(event: Event, cb: (...params: Params) => void) => {
13
13
  addEventListener(event, cb)
14
14
  return () => removeEventListener(event, cb)
15
- }
15
+ }
16
+
17
+ export const emitNetEvent = <Event extends string, Params extends unknown[]>(event: Event, ...params: Params) => emitNet(event, params)
18
+
19
+ export const emitEvent = <Event extends string, Params extends unknown[]>(event: Event, ...params: Params) => emit(event, params)
package/client/nearest.ts CHANGED
@@ -1,12 +1,13 @@
1
1
  import type { Vec3 } from '../shared/types.ts'
2
2
  import { entity } from './client.ts'
3
+ import { player as client } from './client.ts'
3
4
 
4
5
  /**
5
6
  * Finds the nearest coordinates from a list.
6
7
  */
7
8
  const coords = (coords: Vec3, zones: Vec3[]): Vec3 | undefined => {
8
9
  let closest: Vec3 | undefined
9
- let closestDistance = Infinity
10
+ let radialSpace = Infinity
10
11
 
11
12
  for (const zone of zones) {
12
13
  const dx = zone[0] - coords[0]
@@ -14,8 +15,8 @@ const coords = (coords: Vec3, zones: Vec3[]): Vec3 | undefined => {
14
15
  const dz = zone[2] - coords[2]
15
16
  const distance = dx * dx + dy * dy + dz * dz
16
17
 
17
- if (distance < closestDistance) {
18
- closestDistance = distance
18
+ if (distance < radialSpace) {
19
+ radialSpace = distance
19
20
  closest = zone
20
21
  }
21
22
  }
@@ -30,65 +31,44 @@ const player = (
30
31
  coords: Vec3,
31
32
  maxDistance = 2.0,
32
33
  includePlayer = false
33
- ): {
34
- playerId?: number,
35
- playerPed?: number,
36
- playerCoords?: Vec3,
37
- playerVehicle?: number
38
- } => {
39
- const players = GetActivePlayers()
40
-
41
- let closestId: number | undefined
42
- let closestPed: number | undefined
43
- let closestCoords: Vec3 | undefined
44
- let closestVehicle: number | undefined
45
-
46
- for (let i = 0; i < players.length; i++) {
47
- const playerId = players[i]
48
-
49
- if (playerId !== PlayerPedId() || includePlayer) {
34
+ ): number | null => {
35
+
36
+ let closestId
37
+
38
+ for (const playerId of GetActivePlayers() as number[]) {
39
+
40
+ if (playerId !== client.id() || includePlayer) {
50
41
  const playerPed = GetPlayerPed(playerId)
51
42
  const vehicle = GetVehiclePedIsIn(playerPed, false)
52
- const playerCoords: Vec3 = vehicle === 0
43
+ const playerCoords: Vec3 =
44
+ vehicle === 0
53
45
  ? entity.coords(playerPed, true)
54
46
  : GetWorldPositionOfEntityBone(playerPed, 0) as Vec3
55
47
 
56
- const distance = Vdist(
57
- coords[0],
58
- coords[1],
59
- coords[2],
60
- playerCoords[0],
61
- playerCoords[1],
62
- playerCoords[2]
48
+ const distance = Math.sqrt(
49
+ (coords[0] - playerCoords[0]) ** 2 +
50
+ (coords[1] - playerCoords[1]) ** 2 +
51
+ (coords[2] - playerCoords[2]) ** 2
63
52
  )
64
53
 
65
54
  if (distance < maxDistance) {
66
55
  maxDistance = distance
67
56
  closestId = playerId
68
- closestPed = playerPed
69
- closestCoords = playerCoords
70
- closestVehicle = vehicle
71
57
  }
72
58
  }
73
59
  }
74
60
 
75
- return {
76
- playerId: closestId,
77
- playerPed: closestPed,
78
- playerCoords: closestCoords,
79
- playerVehicle: closestVehicle
80
- }
61
+ return closestId ?? null
81
62
  }
82
63
 
83
64
  /**
84
65
  * Finds the nearest vehicle around an entity.
85
66
  */
86
- const vehicle = (entityHandle: number, radialSpace: number): number | undefined => {
67
+ const vehicle = (entityHandle: number, radialSpace: number): number | null => {
87
68
  const coords: Vec3 = entity.coords(entityHandle, true)
88
69
  const vehicles = GetGamePool('CVehicle') as number[]
89
70
 
90
- let closest: number | undefined
91
- let closestDistance = radialSpace
71
+ let closest: number | null = null
92
72
 
93
73
  for (const vehicle of vehicles) {
94
74
  const vehCoords: Vec3 = entity.coords(vehicle, true)
@@ -97,16 +77,16 @@ const vehicle = (entityHandle: number, radialSpace: number): number | undefined
97
77
  const z = coords[2] - vehCoords[2]
98
78
  const distance = Math.sqrt(x * x + y * y + z * z)
99
79
 
100
- if (distance < closestDistance) {
101
- closestDistance = distance
80
+ if (distance < radialSpace) {
81
+ radialSpace = distance
102
82
  closest = vehicle
103
83
  }
104
84
  }
105
85
 
106
- return closest
86
+ return closest ?? null
107
87
  }
108
88
 
109
- export const getNearest = {
89
+ export const nearest = {
110
90
  player,
111
91
  vehicle,
112
92
  coords
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lenix",
3
- "version": "1.8.14",
3
+ "version": "1.8.16",
4
4
  "description": "Typed FiveM utilities for client, server, shared, and NUI scripts",
5
5
  "author": "Lenix",
6
6
  "license": "MIT",
package/server/event.ts CHANGED
@@ -13,4 +13,8 @@ export const onEvent = <
13
13
  >(event: Event, cb: (...params: Params) => void) => {
14
14
  addEventListener(event, cb, false)
15
15
  return () => removeEventListener(event, cb)
16
- }
16
+ }
17
+
18
+ export const emitNetEvent = <Event extends string, Params extends unknown[]>(event: Event, source: number, ...params: Params) => emitNet(event, source, params)
19
+
20
+ export const emitEvent = <Event extends string, Params extends unknown[]>(event: Event, ...params: Params) => emit(event, params)
package/shared/timer.ts CHANGED
@@ -1,16 +1,16 @@
1
1
  /**
2
2
  * Starts a client timer using FiveM game time.
3
3
  */
4
- export const useTimer = ({
4
+ export const timer = ({
5
5
  duration,
6
6
  updateInterval,
7
- onTick,
7
+ onInterval,
8
8
  onEnd
9
9
  }: {
10
10
  duration: number,
11
- updateInterval: number,
12
- onTick: (timeLeft: number) => void,
13
- onEnd: () => void
11
+ updateInterval?: number,
12
+ onInterval: (timeLeft: number) => void,
13
+ onEnd?: () => void
14
14
  }): () => void => {
15
15
  const start = GetGameTimer()
16
16
 
@@ -20,11 +20,19 @@ export const useTimer = ({
20
20
 
21
21
  if (elapsed >= duration) {
22
22
  clearInterval(interval)
23
- onEnd()
23
+ try {
24
+ onEnd?.()
25
+ } catch(e) {
26
+ throw e
27
+ }
24
28
  return
25
29
  }
26
30
 
27
- onTick(timeLeft)
31
+ try {
32
+ onInterval(timeLeft)
33
+ } catch(e) {
34
+ throw e
35
+ }
28
36
  }, updateInterval)
29
37
 
30
38
  return () => clearInterval(interval)
package/shared/types.ts CHANGED
@@ -13,7 +13,7 @@ export type Vec3 = [number, number, number]
13
13
  /**
14
14
  * Two-dimensional coordinates.
15
15
  */
16
- export type Vec2 = [number, number, number]
16
+ export type Vec2 = [number, number]
17
17
 
18
18
  /**
19
19
  * Typed event tuple containing an event id and positional parameters.