Skip to content

Creating blocks

Agzam4 edited this page Nov 11, 2021 · 2 revisions

Creating blocks

Creating blocks

All classes must inherit extends the game.Block.java

public class MyBlock extends Block {

	public Wires() {
		super(...);
	}

}

You can also inherit your class from other Block-classes (see blocks Types)

Use: Transparent - can the block transmit light:

@Override
public boolean isTransparent() {
	return isTransparent;
}

Draw - block drawing:

@Override
public void draw(Graphics2D g, Game game, int x, int y) {
	super.draw(g, game, x, y); // Default draw
}

Update - block updating:

@Override
public void update(Game game, int x, int y) {
	super.update(game,x,y); // <- this is line is important. It is better not to delete it
	...
}

Blocks Types

Block

mechanism.Mechanism.java

public class MyBlock extends Block {

	public Wires() {
		super(...);
	}

}

Has 4 constructors:

  • Block(Color color, boolean isVoid)
  • Block(Color color, boolean isVoid, boolean isFixed)
  • Block(Color color, Color light, boolean isVoid)
  • Block(Color color, Color light, boolean isVoid, boolean isFixed)

color - Block color

light - Block light color

isVoid - It is better to set false :D

isFixed - Can the block fall (false - falling, true - fixed)

Mechanism

mechanism.Mechanism.java

public class MyMechanism extends Mechanism {
	...
}

Blocks inherited from Mechanism:

  • Will have a white border (1px)
  • Method isMechanism() will return true
  • will be updated first (See game.Game.update())
public class MyMechanism extends Mechanism {
	...
}

Wires

wires.Wires.java

public class MyMechanism extends Wires {
	...
}

Has 2 constructors:

  • Wires() - color = Color.WHITE light = Color.BLACK
  • Wires(Color color, Color light)

Blocks inherited from Wires:

  • Method isTransparent() will return true
  • Method isWiretype() will return true

When I created MyBlock

To user can use your block, add it to enum game.Game.Blocks:

public enum Blocks {
	<other enum-fields>
	MY_BLOCK (new MyBlock()), // <--- add here
	<enum-field>;

	<methods>
}

enum Blocks constructor: Blocks(Block block)

Clone this wiki locally