public void OnUpdate() { if (Renderer == null) { return; } bool bAlive = LogicUnit.IsAlive && !LogicUnit.IsDying; if (LogicUnit.GetVisibility() != 2) { oldVisibility = false; Renderer.enabled = false; ShadowRenderer.enabled = false; if (HpRenderer != null) { HpRenderer.enabled = false; } if (PlayerNickObject != null) { PlayerNickObject.SetActive(false); } return; } else if (!oldVisibility) { Renderer.enabled = true; ShadowRenderer.enabled = bAlive; if (HpRenderer != null) { HpRenderer.enabled = LogicUnit.IsAlive; } if (PlayerNickObject != null) { PlayerNickObject.SetActive(LogicUnit.IsAlive); } oldVisibility = true; return; } bool hovered = (MapView.Instance.HoveredObject == LogicUnit); if (Renderer != null) { Renderer.material.SetFloat("_Lightness", hovered ? 0.75f : 0.5f); } bool selected = (MapView.Instance.SelectedObject == LogicUnit); if (HpMat1 != null) { HpMat1.color = new Color(1, 1, 1, selected ? 1f : 0.5f); } if (HpMat2 != null) { HpMat2.color = new Color(1, 1, 1, selected ? 1f : 0.5f); } if (LogicUnit.DoUpdateView) { Renderer.enabled = true; ShadowRenderer.enabled = bAlive; if (HpRenderer != null) { HpRenderer.enabled = LogicUnit.IsAlive; } if (PlayerNickObject != null) { PlayerNickObject.SetActive(LogicUnit.IsAlive); } Images.AllodsSpriteSeparate sprites = LogicUnit.Class.File.File; LogicUnit.Class.File.UpdateSprite(); sprites = LogicUnit.Class.File.File; if (!spriteSet) { Renderer.material = new Material(MainCamera.MainShaderPaletted); ShadowRenderer.material = Renderer.material; ShadowRenderer.material.color = new Color(0, 0, 0, 0.5f); spriteSet = true; } int actualFrame = LogicUnit.Class.Index; // draw frame 0 of each unit UnitVisualState actualVState = LogicUnit.VState; if (!bAlive && actualVState == UnitVisualState.Idle) { actualVState = UnitVisualState.Dying; UnitClass dCls = LogicUnit.Class; while (dCls.Dying != null && dCls.Dying != dCls) { dCls = dCls.Dying; } LogicUnit.DeathFrame = dCls.DyingPhases - 1; } Renderer.material.SetTexture("_Palette", GetPalette()); // first (idle) state is 0..8 frames. frames 1 to 7 are flipped. frames 0 and 8 aren't. // 135 180 225 // 90 270 // 45 0 315 bool doFlip = false; int actualAngle16 = 0; int actualAngle8 = 0; int countFull16 = (!LogicUnit.Class.Flip) ? 16 : 9; int countFull8 = (!LogicUnit.Class.Flip) ? 8 : 5; if (LogicUnit.Class.Flip) { if (LogicUnit.Angle < 180) { actualAngle16 = LogicUnit.Angle * 8 / 180; actualAngle8 = LogicUnit.Angle * 4 / 180; } else { actualAngle16 = (180 - (LogicUnit.Angle - 180)) * 8 / 180; actualAngle8 = (180 - (LogicUnit.Angle - 180)) * 4 / 180; doFlip = true; } } else { actualAngle16 = LogicUnit.Angle * 16 / 360; actualAngle8 = LogicUnit.Angle * 8 / 360; } if (actualVState == UnitVisualState.Rotating || (actualVState == UnitVisualState.Idle && LogicUnit.Class.IdlePhases <= 1)) { actualFrame = actualAngle16; } else if (actualVState == UnitVisualState.Idle) { actualFrame = sprites.Frames.Length - LogicUnit.Class.IdlePhases * countFull8 + LogicUnit.Class.IdlePhases * actualAngle8; actualFrame += LogicUnit.Class.IdleFrames[LogicUnit.IdleFrame].Frame; } else if (actualVState == UnitVisualState.Moving) { int moveSize = LogicUnit.Class.MoveBeginPhases + LogicUnit.Class.MovePhases; actualFrame = countFull16 + moveSize * actualAngle8; actualFrame += LogicUnit.Class.MoveBeginPhases; // movebeginphases, we don't animate this yet actualFrame += LogicUnit.Class.MoveFrames[LogicUnit.MoveFrame].Frame; } else if (actualVState == UnitVisualState.Attacking) { int moveSize = LogicUnit.Class.MoveBeginPhases + LogicUnit.Class.MovePhases; int attackSize = LogicUnit.Class.AttackPhases; actualFrame = countFull16 + moveSize * countFull8 + attackSize * actualAngle8; actualFrame += LogicUnit.Class.AttackFrames[LogicUnit.AttackFrame].Frame; } else if (actualVState == UnitVisualState.Dying) { UnitClass dCls = LogicUnit.Class; while (dCls.Dying != null && dCls.Dying != dCls) { dCls = dCls.Dying; } dCls.File.UpdateSprite(); sprites = dCls.File.File; Renderer.material.SetTexture("_Palette", dCls.File.UpdatePalette(LogicUnit.Face)); int moveSize = dCls.MoveBeginPhases + dCls.MovePhases; int attackSize = dCls.AttackPhases; int dyingSize = dCls.DyingPhases; actualFrame = countFull16 + moveSize * countFull8 + attackSize * countFull8 + dyingSize * actualAngle8; actualFrame += LogicUnit.DeathFrame; } Vector2 xP = MapView.Instance.MapToScreenCoords(LogicUnit.X + LogicUnit.FracX + (float)LogicUnit.Width / 2, LogicUnit.Y + LogicUnit.FracY + (float)LogicUnit.Height / 2, 1, 1); CurrentPoint = xP; float zInv = 0; if (!bAlive) { zInv = 48; } else if (LogicUnit.IsFlying) { zInv = -128; } transform.localPosition = new Vector3(xP.x, xP.y, MakeZFromY(xP.y) + zInv); // order sprites by y coordinate basically //Debug.Log(string.Format("{0} {1} {2}", xP.x, sprites.Sprites[0].rect.width, LogicUnit.Class.CenterX)); //Renderer.sprite = sprites.Sprites[actualFrame]; UnitMesh = UpdateMesh(sprites, actualFrame, Filter.mesh, 0, (UnitMesh == null), doFlip); ShadowMesh = UpdateMesh(sprites, actualFrame, ShadowFilter.mesh, 0.3f, (ShadowMesh == null), doFlip); // 0.3 of sprite height UpdateHpMesh(); LogicUnit.DoUpdateView = false; } }
private Mesh UpdateMesh(Images.AllodsSpriteSeparate sprite, Images.AllodsSpriteSeparate spriteB, int frame, Mesh mesh, float shadowOffs, bool first, bool flip) { // main Texture2D sTex = sprite.Frames[frame].Texture; float sW = sprite.Frames[frame].Width; float sH = sprite.Frames[frame].Height; float tMaxX = sW / sTex.width; float tMaxY = sH / sTex.height; // additional Texture2D sTexB = (spriteB != null) ? spriteB.Frames[frame].Texture : null; float sWB = (spriteB != null) ? spriteB.Frames[frame].Width : 0; float sHB = (spriteB != null) ? spriteB.Frames[frame].Height : 0; float tMaxXB = (spriteB != null) ? (sWB / sTexB.width) : 0; float tMaxYB = (spriteB != null) ? (sHB / sTexB.height) : 0; // bool flying = LogicUnit.IsFlying; // main float shadowOffsReal = shadowOffs * sH; float shadowOffsXLeft = -shadowOffsReal * (1f - LogicUnit.Class.CenterY); // additional float shadowOffsRealB = shadowOffs * sHB; float shadowOffsXLeftB = -shadowOffsRealB * (1f - LogicUnit.Class.CenterY); // int vertexCount = (spriteB != null) ? 8 : 4; Vector3[] qv = new Vector3[vertexCount]; int pp = 0; if (!flying || (shadowOffs == 0)) { // main sprite qv[pp++] = new Vector3(shadowOffsReal, 0, 0); qv[pp++] = new Vector3(shadowOffsReal + sW, 0, 0); qv[pp++] = new Vector3(shadowOffsXLeft + sW, sH, 0); qv[pp++] = new Vector3(shadowOffsXLeft, sH, 0); // calculate offset for spriteB, if it's not the same size if (spriteB != null) { // additional sprite qv[pp++] = new Vector3(shadowOffsRealB, 0, 0); qv[pp++] = new Vector3(shadowOffsRealB + sWB, 0, 0); qv[pp++] = new Vector3(shadowOffsXLeftB + sWB, sHB, 0); qv[pp++] = new Vector3(shadowOffsXLeftB, sHB, 0); } } else { float rShadowOffs = shadowOffs; float shadowOffs1 = rShadowOffs * 64; float shadowOffs2 = shadowOffs1 + sW; shadowOffs = -4; qv[pp++] = new Vector3(shadowOffs1, shadowOffs, 0); qv[pp++] = new Vector3(shadowOffs2, shadowOffs, 0); qv[pp++] = new Vector3(shadowOffs2, shadowOffs + sH, 0); qv[pp++] = new Vector3(shadowOffs1, shadowOffs + sH, 0); // calculate offset for spriteB, if it's not the same size if (spriteB != null) { float shadowOffs1B = rShadowOffs * 64; float shadowOffs2B = shadowOffs1B + sWB; // additional sprite qv[pp++] = new Vector3(shadowOffs1B, shadowOffs, 0); qv[pp++] = new Vector3(shadowOffs2B, shadowOffs, 0); qv[pp++] = new Vector3(shadowOffs2B, shadowOffs + sHB, 0); qv[pp++] = new Vector3(shadowOffs1B, shadowOffs + sHB, 0); } } Vector2[] quv = new Vector2[vertexCount]; if (!flip) { quv[0] = new Vector2(0, 0); quv[1] = new Vector2(tMaxX, 0); quv[2] = new Vector2(tMaxX, tMaxY); quv[3] = new Vector2(0, tMaxY); if (spriteB != null) { quv[4] = new Vector2(0, 0); quv[5] = new Vector2(tMaxXB, 0); quv[6] = new Vector2(tMaxXB, tMaxYB); quv[7] = new Vector2(0, tMaxYB); } } else { quv[0] = new Vector2(tMaxX, 0); quv[1] = new Vector2(0, 0); quv[2] = new Vector2(0, tMaxY); quv[3] = new Vector2(tMaxX, tMaxY); if (spriteB != null) { quv[4] = new Vector2(tMaxXB, 0); quv[5] = new Vector2(0, 0); quv[6] = new Vector2(0, tMaxYB); quv[7] = new Vector2(tMaxXB, tMaxYB); } } float cx = (int)(sprite.Frames[frame].Width * LogicUnit.Class.CenterX); float cy = (int)(sprite.Frames[frame].Height * LogicUnit.Class.CenterY); for (int i = 0; i < 4; i++) { qv[i] -= new Vector3(cx, cy, 0); } if (spriteB != null) { float cxB = (int)(spriteB.Frames[frame].Width * LogicUnit.Class.CenterX); float cyB = (int)(spriteB.Frames[frame].Height * LogicUnit.Class.CenterY); for (int i = 4; i < 8; i++) { qv[i] -= new Vector3(cxB, cyB, 0); } } mesh.subMeshCount = (spriteB != null) ? 2 : 1; mesh.vertices = qv; mesh.uv = quv; Color[] qc = new Color[vertexCount]; qc[0] = qc[1] = qc[2] = qc[3] = new Color(1, 1, 1, 1); if (spriteB != null) { qc[4] = qc[5] = qc[6] = qc[7] = new Color(1, 1, 1, 0.5f); } mesh.colors = qc; int[] qt = new int[4]; for (int i = 0; i < qt.Length; i++) { qt[i] = i; } mesh.SetIndices(qt, MeshTopology.Quads, 0); if (spriteB != null) { int[] qtB = new int[4]; for (int i = 0; i < qtB.Length; i++) { qtB[i] = i + 4; } mesh.SetIndices(qtB, MeshTopology.Quads, 1); } Renderer.materials[0].mainTexture = sTex; ShadowRenderer.materials[0].mainTexture = sTex; if (spriteB != null) { Renderer.materials[1].mainTexture = sTexB; ShadowRenderer.materials[1].mainTexture = sTexB; } return(mesh); }
private Mesh UpdateMesh(Images.AllodsSpriteSeparate sprite, int frame, Mesh mesh, float shadowOffs, bool first, bool flip) { Texture2D sTex = sprite.Frames[frame].Texture; float sW = sprite.Frames[frame].Width; float sH = sprite.Frames[frame].Height; float tMaxX = sW / sTex.width; float tMaxY = sH / sTex.height; bool flying = LogicUnit.IsFlying; float shadowOffsReal = shadowOffs * sH; float shadowOffsXLeft = -shadowOffsReal * (1f - LogicUnit.Class.CenterY); Vector3[] qv = new Vector3[4]; int pp = 0; if (!flying || (shadowOffs == 0)) { qv[pp++] = new Vector3(shadowOffsReal, 0, 0); qv[pp++] = new Vector3(shadowOffsReal + sW, 0, 0); qv[pp++] = new Vector3(shadowOffsXLeft + sW, sH, 0); qv[pp++] = new Vector3(shadowOffsXLeft, sH, 0); } else { float shadowOffs1 = shadowOffs * 64; float shadowOffs2 = shadowOffs1 + sW; shadowOffs = -4; qv[pp++] = new Vector3(shadowOffs1, shadowOffs, 0); qv[pp++] = new Vector3(shadowOffs2, shadowOffs, 0); qv[pp++] = new Vector3(shadowOffs2, shadowOffs + sH, 0); qv[pp++] = new Vector3(shadowOffs1, shadowOffs + sH, 0); } Vector2[] quv = new Vector2[4]; if (!flip) { quv[0] = new Vector2(0, 0); quv[1] = new Vector2(tMaxX, 0); quv[2] = new Vector2(tMaxX, tMaxY); quv[3] = new Vector2(0, tMaxY); } else { quv[0] = new Vector2(tMaxX, 0); quv[1] = new Vector2(0, 0); quv[2] = new Vector2(0, tMaxY); quv[3] = new Vector2(tMaxX, tMaxY); } float cx = (float)sprite.Frames[frame].Width * LogicUnit.Class.CenterX; float cy = (float)sprite.Frames[frame].Height * LogicUnit.Class.CenterY; for (int i = 0; i < qv.Length; i++) { qv[i] -= new Vector3(cx, cy, 0); } mesh.vertices = qv; mesh.uv = quv; if (first) { Color[] qc = new Color[4]; qc[0] = qc[1] = qc[2] = qc[3] = new Color(1, 1, 1, 1); mesh.colors = qc; int[] qt = new int[4]; for (int i = 0; i < qt.Length; i++) { qt[i] = i; } mesh.SetIndices(qt, MeshTopology.Quads, 0); } Renderer.material.mainTexture = sTex; ShadowRenderer.material.mainTexture = sTex; return(mesh); }