Abdulla-binissa / Crowdcrow

Apache License 2.0
0 stars 0 forks source link

Chicken Individual Movement #5

Open Abdulla-binissa opened 3 months ago

Abdulla-binissa commented 3 months ago

Original task: Hexbased movement

Variables to consider to be easily adjustable:

ChatGPT Generated:

To achieve similar functionality in Godot 4, you can utilize the GDScript language to implement a Chicken class with hex-based movement. Below is an example implementation:

GDScript Implementation

extends Node2D

class_name Chicken

# Variables
var current_hex_coordinate: Vector2
var acceleration: float
var direction: int
var velocity: float

# Constants
const HEX_DIRECTIONS = [
    Vector2(1, 0), Vector2(1, -1), Vector2(0, -1),
    Vector2(-1, 0), Vector2(-1, 1), Vector2(0, 1)
]

# Constructor
func _init(x: int, y: int, acc: float = 0.0, dir: int = 0, vel: float = 0.0):
    current_hex_coordinate = Vector2(x, y)
    acceleration = acc
    direction = dir
    velocity = vel

# Walk method
func walk():
    velocity += acceleration
    var next_hex_coordinate = current_hex_coordinate + HEX_DIRECTIONS[direction] * velocity
    current_hex_coordinate = next_hex_coordinate
    position = hex_to_pixel(current_hex_coordinate)

# Turn left method
func turn_left():
    direction = (direction - 1) % 6

# Turn right method
func turn_right():
    direction = (direction + 1) % 6

# Utility: Convert hex coordinate to pixel position
func hex_to_pixel(hex: Vector2) -> Vector2:
    var x = hex.x * TILE_WIDTH + (hex.y % 2) * TILE_WIDTH / 2
    var y = hex.y * TILE_HEIGHT * 0.75
    return Vector2(x, y)

# Utility: Convert pixel position to hex coordinate
func pixel_to_hex(pixel: Vector2) -> Vector2:
    var q = (pixel.x * 2/3) / TILE_WIDTH
    var r = (-pixel.x / 3 + sqrt(3)/3 * pixel.y) / TILE_HEIGHT
    return Vector2(round(q), round(r))

# Constants for tile dimensions (adjust these based on your hex tile size)
const TILE_WIDTH = 64
const TILE_HEIGHT = 64

# Example function to demonstrate movement
func _ready():
    _init(0, 0, 0.5, 0, 1)
    walk()
    print("Current Hex Coordinate: ", current_hex_coordinate)
    turn_left()
    walk()
    print("Current Hex Coordinate: ", current_hex_coordinate)

Explanation

  1. Class Definition: Define the Chicken class extending from Node2D.
  2. Variables and Constants:
    • current_hex_coordinate: Current position in hex coordinates.
    • acceleration: Acceleration value.
    • direction: Current direction (0 to 5 for 6 possible hex directions).
    • velocity: Current velocity.
    • HEX_DIRECTIONS: List of directions for hex movement.
  3. Constructor: Initialize the chicken's position, acceleration, direction, and velocity.
  4. walk Method: Calculate the next hex coordinate based on the current direction and velocity, then update the position.
  5. turn_left and turn_right Methods: Update the direction for turning left and right.
  6. Utility Methods: Convert between hex coordinates and pixel positions for rendering.
  7. Constants for Tile Dimensions: Adjust based on the size of your hex tiles.
  8. Example Function (_ready): Demonstrate the initialization and movement of the chicken.

This script will give you a basic framework for hex-based movement in Godot 4. Adjust the TILE_WIDTH and TILE_HEIGHT constants according to the size of your hex tiles.

Abdulla-binissa commented 2 months ago

Image Moving an object unit from current hex tile to one tile in one of the 6 hex-tile directions.