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(); } }
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(); } }
//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)); } } } }
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); } }
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() { //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(); }
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 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(); } }
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); } } }
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() { 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); } } } }
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(); } }
void Update() { if (building_mode && building_character != null) { PlayerControls constrols = PlayerControls.Get(building_character.player_id); PlayerControlsMouse mouse = PlayerControlsMouse.Get(); if (!position_set) { if (constrols.IsGamePad()) { //Controller Game Pad building transform.position = building_character.transform.position + building_character.transform.forward * build_distance; transform.rotation = Quaternion.Euler(0f, manual_rotate, 0f) * Quaternion.LookRotation(building_character.GetFacing(), Vector3.up); } else { //Mouse/Touch controls transform.position = mouse.GetPointingPos(); transform.rotation = Quaternion.Euler(0f, manual_rotate, 0f) * TheCamera.Get().GetFacingRotation(); } //Snap to grid FindAutoPosition(); //Show/Hide on mobile if (TheGame.IsMobile()) { SetBuildVisible(mouse.IsMouseHold()); } } bool can_build = CheckIfCanBuild(); Color color = can_build ? Color.white : Color.red * 0.9f; SetModelColor(new Color(color.r, color.g, color.b, 0.5f), !can_build); } update_timer += Time.deltaTime; if (update_timer > 0.5f) { update_timer = Random.Range(-0.02f, 0.02f); SlowUpdate(); //Optimization } }
public bool IsNearCamera(float distance) { float dist = (transform.position - TheCamera.Get().GetTargetPos()).magnitude; return(dist < distance); }
protected override void Update() { base.Update(); PlayerCharacter character = GetPlayer(); int gold = (character != null) ? character.Data.gold : 0; if (gold_value != null) { gold_value.text = gold.ToString(); } //Init inventories from here because they are disabled foreach (ItemSlotPanel panel in item_slot_panels) { panel.InitPanel(); } //Fx visibility damage_fx_timer += Time.deltaTime; if (build_mode_text != null) { build_mode_text.enabled = IsBuildMode(); } if (tps_cursor != null) { tps_cursor.enabled = TheCamera.Get().IsLocked(); } if (character != null && !character.IsDead() && character.Attributes.IsDepletingHP()) { DoDamageFXInterval(); } //Cold FX if (character != null && !character.IsDead()) { PlayerCharacterHeat characterHeat = PlayerCharacterHeat.Get(character.player_id); if (cold_fx != null && characterHeat != null) { cold_fx.SetVisible(characterHeat.IsCold()); } if (damage_fx != null && characterHeat != null && characterHeat.IsColdDamage()) { DoDamageFXInterval(); } } //Controls PlayerControls controls = PlayerControls.Get(player_id); if (controls.IsPressCraft()) { CraftPanel.Get(player_id)?.Toggle(); ActionSelectorUI.Get(player_id)?.Hide(); ActionSelector.Get(player_id)?.Hide(); } //Backpack panel BagPanel bag_panel = BagPanel.Get(player_id); if (character != null && bag_panel != null) { InventoryItemData item = character.Inventory.GetBestEquippedBag(); ItemData idata = ItemData.Get(item?.item_id); if (idata != null) { bag_panel.ShowBag(character, item.uid, idata.bag_size); } else { bag_panel.HideBag(); } } }
void FixedUpdate()//프레임과 상관없이 일정 시간의 지나면 실행됨 update보다 자주 실행됨, 물리적 구현 사용 { if (TheGame.Get().IsPaused()) { rigid.velocity = Vector3.zero; return; } if (is_dead) { return; } //죽지 않으면 실행되는 구현 PlayerControls controls = PlayerControls.Get(); PlayerControlsMouse mcontrols = PlayerControlsMouse.Get(); Vector3 tmove = Vector3.zero; //Navmesh 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++; } } //Moving auto_move_timer += Time.fixedDeltaTime; if (auto_move && auto_move_timer > 0.02f) { if (!use_navmesh || !calculating_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(move_speed * move_speed_mult, move_dir.magnitude * 10f); tmove = move_dir.normalized * move_dist; } } else { 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 * move_speed * move_speed_mult; } if (is_action) { tmove = Vector3.zero; } DetectGrounded(); //Falling if (!is_grounded) { tmove += Vector3.down * fall_speed; } //Do move move = Vector3.Lerp(move, tmove, move_accel * Time.fixedDeltaTime); rigid.velocity = move; //Facing if (!is_action && IsMoving()) { facing = new Vector3(move.x, 0f, move.z).normalized; } Quaternion targ_rot = Quaternion.LookRotation(facing, Vector3.up); rigid.MoveRotation(Quaternion.RotateTowards(rigid.rotation, targ_rot, rotate_speed * Time.fixedDeltaTime)); //Fronted DetectFronted(); //Traveled calcul 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) { StopMove(); } if (controls.IsMoving() || mcontrols.IsJoystickActive()) { StopAction(); } }