public static StaticObject SpawnObject(ObjectInfo objectInfo, Vector3 pos, bool fit = false, Transform parent = null) { if (fit) { pos = Iso.MapToIso(pos); if (!CollisionMap.Fit(pos, out pos, objectInfo.sizeX)) { return(null); } pos = Iso.MapToWorld(pos); } var gameObject = new GameObject(objectInfo.description); gameObject.transform.position = pos; var staticObject = gameObject.AddComponent <StaticObject>(); staticObject.objectInfo = objectInfo; staticObject.title = objectInfo.name; gameObject.transform.SetParent(parent, true); return(staticObject); }
protected override void Awake() { base.Awake(); CollisionMap.SetBlocked(Iso.MapToIso(transform.position), CollisionLayers.Item); _animator = GetComponent <SpriteAnimator>(); _renderer = GetComponent <SpriteRenderer>(); }
private static void ApplyTileCollisions(DT1.Tile tile, int x, int y) { var pos = Iso.MapTileToWorld(x, y); var collisionMapOffset = Iso.Snap(Iso.MapToIso(pos)); int flagIndex = 0; DT1.BlockFlags mask = DT1.BlockFlags.Walk | DT1.BlockFlags.PlayerWalk; for (int dy = 2; dy > -3; --dy) { for (int dx = -2; dx < 3; ++dx, ++flagIndex) { Vector2i subCellPos = collisionMapOffset + new Vector2i(dx, dy); bool passable = (tile.flags[flagIndex] & mask) == 0; CollisionLayers blockedLayers = passable ? CollisionLayers.None : CollisionLayers.Walk; if (tile.orientation == 0) { CollisionMap.SetBlocked(subCellPos, blockedLayers); } else if (CollisionMap.Passable(subCellPos, CollisionLayers.Walk) && !passable) { CollisionMap.SetBlocked(subCellPos, blockedLayers); } } } }
void OperateWithTarget() { if (_dead || _dying || _resurrecting || _usingSkill || overrideMode != null) { return; } if (_targetEntity) { var distance = Vector2.Distance(iso.pos, Iso.MapToIso(_targetEntity.transform.position)); if (distance <= size + _targetEntity.operateRange) { _moving = false; if (_targetEntity.isAttackable) { _operatingWithEntity = _targetEntity; overrideMode = "KK"; } else { _targetEntity.Operate(this); } _targetEntity = null; PlayerController.instance.FlushInput(); // Very strange to see it here, remove } else { _moving = true; } } }
void Update() { Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePosition = Iso.MapToIso(mousePos); mouseTile = Iso.Snap(mousePosition); }
void DrawDebugCellGrid() { Color color = new Color(1, 0, 0, 0.3f); Color freeColor = new Color(1, 1, 1, 0.03f); Vector2i pos = Iso.Snap(Iso.MapToIso(Camera.main.transform.position)); int debugWidth = 100; int debugHeight = 100; pos.x -= debugWidth / 2; pos.y -= debugHeight / 2; int index = instance.MapToIndex(pos); for (int y = 0; y < debugHeight; ++y) { for (int x = 0; x < debugWidth; ++x) { if (index + x < 0 || index + x >= instance.map.Length) { continue; } if (!instance.map[index + x].passable) { Iso.DebugDrawTile(pos + new Vector3(x, y), color, 0.9f); } else { Iso.DebugDrawTile(pos + new Vector3(x, y), freeColor, 0.9f); } } index += width; } }
private void Awake() { if (materialProperties == null) { materialProperties = new MaterialPropertyBlock(); } CollisionMap.SetBlocked(Iso.MapToIso(transform.position), CollisionLayers.Item); animator = GetComponent <SpriteAnimator>(); }
private void Awake() { if (materialProperties == null) { materialProperties = new MaterialPropertyBlock(); } CollisionMap.SetPassable(Iso.MapToIso(transform.position), false); animator = GetComponent <SpriteAnimator>(); }
public void Use(Entity entity) { if (_dying || _dead || _resurrecting || _usingSkill || overrideMode != null) { return; } _targetPoint = Iso.MapToIso(entity.transform.position); _targetEntity = entity; _targetCharacter = null; _skillInfo = null; }
protected override void Awake() { base.Awake(); if (materialProperties == null) { materialProperties = new MaterialPropertyBlock(); } CollisionMap.SetBlocked(Iso.MapToIso(transform.position), CollisionLayers.Item); animator = GetComponent <SpriteAnimator>(); renderer = GetComponent <SpriteRenderer>(); }
public void Use(Entity entity) { if (takingDamage || dying || dead || ressurecting || usingSkill) { return; } targetPoint = Iso.MapToIso(entity.transform.position); targetEntity = entity; targetCharacter = null; skillInfo = null; }
void DrawDebugPath() { Vector3 targetPosition; if (MouseSelection.current != null) { targetPosition = Iso.MapToIso(MouseSelection.current.transform.position); } else { targetPosition = IsoInput.mousePosition; } var path = Pathing.BuildPath(iso.pos, targetPosition, size: character.size, self: iso.gameObject); Pathing.DebugDrawPath(iso.pos, path); }
public override void Operate(Character character) { var targetWarp = FindTargetWarp(); if (targetWarp == null) { Debug.LogError("Target warp wasn't found"); return; } ScreenFader.SetToBlack(); ScreenFader.FadeToClear(); var target = Iso.MapToIso(targetWarp.transform.position); character.InstantMove(target); character.GoTo(target + new Vector3(targetWarp.info.exitWalkX, targetWarp.info.exitWalkY)); }
private void DrawDebugCellGrid() { Color occupiedColor = new Color(1, 0, 0, 0.3f); Color passableColor = new Color(1, 1, 1, 0.03f); Vector2i origin = Iso.Snap(Iso.MapToIso(Camera.main.transform.position)); int debugWidth = 100; int debugHeight = 100; origin.x -= debugWidth / 2; origin.y -= debugHeight / 2; for (int y = 0; y < debugHeight; ++y) { for (int x = 0; x < debugWidth; ++x) { var pos = origin + new Vector2i(x, y); bool passable = CollisionMap.Passable(pos, layers); Color color = passable ? passableColor : occupiedColor; Iso.DebugDrawTile(pos, color, 0.9f); } } }
public static Pickup Create(Vector3 position, string flippyFile, string name, string title = null, int dir = 0) { position = Iso.MapToIso(position); if (!CollisionMap.Fit(position, out position, mask: CollisionLayers.Item)) { Debug.LogError("Can't fit pickup"); return(null); } position = Iso.MapToWorld(position); var gameObject = new GameObject(name); gameObject.transform.position = position; var spritesheet = DC6.Load(flippyFile); var animator = gameObject.AddComponent <SpriteAnimator>(); animator.sprites = spritesheet.GetSprites(dir); animator.loop = false; var pickup = gameObject.AddComponent <Pickup>(); pickup.title = title; return(pickup); }
private IEnumerator LoadActCoroutine(int actNumber) { PlayerController.instance.enabled = false; MouseSelection.instance.enabled = false; LoadingScreen.Show(0.5f); // It's not zero due to preceding Unity scene load ScreenFader.SetToBlack(); yield return(null); if (_currentAct != null) { Destroy(_currentAct.root); } _currentAct = CreateAct(actNumber); LoadingScreen.Show(0.75f); yield return(null); if (WorldState.instance.Player == null) { WorldState.instance.Player = new Player(className, _currentAct.entry); PlayerController.instance.SetPlayer(WorldState.instance.Player); } else { WorldState.instance.Player.unit.InstantMove(Iso.MapToIso(Iso.MapTileToWorld(_currentAct.entry))); } LoadingScreen.Show(0.9f); yield return(null); // Workaround to load first DCC while screen is black to avoid visible spikes LoadingScreen.Show(1.0f); yield return(null); PlayerController.instance.enabled = true; MouseSelection.instance.enabled = true; LoadingScreen.Hide(); ScreenFader.FadeToClear(); }
public static Loot Create(Vector3 position, string flippyFile, string name, string title = null, int dir = 0) { position = Iso.MapToIso(position); if (!CollisionMap.Fit(position, out position, mask: CollisionLayers.Item)) { Debug.LogError("Can't fit loot"); return(null); } position = Iso.MapToWorld(position); var gameObject = new GameObject(name); gameObject.transform.position = position; var palette = Palette.GetPalette(PaletteType.Act1); var spritesheet = DC6.Load(flippyFile, palette); var animator = gameObject.AddComponent <SpriteAnimator>(); animator.SetSprites(spritesheet.GetSprites(dir)); animator.loop = false; var loot = gameObject.AddComponent <Loot>(); loot.title = title; return(loot); }
void OperateWithTarget() { if (takingDamage || dead || dying || ressurecting || usingSkill) { return; } if (targetEntity) { var distance = Vector2.Distance(iso.pos, Iso.MapToIso(targetEntity.transform.position)); if (distance <= size + targetEntity.operateRange) { var localEntity = targetEntity; moving = false; targetEntity = null; localEntity.Operate(this); PlayerController.instance.FlushInput(); } else { moving = true; } } }
static Renderer CreateTile(DT1.Tile tile, int x, int y, int orderInLayer = 0, Transform parent = null) { var texture = tile.texture; var pos = Iso.MapTileToWorld(x, y); GameObject gameObject = new GameObject(); gameObject.name = tile.mainIndex + "_" + tile.subIndex + "_" + tile.orientation; gameObject.transform.position = pos; if (parent) { gameObject.transform.SetParent(parent); } var meshRenderer = gameObject.AddComponent <MeshRenderer>(); var meshFilter = gameObject.AddComponent <MeshFilter>(); Mesh mesh = new Mesh(); float x0 = tile.textureX; float y0 = tile.textureY; float w = tile.width / Iso.pixelsPerUnit; float h = (-tile.height) / Iso.pixelsPerUnit; if (tile.orientation == 0 || tile.orientation == 15) { var topLeft = new Vector3(-1f, 0.5f); if (tile.orientation == 15) { topLeft.y += tile.roofHeight / Iso.pixelsPerUnit; } mesh.vertices = new Vector3[] { topLeft, topLeft + new Vector3(0, -h), topLeft + new Vector3(w, -h), topLeft + new Vector3(w, 0) }; mesh.triangles = new int[] { 2, 1, 0, 3, 2, 0 }; mesh.uv = new Vector2[] { new Vector2(x0 / texture.width, -y0 / texture.height), new Vector2(x0 / texture.width, (-y0 + tile.height) / texture.height), new Vector2((x0 + tile.width) / texture.width, (-y0 + tile.height) / texture.height), new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height) }; meshRenderer.sortingLayerID = tile.orientation == 0 ? SortingLayers.Floor : SortingLayers.Roof; meshRenderer.sortingOrder = orderInLayer; gameObject.name += tile.orientation == 0 ? " (floor)" : " (roof)"; } else if (tile.orientation > 15) { int upperPart = Math.Min(96, -tile.height); y0 -= upperPart; var topLeft = new Vector3(-1f, upperPart / Iso.pixelsPerUnit - 0.5f); mesh.vertices = new Vector3[] { topLeft, topLeft + new Vector3(0, -h), topLeft + new Vector3(w, -h), topLeft + new Vector3(w, 0) }; mesh.triangles = new int[] { 2, 1, 0, 3, 2, 0 }; mesh.uv = new Vector2[] { new Vector2(x0 / texture.width, -y0 / texture.height), new Vector2(x0 / texture.width, (-y0 + tile.height) / texture.height), new Vector2((x0 + tile.width) / texture.width, (-y0 + tile.height) / texture.height), new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height) }; meshRenderer.sortingLayerID = SortingLayers.LowerWall; meshRenderer.sortingOrder = orderInLayer; gameObject.name += " (lower wall)"; } else { var topLeft = new Vector3(-1f, h - 0.5f); mesh.vertices = new Vector3[] { topLeft, topLeft + new Vector3(0, -h), topLeft + new Vector3(w, -h), topLeft + new Vector3(w, 0) }; mesh.triangles = new int[] { 2, 1, 0, 3, 2, 0 }; mesh.uv = new Vector2[] { new Vector2(x0 / texture.width, (-y0 - tile.height) / texture.height), new Vector2(x0 / texture.width, -y0 / texture.height), new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height), new Vector2((x0 + tile.width) / texture.width, (-y0 - tile.height) / texture.height) }; if (tile.orientation == 13) // shadows { meshRenderer.sortingLayerID = SortingLayers.Shadow; } meshRenderer.sortingOrder = Iso.SortingOrder(pos) - 4; } meshFilter.mesh = mesh; int flagIndex = 0; var collisionMapOffset = Iso.Snap(Iso.MapToIso(pos)); DT1.BlockFlags mask = DT1.BlockFlags.Walk | DT1.BlockFlags.PlayerWalk; for (int dy = 2; dy > -3; --dy) { for (int dx = -2; dx < 3; ++dx, ++flagIndex) { Vector2i subCellPos = collisionMapOffset + new Vector2i(dx, dy); bool passable = (tile.flags[flagIndex] & mask) == 0; CollisionLayers blockedLayers = passable ? CollisionLayers.None : CollisionLayers.Walk; if (tile.orientation == 0) { CollisionMap.SetBlocked(subCellPos, blockedLayers); } else if (CollisionMap.Passable(subCellPos, CollisionLayers.Walk) && !passable) { CollisionMap.SetBlocked(subCellPos, blockedLayers); } } } meshRenderer.material = tile.material; return(meshRenderer); }
public static Unit SpawnMonster(MonStat monStat, Vector3 pos, Transform parent = null, Unit summoner = null) { pos = Iso.MapToIso(pos); if (!CollisionMap.Fit(pos, out pos, monStat.ext.sizeX)) { return(null); } var monster = new GameObject(monStat.nameStr); monster.transform.SetParent(parent); monster.transform.position = Iso.MapToWorld(pos); CollisionMap.Move(pos, pos, monStat.ext.sizeX, monster); var unit = monster.AddComponent <Unit>(); unit.monStat = monStat; unit.title = monStat.name; unit.basePath = @"data\global\monsters"; unit.token = monStat.code; unit.weaponClass = monStat.ext.baseWeaponClass; unit.run = false; unit.walkSpeed = monStat.speed; unit.runSpeed = monStat.runSpeed; unit.size = monStat.ext.sizeX; unit.killable = monStat.killable; var monLvl = MonLvl.Find(monStat.level[0]); if (monLvl != null && !monStat.noRatio) { unit.health = Random.Range(monLvl.hp[0] * monStat.stats[0].minHP, monLvl.hp[0] * monStat.stats[0].maxHP + 1) / 100; } else { unit.health = Random.Range(monStat.stats[0].minHP, monStat.stats[0].maxHP + 1); } unit.maxHealth = unit.health; var animator = unit.GetComponent <COFAnimator>(); animator.equip = new string[monStat.ext.gearVariants.Length]; for (int i = 0; i < animator.equip.Length; ++i) { var variants = monStat.ext.gearVariants[i]; if (variants == null) { continue; } animator.equip[i] = variants[Random.Range(0, variants.Length)]; } if (summoner != null) { unit.party = summoner.party; var petController = monster.AddComponent <PetController>(); petController.owner = summoner; } else if (monStat.ai == "Npc" || monStat.ai == "Towner" || monStat.ai == "Vendor" || monStat.ai == "Hireable") { unit.party = Party.Good; monster.AddComponent <NpcController>(); } else if (monStat.ai != "Idle" && monStat.ai != "NpcStationary") { unit.party = Party.Evil; monster.AddComponent <MonsterController>(); } var body = monster.AddComponent <Rigidbody2D>(); body.isKinematic = true; var collider = monster.AddComponent <CircleCollider2D>(); collider.radius = monStat.ext.sizeX * Iso.tileSizeY; return(unit); }
private void OnDisable() { CollisionMap.SetBlocked(Iso.MapToIso(transform.position), CollisionLayers.None); }
public static void GoToAct(int actNumber) { Destroy(currentAct.root); currentAct = CreateAct(actNumber); PlayerController.instance.character.InstantMove(Iso.MapToIso(Iso.MapTileToWorld(currentAct.entry))); }
public static Character SpawnMonster(MonStat monStat, Vector3 pos, Transform parent = null) { pos = Iso.MapToIso(pos); if (!CollisionMap.Fit(pos, out pos, monStat.ext.sizeX)) { return(null); } pos = Iso.MapToWorld(pos); var monster = new GameObject(monStat.nameStr); monster.transform.SetParent(parent); monster.transform.position = pos; var character = monster.AddComponent <Character>(); character.monStat = monStat; character.title = monStat.name; character.basePath = @"data\global\monsters"; character.token = monStat.code; character.weaponClass = monStat.ext.baseWeaponClass; character.run = false; character.walkSpeed = monStat.speed; character.runSpeed = monStat.runSpeed; character.size = monStat.ext.sizeX; var monLvl = MonLvl.Find(monStat.level[0]); if (monLvl != null && !monStat.noRatio) { character.health = Random.Range(monLvl.hp[0] * monStat.stats[0].minHP, monLvl.hp[0] * monStat.stats[0].maxHP + 1) / 100; } else { character.health = Random.Range(monStat.stats[0].minHP, monStat.stats[0].maxHP + 1); } character.maxHealth = character.health; var animator = character.GetComponent <COFAnimator>(); animator.equip = new string[monStat.ext.gearVariants.Length]; for (int i = 0; i < animator.equip.Length; ++i) { var variants = monStat.ext.gearVariants[i]; if (variants == null) { continue; } animator.equip[i] = variants[Random.Range(0, variants.Length)]; } if (monStat.ai == "Npc") { monster.AddComponent <NpcController>(); } else if (monStat.ai != "Idle" && monStat.ai != "NpcStationary") { monster.AddComponent <MonsterController>(); } var body = monster.AddComponent <Rigidbody2D>(); body.isKinematic = true; var collider = monster.AddComponent <CircleCollider2D>(); collider.radius = monStat.ext.sizeX * Iso.tileSizeY; return(character); }
static Renderer CreateTile(DT1.Tile tile, int x, int y, int orderInLayer = 0, Transform parent = null) { var texture = tile.texture; var pos = Iso.MapTileToWorld(x, y); GameObject gameObject = new GameObject(); gameObject.name = tile.mainIndex + "_" + tile.subIndex + "_" + tile.orientation; gameObject.transform.position = pos; if (parent) { gameObject.transform.SetParent(parent); } var meshRenderer = gameObject.AddComponent <MeshRenderer>(); var meshFilter = gameObject.AddComponent <MeshFilter>(); Mesh mesh = new Mesh(); float x0 = tile.textureX; float y0 = tile.textureY; float w = tile.width / Iso.pixelsPerUnit; float h = (-tile.height) / Iso.pixelsPerUnit; if (tile.orientation == 0 || tile.orientation == 15) { var topLeft = new Vector3(-1f, 0.5f); if (tile.orientation == 15) { topLeft.y += tile.roofHeight / Iso.pixelsPerUnit; } mesh.vertices = new Vector3[] { topLeft, topLeft + new Vector3(0, -h), topLeft + new Vector3(w, -h), topLeft + new Vector3(w, 0) }; mesh.triangles = new int[] { 2, 1, 0, 3, 2, 0 }; mesh.uv = new Vector2[] { new Vector2(x0 / texture.width, -y0 / texture.height), new Vector2(x0 / texture.width, (-y0 + tile.height) / texture.height), new Vector2((x0 + tile.width) / texture.width, (-y0 + tile.height) / texture.height), new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height) }; meshRenderer.sortingLayerName = tile.orientation == 0 ? "Floor" : "Roof"; meshRenderer.sortingOrder = orderInLayer; } else { var topLeft = new Vector3(-1f, h - 0.5f); mesh.vertices = new Vector3[] { topLeft, topLeft + new Vector3(0, -h), topLeft + new Vector3(w, -h), topLeft + new Vector3(w, 0) }; mesh.triangles = new int[] { 2, 1, 0, 3, 2, 0 }; mesh.uv = new Vector2[] { new Vector2(x0 / texture.width, (-y0 - tile.height) / texture.height), new Vector2(x0 / texture.width, -y0 / texture.height), new Vector2((x0 + tile.width) / texture.width, -y0 / texture.height), new Vector2((x0 + tile.width) / texture.width, (-y0 - tile.height) / texture.height) }; meshRenderer.sortingOrder = Iso.SortingOrder(pos) - 4; } meshFilter.mesh = mesh; int flagIndex = 0; var collisionMapOffset = Iso.Snap(Iso.MapToIso(pos)); byte mask = DT1.BlockFlags.Walk | DT1.BlockFlags.PlayerWalk; for (int dy = 2; dy > -3; --dy) { for (int dx = -2; dx < 3; ++dx, ++flagIndex) { var subCellPos = collisionMapOffset + new Vector2i(dx, dy); bool passable = (tile.flags[flagIndex] & mask) == 0; if (tile.orientation == 0) { CollisionMap.SetPassable(subCellPos, passable); } else if (CollisionMap.Passable(subCellPos) && !passable) { CollisionMap.SetPassable(subCellPos, false); } } } meshRenderer.material = tile.material; return(meshRenderer); }
void ControlCharacter() { if (character == null) { return; } DrawDebugPath(); if (_mouseItem != null) { if (Input.GetMouseButtonDown(0)) { Pickup.Create(character.transform.position, _mouseItem); FlushInput(); mouseItem = null; } return; } usingSkills = false; for (int i = 0; i < hotSkills.Count; ++i) { SkillInfo skill = hotSkills[i]; KeyCode key = hotSkillsBindings[i]; if (skill == null || !Input.GetKey(key)) { continue; } usingSkills = true; if (MouseSelection.current != null) { var targetCharacter = MouseSelection.current.GetComponent <Character>(); if (targetCharacter != null) { character.UseSkill(skill, targetCharacter); } else { character.UseSkill(skill, Iso.MapToIso(MouseSelection.current.transform.position)); } } else { character.UseSkill(skill, IsoInput.mousePosition); } } // move to PlayerController members once Datasheets loading done not in static section SkillInfo leftSkill = SkillInfo.Attack; SkillInfo rightSkill = SkillInfo.Attack; if (!usingSkills) { if (Input.GetMouseButton(1) || (Input.GetKey(KeyCode.LeftShift) && Input.GetMouseButton(0))) { character.UseSkill(rightSkill, IsoInput.mousePosition); } else if (Input.GetMouseButton(0) && !EventSystem.current.IsPointerOverGameObject()) { if (MouseSelection.current != null) { var targetCharacter = MouseSelection.current.GetComponent <Character>(); if (targetCharacter != null) { if (targetCharacter.monStat != null && targetCharacter.monStat.npc) { // todo interact with npc } else { character.UseSkill(leftSkill, targetCharacter); } } else { character.Use(MouseSelection.current); } } else { character.GoTo(IsoInput.mousePosition); } } else { character.LookAt(IsoInput.mousePosition); } } character.run = run; }
private void OnDisable() { CollisionMap.SetPassable(Iso.MapToIso(transform.position), true); }