RapidGameFramework
Reusable Godot managers for data-driven small games
Loading...
Searching...
No Matches
spatial_state.gd
Go to the documentation of this file.
1extends RefCounted
2
3
6
7var bodies := {}
8
9
10
11func create_body(id:String, position:Vector2, options:Dictionary = {}) -> Dictionary:
12 var body = {
13 "id": id,
14 "position": position,
15 "previous_position": position,
16 "velocity": options.get("velocity", Vector2.ZERO),
17 "acceleration": Vector2.ZERO,
18 "force": Vector2.ZERO,
19 "mass": max(0.0001, float(options.get("mass", 1.0))),
20 "inv_mass": 0.0,
21 "static": bool(options.get("static", false)),
22 "restitution": clamp(float(options.get("restitution", 0.1)), 0.0, 1.0),
23 "friction": clamp(float(options.get("friction", 0.2)), 0.0, 1.0),
24 "shape": options.get("shape", {"type": "aabb", "size": Vector2(16, 16)}).duplicate(true),
25 "layer": int(options.get("layer", 1)),
26 "mask": int(options.get("mask", 1)),
27 "metadata": options.get("metadata", {}).duplicate(true)
28 }
29 body["inv_mass"] = 0.0 if bool(body["static"]) else 1.0 / float(body["mass"])
30 bodies[id] = body
31 return body.duplicate(true)
32
33
34
35func get_body(id:String) -> Dictionary:
36 return bodies.get(id, {}).duplicate(true)
37
38
39
40func set_body(body:Dictionary) -> void:
41 var id = str(body.get("id", ""))
42 if id == "":
43 return
44 bodies[id] = body.duplicate(true)
45
46
47
48func remove_body(id:String) -> void:
49 bodies.erase(id)
50
51
52
53func get_body_ids() -> Array:
54 return bodies.keys()
55
56
57
58func get_bodies() -> Array:
59 var results = []
60 for id in bodies.keys():
61 results.append(bodies[id].duplicate(true))
62 return results
63
64
65
66func can_collide(a:Dictionary, b:Dictionary) -> bool:
67 if bool(a.get("static", false)) and bool(b.get("static", false)):
68 return false
69 var a_layer = int(a.get("layer", 1))
70 var a_mask = int(a.get("mask", 1))
71 var b_layer = int(b.get("layer", 1))
72 var b_mask = int(b.get("mask", 1))
73 return (a_mask & b_layer) != 0 and (b_mask & a_layer) != 0
74
75
76
77func get_state() -> Dictionary:
78 return {"bodies": bodies.duplicate(true)}
79
80
81
82func apply_state(state:Dictionary) -> void:
83 bodies = state.get("bodies", {}).duplicate(true)
84
85
86func clear() -> void:
87 bodies.clear()