/// <summary> /// Creates a LevelRefreshStep. /// </summary> /// <param name="items">Items for the setup. It will save it's own copy</param> /// <param name="newLine">Items to show at the new line</param> public LevelRefreshStep(Gem[] items, Gem[] newLine) { this.items = new Gem[items.Length]; for(int i = 0; i < items.Length; i++){ if(items[i] != null) this.items[i] = (Gem)items[i].Clone(); } this.newLine = newLine; }
/// <summary> /// Give a new line after round is finished. /// It's also taking care of NOT passing items if the column is already full. /// </summary> /// <returns>The new line.</returns> public Gem[] GetNewLine() { Gem[] line = new Gem[source.width]; for(int i = 0; i < source.width; i++){ // if the top row has space bring in a new item if(context.GetItem(i) == null){ int yPos = currentLines[i]; currentLines[i]++; if(yPos * source.width + i < source.gems.Length){ line[i] = GemFactory.GetGem(source.gems[yPos * source.width + i]); } } } return line; }
/// <summary> /// This fills the lineCache dependent on the last line. /// While generating the level the last line is simply the last generated one. /// While playing the game the last line is simply the bottom line. /// </summary> /// <param name="lastLine">Last line.</param> public void FillLineCache(Gem[] lastLine) { for(int i = 0; i < levelWidth; i++){ if(lineCache[i] == null){ Gem g = lastLine[i]; //roll a dice for getting the same Color gem then before if(g != null && g.GetType() == typeof(ColorGem)){ int sameThenBefore = random.Next(0,4); if(sameThenBefore == 0){ lineCache[i] = new ColorGem(((ColorGem)g).color); } } //Get random Gem. if(lineCache[i] == null){ lineCache[i] = GetRandomGem(lastLine[i] == null); } } } }
/// <summary> /// Get the level from source. /// </summary> /// <param name="level">level</param> /// <param name="levelSize">levelSize of the level x = height y = wigth</param> public void GetLevel(out Gem[] level, out IntVector2 levelSize) { levelSize = new IntVector2(source.width, source.maxLinesOnScreen); level = new Gem[levelSize.x*levelSize.y]; currentLines = new int[levelSize.x]; //calc free lines int start = source.maxLinesOnScreen - source.linesOnStart; //convert in one dim array pos start = start * levelSize.x; int i = 0; for(; i < source.gems.Length && i < level.Length - start; i++){ level[i + start] = GemFactory.GetGem(source.gems[i]); } start = i + 1; for(int j = 0; j < currentLines.Length; j++){ currentLines[j] = source.linesOnStart; } }
/// <summary> /// Shows the next line. Here the player can see what could the next be line if /// all columns had space. /// </summary> /// <returns>The new line.</returns> public Gem[] ShowNewLine() { Gem[] lastLine = new Gem[levelWidth]; for(int x = 0; x < levelWidth; x++){ lastLine[x] = context.GetItem(new IntVector2(x, levelHeight-1)); } FillLineCache(lastLine); return lineCache; }
/// <summary> /// Give a new line after round is finished. /// It's also taking care of NOT passing items if the column is already full. /// </summary> /// <returns>The new line.</returns> public Gem[] GetNewLine() { Gem[] back = new Gem[levelWidth]; for(int x = 0; x < levelWidth; x++){ if(context.GetItem(new IntVector2(x, 0)) == null){ back[x] = lineCache[x]; lineCache[x] = null; } } return back; }
/// <summary> /// Set a one dimensional level /// </summary> /// <param name="level">Level</param> /// <param name="size">Size of the level</param> public void SetLevel(Gem[] level, IntVector2 size) { bonus = 0; this.size = size; this.items = level; for(int i = 0; i < level.Length; i++){ if(level[i] != null) level[i].Init(this); } }
/// <summary> /// Set a two dimensional level. /// </summary> /// <param name="level">Level.</param> public void SetLevel(Gem[,] level) { bonus = 0; size = new IntVector2(level.GetLength(1), level.GetLength(0)); items = new Gem[size.x * size.y]; int i = 0; for(int y = 0; y < size.y; y++){ for(int x = 0; x < size.x; x++){ items[i++] = level[x,y]; if(level[x,y] != null) level[x,y].Init(this); } } }
/// <summary> /// Sets the gem visuals. /// </summary> /// <param name="pos">Position.</param> /// <param name="g">The Gem item.</param> public void SetGem(int pos, Gem g) { SetGem(IntVector2.FromOneDim(pos, visuals.GetLength(0)),g); }
/// <summary> /// Gets the identifier for a Gem Object. /// </summary> /// <returns>The identifier.</returns> /// <param name="g">The Gem object.</param> public static int GetId(Gem g) { if(g == null){ return (int)GemId.NONE; } else if(g is ColorGem){ int id = 0; switch(((IcedColorGem)g).color){ case ColorGem.GemColor.BLUE: id = id | (int)GemId.BLUE; break; case ColorGem.GemColor.GREEN: id = id | (int)GemId.GREEN; break; case ColorGem.GemColor.LIME: id = id | (int)GemId.LIME; break; case ColorGem.GemColor.ORANGE: id = id | (int)GemId.ORANGE; break; case ColorGem.GemColor.RED: id = id | (int)GemId.RED; break; case ColorGem.GemColor.WHITE: id = id | (int)GemId.WHITE; break; } if(g is IcedColorGem){ id = id | ICE_FAG; }else if(g is BonusColorGem){ id = id | BONUS_FAG; } return id; }else if(g is Target){ return (int)GemId.TARGET; }else if(g is Turn){ return(int) GemId.TURN; }else if(g is Firework){ return (int)GemId.FIREWORK; }else if(g is Bomb){ return (int)GemId.BOMB; } throw new ArgumentException(g.GetType().ToString() + " is not a known type","g"); }
/// <summary> /// Sets the item on x and y. /// </summary> /// <param name="pos">Position.</param> /// <param name="g">A item or null.</param> public void SetItem(int pos, Gem g) { items[pos] = g; }
/// <summary> /// Shows the next line. Here the player can see what could the next line if /// all columns had space. /// </summary> /// <returns>The new line.</returns> public Gem[] ShowNewLine() { Gem[] line = new Gem[source.width]; for(int i = 0; i < source.width; i++){ int yPos = currentLines[i]; if(yPos * source.width + i < source.gems.Length){ line[i] = GemFactory.GetGem(source.gems[yPos * source.width + i]); } } return line; }
/// <summary> /// Equals only for the Color. /// If this Item or a subtype of this item has the same color. /// It returns true otherwise false. /// /// If the Gem is NOT a <see cref="DiamondJam.Control.Gems.ColorGem"/> /// or a subtype it will also return false. /// </summary> /// <returns><c>true</c>, if the color is the same, <c>false</c> otherwise.</returns> /// <param name="o">A other Gem to test</param> public bool ColorEquals(Gem o) { if (!(o is ColorGem)) return false; return this.color == ((ColorGem)o).color; }
/// <summary> /// Change your visuals to represent this Gem /// </summary> /// <param name="g">The Gem item.</param> public void Show(Gem g) { transform.position = new Vector3(startPos.x,-startPos.y,0); name = "("+(int)transform.position.x+","+(-(int)transform.position.y)+")"; if(g == null){ spriteRenderer.enabled = false; ice.SetActive(false); bonus.SetActive(false); name = "None "+name; }else if(g is ColorGem){ spriteRenderer.enabled = true; ColorGem cg = (ColorGem)g; name = "Color "+name; switch (cg.color) { case ColorGem.GemColor.WHITE: spriteRenderer.sprite = white; break; case ColorGem.GemColor.BLUE: spriteRenderer.sprite = blue; break; case ColorGem.GemColor.GREEN: spriteRenderer.sprite = green; break; case ColorGem.GemColor.LIME: spriteRenderer.sprite = lime; break; case ColorGem.GemColor.ORANGE: spriteRenderer.sprite = orange; break; case ColorGem.GemColor.RED: spriteRenderer.sprite = red; break; } if(cg is IcedColorGem){ name = "Iced"+name; int icehp = ((IcedColorGem)cg).hitpoints; ice.SetActive(icehp > 0); }else{ ice.SetActive(false); } if(cg is BonusColorGem){ name = "Bonus"+name; bonus.SetActive(true); }else{ bonus.SetActive(false); } }else{ spriteRenderer.enabled = true; bonus.SetActive(false); ice.SetActive(false); if(g is Bomb){ name = "Bomb "+name; spriteRenderer.sprite = bomb; }else if(g is Firework){ name = "Firework "+name; spriteRenderer.sprite = firework; }else if(g is Target){ name = "Target "+name; spriteRenderer.sprite = target; }else if(g is Turn){ name = "Turn "+name; spriteRenderer.sprite = turn; }else{ Debug.LogError("Unknown Gem Type in Visuals"); } } }
/// <summary> /// Sets the gem visuals. /// </summary> /// <param name="pos">Position.</param> /// <param name="g">The Gem item.</param> public void SetGem(IntVector2 pos, Gem g) { visuals[pos.x,pos.y].Show(g); }
/// <summary> /// Generate the level to view on start. /// </summary> /// <param name="level">level</param> /// <param name="levelSize">levelSize of the level x = height y = wigth</param> public void GetLevel(out Gem[] level, out IntVector2 levelSize) { levelSize = new IntVector2 (levelWidth, levelHeight); lineCache = new Gem[levelWidth]; level = new Gem[levelWidth * levelHeight]; for(int y = freeLines; y < levelHeight; y++){ //read in last line Gem[] lastLine = new Gem[levelWidth]; for(int x = 0; x < levelWidth; x++){ lastLine[x] = level[(y-1) * levelWidth + x]; } //refill Cached Line FillLineCache(lastLine); //Fill in a hippo if needed if(y == freeLines && generateTarget){ lineCache[random.Next(0, levelWidth)] = new Target(); } //copy the lineCache in the level and clear it for(int x = 0; x < levelWidth; x++){ level[y * levelWidth + x] = lineCache[x]; lineCache[x] = null; } } }
/// <summary> /// Gets a Unity Color by Gem Object. /// </summary> /// <returns>The color.</returns> /// <param name="g">The Gem object.</param> public static Color GetColor(Gem g) { return GetColor(GetId(g)); }
public void SetItem(IntVector2 pos, Gem g) { SetItem(pos.ToOneDim(size.x),g); }
/// <summary> /// Sets the item on x and y. /// </summary> /// <param name="x">The x coordinate.</param> /// <param name="y">The y coordinate.</param> /// <param name="g">The green component.</param> public void SetItem(int x, int y, Gem g) { SetItem(new IntVector2(x,y),g); }
/// <summary> /// Sets the extra line. To show the player what comes next. /// </summary> /// <param name="gems">Gems.</param> public void SetExtraLine(Gem[] gems) { for(int x = 0; x < gems.Length; x++){ visuals[x,visuals.GetLength(1)-1].Show(gems[x]); } }