Created
March 3, 2025 11:54
-
-
Save RednibCoding/59ca38677b57e7b0bc5404bb28c922e9 to your computer and use it in GitHub Desktop.
Cave CamSwitcher script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cave # type: ignore | |
# Private module-level variables (not accessible outside this script) | |
_cam_switcher_data = { | |
"targetOneName": "", | |
"targetTwoName": "" | |
} | |
# Add this to the first entity that the camera can switch to (it must have a CameraComponent) | |
class CamSwitcherTargetOne(cave.Component): | |
def start(self, scene: cave.Scene): | |
self.globals = cave.getGlobalDict() | |
self.globals["cam-target"] = "" | |
self.globals["cam-targets"] = set() | |
_cam_switcher_data["targetOneName"] = self.entity.name | |
self.camComp = None | |
self.cam = self.entity.getChild("Camera") | |
if not self.cam: | |
print(f"Error in CamSwitcher: entity '{self.entity.name}' does not have a camera.") | |
return | |
self.camComp = self.cam.get("Camera") | |
if not self.camComp: | |
print(f"Error in CamSwitcher: entity {self.entity.name} does not have a camera component.") | |
return | |
self.camComp.lerpPosition = 0.3 | |
def firstUpdate(self): | |
if self.camComp: | |
self.globals["cam-target"] = self.entity.name | |
self.globals["cam-targets"].add(self.entity.name) | |
else: | |
print(f"Error in CamSwitcher: cannot add entity '{self.entity.name}' to camera target list because it does not have a camera and camera component.") | |
def update(self): | |
events = cave.getEvents() | |
if events.pressed(cave.event.KEY_TAB): | |
if self.globals["cam-target"] == self.entity.name and _cam_switcher_data["targetTwoName"] in self.globals["cam-targets"]: | |
self.globals["cam-target"] = _cam_switcher_data["targetTwoName"] | |
elif self.globals["cam-target"] == _cam_switcher_data["targetTwoName"] and self.entity.name in self.globals["cam-targets"]: | |
self.globals["cam-target"] = self.entity.name | |
else: | |
self.globals["cam-target"] = "" | |
print("Warning in CamSwitcher: no cam target to switch to.") | |
if not self.camComp: | |
print(f"Error in CamSwitcher: entity '{self.entity.name}' does not have a camera component.") | |
else: | |
self.camComp.useCamera = (self.globals["cam-target"] == self.entity.name) | |
def end(self, scene: cave.Scene): | |
self.globals["cam-targets"].remove(self.entity.name) | |
if self.globals["cam-target"] == self.entity.name: | |
if _cam_switcher_data["targetTwoName"] in self.globals["cam-targets"]: | |
self.globals["cam-target"] = _cam_switcher_data["targetTwoName"] | |
else: | |
print("Warning in CamSwitcher: no cam target to switch to.") | |
# Add this to the second entity that the camera can switch to (it must have a CameraComponent) | |
class CamSwitcherTargetTwo(cave.Component): | |
def start(self, scene: cave.Scene): | |
self.globals = cave.getGlobalDict() | |
_cam_switcher_data["targetTwoName"] = self.entity.name | |
self.camComp = None | |
self.cam = self.entity.getChild("Camera") | |
if not self.cam: | |
print(f"Error in CamSwitcher: entity '{self.entity.name}' does not have a camera.") | |
return | |
self.camComp = self.cam.get("Camera") | |
if not self.camComp: | |
print(f"Error in CamSwitcher: entity '{self.entity.name}' does not have a camera component.") | |
return | |
self.camComp.lerpPosition = 0.3 | |
def firstUpdate(self): | |
if self.camComp: | |
self.globals["cam-targets"].add(self.entity.name) | |
else: | |
print(f"Error in CamSwitcher: cannot add entity '{self.entity.name}' to camera target list because it does not have a camera component.") | |
def update(self): | |
if not self.camComp: | |
print(f"Error in CamSwitcher: {self.entity.name} does not have a camera component") | |
else: | |
self.camComp.useCamera = (self.globals["cam-target"] == self.entity.name) | |
def end(self, scene: cave.Scene): | |
self.globals["cam-targets"].remove(self.entity.name) | |
if self.globals["cam-target"] == self.entity.name: | |
if _cam_switcher_data["targetOneName"] in self.globals["cam-targets"]: | |
self.globals["cam-target"] = _cam_switcher_data["targetOneName"] | |
else: | |
print("Warning in CamSwitcher: no cam target to switch to.") | |
# In your player controller (where movement and other stuff happens) | |
# you can check in the update method if the camera is attached to the entity by doing: | |
# | |
# def update(self): | |
# events = cave.getEvents() | |
# if self.globals["cam-target"] == self.entity.name: | |
# ... do logic that should only happen when the camera is attached to this player entity ... | |
# dont forget to create a self.globals variable in that script in the start method: | |
# | |
# def start(self, scene:cave.Scene): | |
# self.globals = cave.getGlobalDict() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment