// Start is called before the first frame update void OnEnable() { if (I == null) { I = this; } }
void Awake() { _instance = this; cam = GetComponent <Camera>(); rotated_offset = follow_offset; current_offset = follow_offset; }
void Update() { transform.position = PlayerControlsMouse.Get().GetPointingPos(); transform.rotation = Quaternion.LookRotation(TheCamera.Get().transform.forward, Vector3.up); PlayerCharacter player = PlayerCharacter.GetFirst(); PlayerControls controls = PlayerControls.Get(player ? player.player_id : 0); MAction maction = current_slot != null && current_slot.GetItem() != null?current_slot.GetItem().FindMergeAction(current_select) : null; title.enabled = maction != null && player != null && maction.CanDoAction(player, current_slot, current_select); title.text = maction != null ? maction.title : ""; bool active = current_slot != null && controls != null && !controls.IsGamePad(); if (active != icon_group.activeSelf) { icon_group.SetActive(active); } icon.enabled = false; if (current_slot != null && current_slot.GetItem()) { icon.sprite = current_slot.GetItem().icon; icon.enabled = true; } timer += Time.deltaTime; if (timer > refresh_rate) { timer = 0f; SlowUpdate(); } }
//Strike without target private void DoRangedAttackStrike() { //Ranged attack ItemData equipped = character.EquipData.GetEquippedWeaponData(); if (equipped != null && equipped.IsRangedWeapon()) { InventoryItemData projectile_inv = character.Inventory.GetFirstItemInGroup(equipped.projectile_group); ItemData projectile = ItemData.Get(projectile_inv?.item_id); if (projectile != null) { character.Inventory.UseItem(projectile, 1); Vector3 pos = GetProjectileSpawnPos(); Vector3 dir = transform.forward; bool freerotate = TheCamera.Get().IsFreeRotation(); if (freerotate) { dir = TheCamera.Get().GetFacingDir(); } GameObject proj = Instantiate(projectile.projectile_prefab, pos, Quaternion.LookRotation(dir.normalized, Vector3.up)); Projectile project = proj.GetComponent <Projectile>(); project.shooter = character; project.dir = dir.normalized; project.damage = equipped.damage; if (freerotate) { project.SetInitialCurve(TheCamera.Get().GetAimDir(character)); } } } }
protected override void Update() { base.Update(); if (IsVisible() && character != null && select != null) { float dist = (interact_pos - character.transform.position).magnitude; if (dist > select.use_range * 1.2f) { Hide(); } } if (IsVisible()) { Vector3 dir = TheCamera.Get().GetFacingFront(); transform.rotation = Quaternion.LookRotation(dir, Vector3.up); } if (IsVisible() && select == null) { Hide(); } //Auto focus UISlotPanel focus_panel = UISlotPanel.GetFocusedPanel(); if (focus_panel != this && IsVisible() && PlayerControls.IsAnyGamePad()) { Focus(); } }
private void UpdateSlow() { Vector3 pos = TheCamera.Get().GetTargetPos(); Vector3 cam_dir = TheCamera.Get().GetFacingFront(); Vector3 obj_dir = transform.position - pos; bool is_behind = Vector3.Dot(obj_dir.normalized, cam_dir) < 0f; bool is_near = (transform.position - pos).magnitude < distance; SetMaterial(is_behind && is_near); }
void SlowUpdate() { //Optimization Vector3 center_pos = TheCamera.Get().GetTargetPosOffsetFace(); foreach (Selectable select in Selectable.GetAll()) { float dist = (select.GetPosition() - center_pos).magnitude; select.SetActive(dist < select.active_range); } }
void FixedUpdate() { if (TheGame.Get().IsPaused()) { return; } //Optimization, dont run if too far float dist = (TheCamera.Get().GetTargetPos() - transform.position).magnitude; float active_range = Mathf.Max(detect_range * 2f, selectable.active_range * 0.8f); is_active = (state != AnimalState.Wander && state != AnimalState.Dead) || character.IsMoving() || dist < active_range; }
//Ranged attack without a target private IEnumerator AttackRunNoTarget() { character.SetDoingAction(true); is_attacking = true; //Rotate toward bool freerotate = TheCamera.Get().IsFreeRotation(); if (freerotate) { character.FaceTorward(transform.position + TheCamera.Get().GetFacingFront()); } //Start animation if (onAttack != null) { onAttack.Invoke(null, true); } //Wait for windup float windup = GetAttackWindup(); yield return(new WaitForSeconds(windup)); //Durability character.Inventory.UpdateAllEquippedItemsDurability(true, -1f); int nb_strikes = GetAttackStrikes(); float strike_interval = GetAttackStikesInterval(); while (nb_strikes > 0) { DoRangedAttackStrike(); yield return(new WaitForSeconds(strike_interval)); nb_strikes--; } //Reset timer attack_timer = 0f; //Wait for the end of the attack before character can move again float windout = GetAttackWindout(); yield return(new WaitForSeconds(windout)); character.SetDoingAction(false); is_attacking = false; }
private void DoMoveToward(Vector3 target_pos) { Vector3 dir = target_pos - transform.position; Vector3 tDir = target_pos - start_pos; float mdist = Mathf.Min(fx_speed * Time.deltaTime, dir.magnitude); float scale = dir.magnitude / tDir.magnitude; transform.position += dir.normalized * mdist; transform.localScale = start_scale * scale; transform.rotation = Quaternion.LookRotation(TheCamera.Get().transform.forward, Vector3.up); if (dir.magnitude < 0.1f) { Destroy(gameObject); } }
protected virtual void OnMouseBtnHold() { LastClickPosition = Input.mousePosition; Vector2 clickPosition = Input.mousePosition; // Длинный жест пальцем - перетаскивание if ((clickPosition - InitialClick).magnitude > DragTreshold) { if (DragScreenEnabled) { TheCamera.TranslateScreen(LastClickPosition, clickPosition); } IsInteractionStatic = false; } }
void Update() { Vector3 dir = TheCamera.Get().GetFacingFront(); transform.rotation = Quaternion.LookRotation(dir, Vector3.up); if (target == null || target.IsDead()) { Destroy(gameObject); } else { fill.fillAmount = target.hp / (float)target.GetMaxHP(); canvas_group.alpha = fill.fillAmount < 0.999f ? 1f : 0f; } }
private void Start() { canvas.worldCamera = TheCamera.GetCamera(); if (!TheGame.IsMobile() && ItemSelectedFX.Get() == null && AssetData.Get().item_select_fx != null) { Instantiate(AssetData.Get().item_select_fx, transform.position, Quaternion.identity); } PlayerUI gameplay_ui = GetComponentInChildren <PlayerUI>(); if (gameplay_ui == null) { Debug.LogError("Warning: Missing PlayerUI script on the Gameplay tab in the UI prefab"); } }
public static TheCamera GetInstance() { if (!instance) { parentObject = GameObject.Find("TheCamera"); if (parentObject != null) { instance = parentObject.GetComponent <TheCamera>(); } else { Debug.Log("WTF : no instance found for TheCamera"); } } return(instance); }
private void Start() { //Load game data PlayerData pdata = PlayerData.Get(); GameData gdata = GameData.Get(); if (!string.IsNullOrEmpty(pdata.current_scene)) { if (pdata.current_entry_index < 0 && pdata.current_scene == SceneNav.GetCurrentScene()) { PlayerCharacter.Get().transform.position = pdata.current_pos; TheCamera.Get().MoveToTarget(pdata.current_pos); } } pdata.current_scene = SceneNav.GetCurrentScene(); //Spawn dropped items foreach (KeyValuePair <string, DroppedItemData> elem in pdata.dropped_items) { if (elem.Value.scene == SceneNav.GetCurrentScene()) { Item.Spawn(elem.Key); } } //Spawn constructions foreach (KeyValuePair <string, BuiltConstructionData> elem in pdata.built_constructions) { if (elem.Value.scene == SceneNav.GetCurrentScene()) { Construction.Spawn(elem.Key); } } //Spawn plants foreach (KeyValuePair <string, SowedPlantData> elem in pdata.sowed_plants) { if (elem.Value.scene == SceneNav.GetCurrentScene()) { Plant.Spawn(elem.Key); } } BlackPanel.Get().Show(true); BlackPanel.Get().Hide(); }
protected virtual void OnTwinTouch() { if (!ZoomStarted) { ZoomStarted = true; LastZoomCenter = TheCamera.Camera.ScreenToWorldPoint((Input.GetTouch(0).position + Input.GetTouch(1).position) / 2f); } if (LastTouchPosition.Length > 1) { float deltaScale = (LastTouchPosition [0] - LastTouchPosition [1]).magnitude - (Input.GetTouch(0).position - Input.GetTouch(1).position).magnitude; TheCamera.Zoom( LastZoomCenter, ZoomSpeed * deltaScale / TheCamera.Camera.ScreenToWorldPoint(Vector3.one).magnitude ); IsInteractionStatic = false; } UpdateTouchPositions(); }
void Update() { if (TheGame.Get().IsPaused()) { return; } if (IsDead()) { return; } if (rider != null) { PlayerControls controls = PlayerControls.Get(rider.player_id); PlayerControlsMouse mcontrols = PlayerControlsMouse.Get(); Vector3 tmove = Vector3.zero; Vector3 cam_move = TheCamera.Get().GetRotation() * controls.GetMove(); if (mcontrols.IsJoystickActive()) { Vector2 joystick = mcontrols.GetJoystickDir(); cam_move = TheCamera.Get().GetRotation() * new Vector3(joystick.x, 0f, joystick.y); } tmove = cam_move * ride_speed; if (tmove.magnitude > 0.1f) { character.DirectMoveToward(tmove); } //Character stuck if (tmove.magnitude < 0.1f && character.IsStuck()) { character.Stop(); } } //Animations if (animator.enabled) { animator.SetBool("Move", IsMoving()); animator.SetBool("Run", IsMoving()); } }
public void TakeDamage(int damage) { if (is_dead) { return; } int dam = damage - GetArmor(); dam = Mathf.Max(dam, 1); PlayerData.Get().AddAttributeValue(AttributeType.Health, -dam, GetAttributeMax(AttributeType.Health)); TheCamera.Get().Shake(); TheAudio.Get().PlaySFX("character", hit_sound); if (onDamaged != null) { onDamaged.Invoke(); } }
public Mat Frame() { if (!Connected) { throw new ArgumentException("Trying to read frame from invalid camera " + Path); } var res = new Mat(); try { TheCamera.Read(res); } catch { Error("An error occured while reading the frame"); throw; } return(res); }
public void TakeDamage(int damage) { if (is_dead) { return; } if (character.Attributes.GetBonusEffectTotal(BonusType.Invulnerable) > 0.5f) { return; } int dam = damage - GetArmor(); dam = Mathf.Max(dam, 1); int invuln = Mathf.RoundToInt(dam * character.Attributes.GetBonusEffectTotal(BonusType.Invulnerable)); dam = dam - invuln; if (dam <= 0) { return; } character_attr.AddAttribute(AttributeType.Health, -dam); //Durability character.Inventory.UpdateAllEquippedItemsDurability(false, -1f); character.StopSleep(); TheCamera.Get().Shake(); TheAudio.Get().PlaySFX("player", hit_sound); if (onDamaged != null) { onDamaged.Invoke(); } }
protected override void OnMouseBtnDown() { base.OnMouseBtnDown(); RaycastHit2D hit = TheCamera.Raycast2DScreen(Input.mousePosition); if (hit && SelectedBas == null) { Planet bas = hit.transform.gameObject.GetComponent <Planet>(); MothershipOrbit mothershipOrbit = hit.transform.GetComponent <MothershipOrbit>(); if (bas != null && bas.Owner.PlayerNumber == 0) { SelectedBas = bas; bas.MakeArrow(); } else if (mothershipOrbit != null && mothershipOrbit.Mothership.Owner.PlayerNumber == 0) { SelectedMothershipOrbit = mothershipOrbit; mothershipOrbit.MakeArrow(); } } }
void Update() { if (visible && character != null && select != null) { float dist = (transform.position - character.transform.position).magnitude; if (dist > select.use_range) { Hide(); } } if (visible) { Vector3 dir = TheCamera.Get().GetFacingFront(); transform.rotation = Quaternion.LookRotation(dir, Vector3.up); } if (visible && select == null) { Hide(); } }
void Update() { Vector3 dir = TheCamera.Get().GetFacingFront(); transform.rotation = Quaternion.LookRotation(dir, Vector3.up); if (manual) { fill.fillAmount = manual_value; } else { timer += Time.deltaTime; float value = timer / duration; fill.fillAmount = value; if (value > 1f) { Destroy(gameObject); } } }
public void RaycastFloorPos() { Ray ray = TheCamera.GetCamera().ScreenPointToRay(Input.mousePosition); RaycastHit hit; bool success = Physics.Raycast(ray, out hit, 100f, floor_layer.value); if (success) { floor_pos = ray.GetPoint(hit.distance); } else { Plane plane = new Plane(Vector3.up, 0f); float dist; bool phit = plane.Raycast(ray, out dist); if (phit) { floor_pos = ray.GetPoint(dist); } } //Debug.DrawLine(TheCamera.GetCamera().transform.position, floor_pos); }
void Update() { if (target == null) { Destroy(gameObject); return; } if (icon_group.activeSelf) { icon_group.SetActive(false); } if (!target.IsActive()) { return; } transform.position = target.transform.position; transform.rotation = Quaternion.LookRotation(TheCamera.Get().transform.forward, Vector3.up); ItemSlot selected = ItemSlotPanel.GetSelectedSlotInAllPanels(); if (selected != null && selected.GetItem() != null) { MAction action = selected.GetItem().FindMergeAction(target); foreach (PlayerCharacter player in PlayerCharacter.GetAll()) { if (player != null && action != null && action.CanDoAction(player, selected, target)) { icon.sprite = selected.GetItem().icon; title.text = action.title; icon_group.SetActive(true); } } } }
protected override void OnMouseBtnUp() { base.OnMouseBtnUp(); if (SelectedBas != null) { DestroyObject(SelectedBas.LineRendererArrow); foreach (var b in GameManager.Instance.Planets) { b.GlowRemove(); } RaycastHit2D hit = TheCamera.Raycast2DScreen(Input.mousePosition); if (hit) { Planet bas = hit.transform.gameObject.GetComponent <Planet>(); if (bas != null && bas != SelectedBas) { SelectedBas.Spawner.ReleaseUnits(bas.gameObject); } } SelectedBas = null; } if (SelectedMothershipOrbit != null) { DestroyObject(SelectedMothershipOrbit.LineRendererArrow); RaycastHit2D hit = TheCamera.Raycast2DScreen(Input.mousePosition); if (hit) { Planet bas = hit.transform.GetComponent <Planet>(); if (bas != null && bas != SelectedMothershipOrbit.Planet) // && bas.owner.playerNumber == 0) { SelectedMothershipOrbit.AssignToBase(bas); } } SelectedMothershipOrbit = null; } }
void Update() { Vector3 wPos = InventoryBar.Get().GetSlotWorldPosition(slot_target); Vector3 dir = wPos - transform.position; Vector3 tDir = wPos - start_pos; float mdist = Mathf.Min(fx_speed * Time.deltaTime, dir.magnitude); float scale = dir.magnitude / tDir.magnitude; transform.position += dir.normalized * mdist; transform.localScale = start_scale * scale; transform.rotation = Quaternion.LookRotation(TheCamera.Get().transform.forward, Vector3.up); if (dir.magnitude < 0.1f) { Destroy(gameObject); } timer += Time.deltaTime; if (timer > 2f) { Destroy(gameObject); } }
void Update() { transform.position = PlayerControlsMouse.Get().GetPointingPos(); transform.rotation = Quaternion.LookRotation(TheCamera.Get().transform.forward, Vector3.up); ItemSlot islot = InventoryBar.Get().GetSelectedSlot(); ItemSlot eslot = EquipBar.Get().GetSelectedSlot(); ItemSlot slot = eslot != null ? eslot : islot; Selectable select = Selectable.GetNearestHover(transform.position); MAction maction = slot != null && slot.GetItem() != null?slot.GetItem().FindMergeAction(select) : null; title.enabled = maction != null; title.text = maction != null ? maction.title : ""; if ((slot != null) != icon_group.activeSelf) { icon_group.SetActive(slot != null); } if (slot != null && slot.GetItem()) { icon.sprite = slot.GetItem().icon; } }
void FixedUpdate() { if (TheGame.Get().IsPaused()) { rigid.velocity = Vector3.zero; return; } if (IsDead()) { return; } PlayerControls controls = PlayerControls.Get(player_id); PlayerControlsMouse mcontrols = PlayerControlsMouse.Get(); Vector3 tmove = Vector3.zero; //Update auto move for moving targets GameObject auto_move_obj = null; if (auto_move_select != null && auto_move_select.type == SelectableType.Interact) { auto_move_obj = auto_move_select.gameObject; } if (auto_move_attack != null) { auto_move_obj = auto_move_attack.gameObject; } if (auto_move && auto_move_obj != null) { Vector3 diff = auto_move_obj.transform.position - auto_move_target; if (diff.magnitude > 1f) { auto_move_target = auto_move_obj.transform.position; auto_move_target_next = auto_move_obj.transform.position; CalculateNavmesh(); //Recalculate navmesh because target moved } } //Navmesh calculate next path if (auto_move && use_navmesh && path_found && path_index < nav_paths.Length) { auto_move_target_next = nav_paths[path_index]; Vector3 move_dir_total = auto_move_target_next - transform.position; move_dir_total.y = 0f; if (move_dir_total.magnitude < 0.2f) { path_index++; } } //AUTO Moving (after mouse click) auto_move_timer += Time.fixedDeltaTime; if (auto_move && auto_move_timer > 0.02f) //auto_move_timer to let the navmesh time to calculate a path { Vector3 move_dir_total = auto_move_target - transform.position; Vector3 move_dir_next = auto_move_target_next - transform.position; Vector3 move_dir = move_dir_next.normalized * Mathf.Min(move_dir_total.magnitude, 1f); move_dir.y = 0f; float move_dist = Mathf.Min(GetMoveSpeed(), move_dir.magnitude * 10f); tmove = move_dir.normalized * move_dist; } //Keyboard/gamepad moving else if (IsControlsEnabled()) { Vector3 cam_move = TheCamera.Get().GetRotation() * controls.GetMove(); if (mcontrols.IsJoystickActive() && !character_craft.IsBuildMode()) { Vector2 joystick = mcontrols.GetJoystickDir(); cam_move = TheCamera.Get().GetRotation() * new Vector3(joystick.x, 0f, joystick.y); } tmove = cam_move * GetMoveSpeed(); } //CancelAction if (is_action && can_cancel_action && tmove.magnitude > 0.1f) { CancelAction(); } //Stop moving if doing action if (is_action) { tmove = Vector3.zero; } //Check if grounded DetectGrounded(); //Add Falling to the move vector if (!is_grounded || IsJumping()) { if (!IsJumping()) { fall_vect = Vector3.MoveTowards(fall_vect, Vector3.down * fall_speed, fall_gravity * Time.fixedDeltaTime); } tmove += fall_vect; } //Add slope angle else if (is_grounded) { tmove = Vector3.ProjectOnPlane(tmove.normalized, ground_normal).normalized *tmove.magnitude; } //Apply the move calculated previously move = Vector3.Lerp(move, tmove, move_accel * Time.fixedDeltaTime); rigid.velocity = move; //Calculate Facing if (!is_action && IsMoving()) { facing = new Vector3(move.x, 0f, move.z).normalized; } //Rotate character with right joystick when not in free rotate mode bool freerotate = TheCamera.Get().IsFreeRotation(); if (!is_action && !freerotate && controls.IsGamePad()) { Vector2 look = controls.GetFreelook(); Vector3 look3 = TheCamera.Get().GetRotation() * new Vector3(look.x, 0f, look.y); if (look3.magnitude > 0.5f) { facing = look3.normalized; } } //Apply the facing Quaternion targ_rot = Quaternion.LookRotation(facing, Vector3.up); rigid.MoveRotation(Quaternion.RotateTowards(rigid.rotation, targ_rot, rotate_speed * Time.fixedDeltaTime)); //Fronted (need to be done after facing to avoid issues) DetectFronted(); //Check the average traveled movement (allow to check if character is stuck) Vector3 last_frame_travel = transform.position - prev_pos; move_average = Vector3.MoveTowards(move_average, last_frame_travel, 1f * Time.fixedDeltaTime); prev_pos = transform.position; //Stop auto move bool stuck_somewhere = move_average.magnitude <0.02f && auto_move_timer> 1f; if (stuck_somewhere) { auto_move = false; } //Stop the click auto move when moving with keyboard/joystick/gamepad if (controls.IsMoving() || mcontrols.IsJoystickActive() || mcontrols.IsDoubleTouch()) { StopAutoMove(); } }
public void Dispose() { TheCamera.Release(); }