boardscreen code front end
This commit is contained in:
@@ -1,41 +1,58 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public partial class BoardScreen : Node2D {
|
||||
private const string RED_CHIP_PATH = "res://scenes/red_chip.tscn";
|
||||
private const string YELLOW_CHIP_PATH = "res://scenes/yellow_chip.tscn";
|
||||
private const int CHIP_SIZE = 24;
|
||||
private const int CHIP_PADDING = 2;
|
||||
private const int INIT_OFFSET = -(CHIP_SIZE + CHIP_PADDING) * 7 / 2;
|
||||
private const int X_OFF = -(CHIP_SIZE + CHIP_PADDING) * 7 / 2;
|
||||
private const int Y_OFF = 300;
|
||||
|
||||
private TextureButton spwnRed;
|
||||
private TextureButton spwnYlw;
|
||||
private PackedScene redChip;
|
||||
private PackedScene ylwChip;
|
||||
|
||||
private Node2D player1Card;
|
||||
private Node2D player2Card;
|
||||
private PlayerData p1;
|
||||
private PlayerData p2;
|
||||
|
||||
private RigidBody2D[,] chips = new RigidBody2D[6, 7]; // 6 rows 7 cols | 0, 0 is top left
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready() {
|
||||
spwnRed = GetNode<TextureButton>("SpwnRed");
|
||||
spwnYlw = GetNode<TextureButton>("SpwnYlw");
|
||||
|
||||
spwnRed.GetNode<Label>("Label").Text = "Red";
|
||||
spwnYlw.GetNode<Label>("Label").Text = "Ylw";
|
||||
|
||||
spwnRed.Pressed += () => spawnRed(0);
|
||||
spwnYlw.Pressed += () => spawnYellow(0);
|
||||
// Node initialization
|
||||
player1Card = GetNode<Node2D>("Player1Card");
|
||||
player2Card = GetNode<Node2D>("Player2Card");
|
||||
|
||||
redChip = GD.Load<PackedScene>(RED_CHIP_PATH);
|
||||
ylwChip = GD.Load<PackedScene>(YELLOW_CHIP_PATH);
|
||||
}
|
||||
}
|
||||
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta) {
|
||||
|
||||
}
|
||||
if(p1 != null) {
|
||||
Label username = player1Card.GetNode<Label>("Name");
|
||||
username.Text = p1.username;
|
||||
|
||||
if(p1.isReady) {
|
||||
Label status = player1Card.GetNode<Label>("Status");
|
||||
status.Text = "Ready!";
|
||||
status.AddThemeColorOverride("font_color", Colors.LimeGreen);
|
||||
}
|
||||
}
|
||||
|
||||
if(p2 != null) {
|
||||
Label username = player2Card.GetNode<Label>("Name");
|
||||
username.Text = p2.username;
|
||||
|
||||
if(p2.isReady) {
|
||||
Label status = player2Card.GetNode<Label>("Status");
|
||||
status.Text = "Ready!";
|
||||
status.AddThemeColorOverride("font_color", Colors.LimeGreen);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Determines if the column can have a new chip placed
|
||||
@@ -48,16 +65,16 @@ public partial class BoardScreen : Node2D {
|
||||
return -1;
|
||||
|
||||
return getNextAvailRow(col);
|
||||
}
|
||||
}
|
||||
|
||||
public int getNextAvailRow(int col) {
|
||||
for(int i = chips.GetLength(0) - 1; i >= 0; i--) { // Start at bottom
|
||||
if(chips[i, col] == null)
|
||||
for(int i = chips.GetLength(0) - 1; i >= 0; i--) { // Start at bottom
|
||||
if(chips[i, col] == null)
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
private void spawnRed(int col) {
|
||||
int row = canPlaceOnCol(col);
|
||||
@@ -66,13 +83,12 @@ public partial class BoardScreen : Node2D {
|
||||
return;
|
||||
}
|
||||
|
||||
GD.Print("Spawn Red!");
|
||||
RigidBody2D newNode = redChip.Instantiate<RigidBody2D>();
|
||||
newNode.Position = new Vector2(INIT_OFFSET + (CHIP_SIZE + CHIP_PADDING) * col, -(CHIP_SIZE + CHIP_PADDING) * 7);
|
||||
newNode.Position = new Vector2(X_OFF + (CHIP_SIZE + CHIP_PADDING) * col, -(CHIP_SIZE + CHIP_PADDING) * 7);
|
||||
AddChild(newNode);
|
||||
|
||||
chips[row, col] = newNode;
|
||||
}
|
||||
}
|
||||
|
||||
private void spawnYellow(int col) {
|
||||
int row = canPlaceOnCol(col);
|
||||
@@ -81,11 +97,33 @@ public partial class BoardScreen : Node2D {
|
||||
return;
|
||||
}
|
||||
|
||||
GD.Print("Spawn Yellow!");
|
||||
RigidBody2D newNode = ylwChip.Instantiate<RigidBody2D>();
|
||||
newNode.Position = new Vector2(INIT_OFFSET + (CHIP_SIZE + CHIP_PADDING) * col, -(CHIP_SIZE + CHIP_PADDING) * 7);
|
||||
newNode.Position = new Vector2(X_OFF + (CHIP_SIZE + CHIP_PADDING) * col, -(CHIP_SIZE + CHIP_PADDING) * 7);
|
||||
AddChild(newNode);
|
||||
|
||||
chips[row, col] = newNode;
|
||||
}
|
||||
|
||||
private void setPlayerCardScale(float x, Node2D playerCard) {
|
||||
Sprite2D cardCenter = playerCard.GetNode<Sprite2D>("Center");
|
||||
Sprite2D cardRight = playerCard.GetNode<Sprite2D>("Right");
|
||||
|
||||
float offX = 16 * x / 2 - 16;
|
||||
cardCenter.Scale = new Vector2(x, 2);
|
||||
cardCenter.Position += new Vector2(offX, 0);
|
||||
|
||||
cardRight.Position += new Vector2(offX * 2, 0);
|
||||
}
|
||||
}
|
||||
|
||||
enum Direction
|
||||
{
|
||||
UP,
|
||||
UP_RIGHT,
|
||||
RIGHT,
|
||||
DOWN_RIGHT,
|
||||
DOWN,
|
||||
DOWN_LEFT,
|
||||
LEFT,
|
||||
UP_LEFT
|
||||
}
|
||||
12
scripts/red_chip.gd
Normal file
12
scripts/red_chip.gd
Normal file
@@ -0,0 +1,12 @@
|
||||
extends RigidBody2D
|
||||
|
||||
var color_ = "red";
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(_delta: float) -> void:
|
||||
pass
|
||||
1
scripts/red_chip.gd.uid
Normal file
1
scripts/red_chip.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dd5nu6037qsr0
|
||||
12
scripts/yellow_chip.gd
Normal file
12
scripts/yellow_chip.gd
Normal file
@@ -0,0 +1,12 @@
|
||||
extends RigidBody2D
|
||||
|
||||
var color_ = "yellow";
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(_delta: float) -> void:
|
||||
pass
|
||||
1
scripts/yellow_chip.gd.uid
Normal file
1
scripts/yellow_chip.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://ddg7dv686sbrb
|
||||
Reference in New Issue
Block a user