Skip to content

Instantly share code, notes, and snippets.

@igrir
Last active October 10, 2023 11:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igrir/12cb89bbea12c89600f37227356e8bed to your computer and use it in GitHub Desktop.
Save igrir/12cb89bbea12c89600f37227356e8bed to your computer and use it in GitHub Desktop.
Camera target assigner from Corgi Engine to ProCamera2D
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using CameraEvents;
using Com.LuisPedroFonseca.ProCamera2D;
using MoreMountains.CorgiEngine;
using MoreMountains.Tools;
using UnityEngine;
public class CorgiProCamHandler : MonoBehaviour, MMEventListener<CorgiEngineEvent>, MMEventListener<MMCameraEvent>
{
[SerializeField] ProCamera2D procam;
public bool AssignCameraOnSpawn;
private Transform Target;
private bool FollowsPlayer = false;
private void Awake()
{
}
// Start is called before the first frame update
void Start()
{
// we make sure we have a Player
if ( (LevelManager.Instance.Players == null) || (LevelManager.Instance.Players.Count == 0) )
{
Debug.LogWarning ("CameraController : The LevelManager couldn't find a Player character."+
"Make sure there's one set in the Level Manager. The camera script won't work without that.");
return;
}
AssignTarget();
}
/// <summary>
/// On enable, we start listening to events
/// </summary>
protected virtual void OnEnable()
{
this.MMEventStartListening<CorgiEngineEvent> ();
this.MMEventStartListening<MMCameraEvent>();
}
/// <summary>
/// On disable, we stop listening to events
/// </summary>
protected virtual void OnDisable()
{
this.MMEventStopListening<CorgiEngineEvent> ();
this.MMEventStopListening<MMCameraEvent>();
}
// Update is called once per frame
void Update()
{
}
public void OnMMEvent(CorgiEngineEvent corgiEngineEvent)
{
if (corgiEngineEvent.EventType == CorgiEngineEventTypes.Respawn)
{
// if (InstantRepositionCameraOnRespawn)
// {
// TeleportCameraToTarget ();
// }
}
if (corgiEngineEvent.EventType == CorgiEngineEventTypes.CharacterSwitch)
{
AssignTarget();
}
if (corgiEngineEvent.EventType == CorgiEngineEventTypes.CharacterSwap)
{
AssignTarget();
}
}
protected virtual void AssignTarget()
{
if (!AssignCameraOnSpawn)
{
return;
}
// we make sure it has a CorgiController associated to it
Target = LevelManager.Instance.Players[0].transform;
if (Target.GetComponent<CorgiController>() == null)
{
Debug.LogWarning("CameraController : The Player character doesn't have a CorgiController "+
"associated to it, the Camera won't work.");
return;
}
}
public void OnMMEvent(MMCameraEvent cameraEvent)
{
switch (cameraEvent.EventType)
{
case MMCameraEventTypes.StartFollowing:
StartFollowing();
TeleportCameraToTarget();
break;
case MMCameraEventTypes.StopFollowing:
StopFollowing();
break;
}
}
private void StartFollowing()
{
FollowsPlayer = true;
var player = LevelManager.Instance.Players[0].transform;
var previousFollows = procam.CameraTargets.FirstOrDefault(x => x.TargetTransform == player);
if (previousFollows == null)
{
procam.AddCameraTarget(LevelManager.Instance.Players[0].transform);
}
}
void StopFollowing()
{
procam.CameraTargets.Clear();
}
public void TeleportCameraToTarget()
{
if (Target != null)
{
// this.transform.position = Target.position;
var position = Target.position;
procam.MoveCameraInstantlyToPosition(new Vector2(position.x, position.y));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment