# Arup Guha
# 6/6/2024
# Rewritten Token Class for a square.

import random
import math
import time
import pygame, sys
from pygame.locals import *

# Token Class we'll use for drawing objects in pyGame
class token:

    # Default settings.
    x = 0
    y = 0
    dx = 0
    dy = 0
    side = 0
    color = pygame.Color(0,0,255)

    # Constructor.
    def __init__(self,myx,myy,mydx,mydy,myside,mycolor):
        self.x = myx
        self.y = myy
        self.dx = mydx
        self.dy = mydy
        self.side = myside
        self.color = mycolor

    # Call each frame.
    def move(self):
        self.x += self.dx
        self.y += self.dy

    # Executes updating dx as necessary for bouncing off the left wall.
    def bounceLeft(self):
        if self.x + self.dx < 0:
            self.dx = -self.dx

    # Executes updating dx as necessary for bouncing off the right wall.
    def bounceRight(self, SCREEN_W):
        if self.x + self.dx > SCREEN_W-self.side:
            self.dx = -self.dx

    # Executes updating y as necessary for bouncing off the top wall.
    def bounceUp(self):
        if self.y + self.dy < 0:
            self.dy = -self.dy

    # Executes updating y as necessary for bouncing off the bottom wall.
    def bounceDown(self, SCREEN_H):
        if self.y + self.dy > SCREEN_H-self.side:
            self.dy = -self.dy

    # Update for a single frame. Maybe this will typically be overridden.
    def updateFrame(self, DISPLAYSURF):
        self.move()
        self.bounceLeft()
        self.bounceRight(DISPLAYSURF.get_width())
        self.bounceUp()
        self.bounceDown(DISPLAYSURF.get_height())
        self.draw(DISPLAYSURF)

    # Draws this object on the display surface as a circle.
    # Likely to be overridden most of the time.
    def draw(self, DISPLAYSURF):
        pygame.draw.rect(DISPLAYSURF, self.color, (self.x, self.y, self.side, self.side), 0)

    # This is rectangle collision.
    def collide(self, other):

        # One rectangle is to the right of the other completely.
        if self.x + self.side < other.x or other.x+other.side < self.x:
            return False

        # One rectangle is below the other completely.
        if self.y + self.side < other.y or other.y+other.side < self.y:
            return False

        # They must intersect.
        return True


