Example #1
0
class Weapon:
    def __init__(self, pos, vel, nextPosTime, nextPos, maxVel, angle, radius,
                 spriteKey, spriteDictionary, spriteFps, idObject, numRows,
                 numColumns, startRow, startColumn, endRow, endColumn,
                 removeOnVelocity0, removeOnAnimationLoop, damage):
        # id's
        self.remove = False
        self.idClass = 5
        self.idPlayer = 0  #set on weapon creation.
        self.idObject = idObject
        self.updateSprite = True
        # non-vectors (attributes)
        self.damage = damage
        self.applied = False
        self.sent = False  #the above has a bug that i can't solve, continuous damage application accross network, so I will limit to sending this object once

        # vectors

        # Sprite Attributes
        self.currentTime = 0
        self.removeOnVelocity0 = removeOnVelocity0
        self.removeOnAnimationLoop = removeOnAnimationLoop

        # sub class
        self.particle = Particle(
            self.updateSprite,
            pos,
            vel,
            nextPosTime,
            nextPos,
            maxVel,
            0,
            angle,
            radius,
            spriteKey,  #0 is the range, as this is a weapon, no need.
            spriteDictionary,
            spriteFps,
            self.removeOnVelocity0,
            self.removeOnAnimationLoop,
            self.idObject,
            numRows,
            numColumns,
            startRow,
            startColumn,
            endRow,
            endColumn)

    def recieve(self, nextPos, nextPosTime, maxVel, angle, spriteKey, fps,
                numRows, numColumns, startRow, startColumn, endRow, endColumn,
                radius, spriteDictionary, applied):

        self.particle.recieve(nextPos, nextPosTime, maxVel, 0, angle, True,
                              spriteKey, fps, numRows, numColumns, startRow,
                              startColumn, endRow, endColumn, radius,
                              spriteDictionary)
        self.applied = applied

    def draw(self, canvas, cam):
        self.particle.draw(canvas, cam)

    def move(self, pos):
        self.particle.move(pos)

    def update(self):
        self.particle.update()
        self.currentTime = time.time()

    def turn(self, angle):
        self.particle.angle += angle

    def encode(self):
        data = {
            'pos': {
                'x': self.particle.pos.x,
                'y': self.particle.pos.y
            },
            'vel': {
                'x': self.particle.vel.x,
                'y': self.particle.vel.y
            },
            'nextPosTime': self.particle.nextPosTime,
            'nextPos': {
                'x': self.particle.nextPos.x,
                'y': self.particle.nextPos.y
            },
            'maxVel': self.particle.maxVel,
            'angle': self.particle.angle,
            'radius': self.particle.radius,
            'spriteKey': self.particle.spriteKey,
            'fps': self.particle.spriteSheet.fps,
            'idObject': self.idObject,
            'removeOnVelocity0': self.removeOnVelocity0,
            'removeOnAnimationLoop': self.removeOnAnimationLoop,
            'idClass': self.idClass,
            'currentTime': self.currentTime,
            'applied': self.applied,
            'numColumns': self.particle.spriteSheet.numColumns,
            'numRows': self.particle.spriteSheet.numRows,
            'startColumn': self.particle.spriteSheet.startColumn,
            'startRow': self.particle.spriteSheet.startRow,
            'endRow': self.particle.spriteSheet.endRow,
            'endColumn': self.particle.spriteSheet.endColumn,
            'damage': self.damage
        }
        e = json.dumps(data)
        return data
Example #2
0
class Player:
    def __init__(self, pos, vel, nextPosTime, nextPos, maxVel, angle, radius,
                 spriteKey, spriteDictionary, spriteFps, idObject, hasFired,
                 clickPosition, spriteState, numRows, numColumns, startRow,
                 startColumn, endRow, endColumn):
        # id's
        self.remove = False
        self.idClass = 3
        self.idObject = idObject

        # vectors
        self.clickPosition = clickPosition
        # Sprite Attributes
        self.spriteState = spriteState
        self.currentTime = 0
        self.hasFired = hasFired

        self.totalLife = 6000
        self.life = 6000
        self.range = 600
        self.melee = 600  #OPTIONAL,NOT IMPLEMENTED (REQUIRES SPRITE STATES AND MORE TIME)
        self.magic = 600
        self.weapon = 1
        # sub class
        self.particle = Particle(True, pos, vel, nextPosTime, nextPos, maxVel,
                                 0, angle, radius, spriteKey, spriteDictionary,
                                 spriteFps, False, False, self.idObject,
                                 numRows, numColumns, startRow, startColumn,
                                 endRow, endColumn)

    def recieve(self, hasFired, clickPosition, nextPos, nextPosTime, maxVel,
                maxRange, angle, updateSprite, spriteKey, fps, numRows,
                numColumns, startRow, startColumn, endRow, endColumn, radius,
                spriteDictionary, life, range, melee, magic):

        self.life = life
        self.melee = melee
        self.range = range
        self.magic = magic
        self.hasFired = hasFired
        self.clickPosition = clickPosition
        self.particle.recieve(nextPos, nextPosTime, maxVel, maxRange, angle,
                              updateSprite, spriteKey, fps, numRows,
                              numColumns, startRow, startColumn, endRow,
                              endColumn, radius, spriteDictionary)

    def update(self):
        self.particle.update()
        self.setCorrectSpriteState()
        self.currentTime = time.time()

    def draw(self, canvas, cam):
        self.particle.draw(canvas, cam)

    def move(self, pos):
        self.particle.move(pos)

    def setCorrectSpriteState(self):
        if self.spriteState == 0:
            self.setCorrectAnimation(1)
        # CORRECT SPRITE ROW AND UPDATE FPS
        if self.hasFired and self.particle.spriteSheet.hasLooped:
            self.hasFired = False
            self.setCorrectAnimation(1)

        if self.particle.vel.getX() == 0 and self.particle.vel.getY(
        ) == 0 and not self.hasFired:
            self.particle.spriteSheet.currentColumn = 1

    def setCorrectAnimation(self, action):
        if action == 1:
            self.particle.spriteSheet.fps = 20
        if action == 2:
            self.particle.spriteSheet.fps = 30
        if action == 3:
            self.particle.spriteSheet.fps = 5
        x, y = self.particle.pos.copy().distanceToVector(self.clickPosition)
        if y < 0:
            if action == 2:
                self.setSpriteState(6)
            elif action == 3:
                self.setSpriteState(11)
            else:
                self.setSpriteState(3)

        if y > 0:
            if action == 2:
                self.setSpriteState(8)
            elif action == 3:
                self.setSpriteState(9)
            else:
                self.setSpriteState(1)

        if y != 0:
            if (x + y) / y > 2 and y < 0:
                if action == 2:
                    self.setSpriteState(5)
                elif action == 3:
                    self.setSpriteState(12)
                else:
                    self.setSpriteState(4)

            if (x + y) / y < 0 and y < 0:
                if action == 2:
                    self.setSpriteState(7)
                elif action == 3:
                    self.setSpriteState(10)

                else:
                    self.setSpriteState(2)

    def walkUp(self):
        self.particle.spriteSheet.setRow(21, 13, 9, 1, 9, 9)

    def walkLeft(self):
        self.particle.spriteSheet.setRow(21, 13, 10, 1, 10, 9)

    def walkDown(self):
        self.particle.spriteSheet.setRow(21, 13, 11, 1, 11, 9)

    def walkRight(self):
        self.particle.spriteSheet.setRow(21, 13, 12, 1, 12, 9)

    def fireRight(self):
        self.particle.spriteSheet.setRow(21, 13, 20, 1, 20, 13)

    def fireDown(self):
        self.particle.spriteSheet.setRow(21, 13, 19, 1, 19, 13)

    def fireLeft(self):
        self.particle.spriteSheet.setRow(21, 13, 18, 1, 18, 13)

    def fireUp(self):
        self.particle.spriteSheet.setRow(21, 13, 17, 1, 17, 13)

    def magicUp(self):
        self.particle.spriteSheet.setRow(21, 13, 1, 1, 1, 7)

    def magicRight(self):
        self.particle.spriteSheet.setRow(21, 13, 4, 1, 4, 7)

    def magicDown(self):
        self.particle.spriteSheet.setRow(21, 13, 3, 1, 3, 7)

    def magicLeft(self):
        self.particle.spriteSheet.setRow(21, 13, 2, 1, 2, 7)

    def setSpriteState(self, id):
        self.spriteState = id
        self.particle.spriteSheet.resetLoop()
        if id == 1:
            self.walkUp()
        elif id == 2:
            self.walkLeft()
        elif id == 3:
            self.walkDown()
        elif id == 4:
            self.walkRight()
        elif id == 5:
            self.fireRight()
        elif id == 6:
            self.fireDown()
        elif id == 7:
            self.fireLeft()
        elif id == 8:
            self.fireUp()
        elif id == 9:
            self.magicUp()
        elif id == 10:
            self.magicLeft()
        elif id == 11:
            self.magicDown()
        elif id == 12:
            self.magicRight()

    def turn(self, angle):
        self.particle.angle += angle

    def encode(self):

        data = {
            'spriteState': self.spriteState,
            'clickPosition': {
                'x': self.clickPosition.x,
                'y': self.clickPosition.y
            },
            'hasFired': self.hasFired,
            'idObject': self.idObject,
            'idClass': self.idClass,
            'pos': {
                'x': self.particle.pos.x,
                'y': self.particle.pos.y
            },
            'vel': {
                'x': self.particle.vel.x,
                'y': self.particle.vel.y
            },
            'maxVel': self.particle.maxVel,
            'angle': self.particle.angle,
            'radius': self.particle.radius,
            'spriteKey': self.particle.spriteKey,
            'currentTime': self.currentTime,
            'nextPos': {
                'x': self.particle.nextPos.x,
                'y': self.particle.nextPos.y
            },
            'nextPosTime': self.particle.nextPosTime,
            'fps': self.particle.spriteSheet.fps,
            'remove': self.remove,
            'updateSprite': self.particle.updateSprite,
            'maxRange': self.particle.maxRange,
            'numColumns': self.particle.spriteSheet.numColumns,
            'numRows': self.particle.spriteSheet.numRows,
            'startColumn': self.particle.spriteSheet.startColumn,
            'startRow': self.particle.spriteSheet.startRow,
            'endRow': self.particle.spriteSheet.endRow,
            'endColumn': self.particle.spriteSheet.endColumn,
            'magic': self.magic,
            'melee': self.melee,
            'range': self.range,
            'life': self.life,
        }
        c = json.dumps(data)
        return data
Example #3
0
class Monster:
    def __init__(self, pos, vel, nextPosTime, nextPos, maxVel, angle, radius,
                 spriteKey, spriteDictionary, spriteFps, idObject, hasFired,
                 clickPosition, spriteState, numRows, numColumns, startRow,
                 startColumn, endRow, endColumn, tier, aBack, external,
                 totalLife, operationOrigin, operationRange, attackRange,
                 followDistance):
        # id's
        self.remove = False
        self.idClass = 4
        self.idObject = idObject
        self.external = external
        # non-vectors (attributes)
        self.operationOrigin = operationOrigin
        self.operationRange = operationRange
        self.attackRange = attackRange
        self.followDistance = followDistance
        self.returning = False
        self.hasSelectedReturn = False
        self.tier = tier
        self.life = totalLife
        self.lifePrev = self.life
        self.range = 0
        self.melee = 0
        self.magic = 0
        self.tier = 0
        self.totalLife = totalLife
        self.hitByWeapon = 0  #used for when external arrow hits local monster... need to save that arrows id as it can reapear even if removed locally

        # vectors
        self.clickPosition = clickPosition
        # Sprite Attributes
        self.spriteState = spriteState
        self.currentTime = 0
        self.hasFired = hasFired
        self.fireCooldown = 0

        self.aBack = aBack

        #sub class
        self.particle = Particle(True, pos, vel, nextPosTime, nextPos, maxVel,
                                 0, angle, radius, spriteKey, spriteDictionary,
                                 spriteFps, False, False, self.idObject,
                                 numRows, numColumns, startRow, startColumn,
                                 endRow, endColumn)

    def recieve(self, hasFired, clickPosition, nextPos, nextPosTime, maxVel,
                maxRange, angle, updateSprite, spriteKey, fps, numRows,
                numColumns, startRow, startColumn, endRow, endColumn, radius,
                spriteDictionary, life, range, melee, magic, remove):

        self.life = life
        self.melee = melee
        self.range = range
        self.magic = magic
        self.remove = remove
        self.hasFired = hasFired
        self.clickPosition = clickPosition
        self.particle.recieve(nextPos, nextPosTime, maxVel, maxRange, angle,
                              updateSprite, spriteKey, fps, numRows,
                              numColumns, startRow, startColumn, endRow,
                              endColumn, radius, spriteDictionary)

    def update(self):

        self.particle.update()
        self.setCorrectSpriteState()
        self.checkFireCooldown()
        self.currentTime = time.time()

    def setSpriteState(self, state):
        self.spriteState = state
        if state == 1:  # FIRST HALF OF SPRITE SHEET GOING LEFT

            self.particle.spriteSheet.startRow = 1
            self.particle.spriteSheet.endRow = self.particle.spriteSheet.numRows / 2
            self.particle.spriteSheet.currentRow -= self.particle.spriteSheet.numRows / 2
        elif state == 2:  # SECOND HALF OF SPRITE SHEET GOING RIGHT

            self.particle.spriteSheet.endRow = self.particle.spriteSheet.numRows
            self.particle.spriteSheet.startRow = self.particle.spriteSheet.numRows / 2 + 1

            self.particle.spriteSheet.currentRow += self.particle.spriteSheet.numRows / 2

        if self.aBack and self.spriteState == 1 and self.particle.spriteSheet.startColumn < self.particle.spriteSheet.endColumn:
            self.particle.spriteSheet.startColumn = self.particle.spriteSheet.numColumns
            self.particle.spriteSheet.endColumn = self.particle.spriteSheet.numColumns - self.particle.spriteSheet.endColumn
        elif not self.aBack and self.spriteState == 1 and self.particle.spriteSheet.startColumn > self.particle.spriteSheet.endColumn:
            self.particle.spriteSheet.startColumn = 1
            self.particle.spriteSheet.endColumn = self.particle.spriteSheet.numColumns - self.particle.spriteSheet.endColumn
        elif not self.aBack and self.spriteState == 2 and self.particle.spriteSheet.startColumn < self.particle.spriteSheet.endColumn:
            self.particle.spriteSheet.startColumn = self.particle.spriteSheet.numColumns
            self.particle.spriteSheet.endColumn = self.particle.spriteSheet.numColumns - self.particle.spriteSheet.endColumn
        elif self.aBack and self.spriteState == 2 and self.particle.spriteSheet.startColumn > self.particle.spriteSheet.endColumn:
            self.particle.spriteSheet.startColumn = 1
            self.particle.spriteSheet.endColumn = self.particle.spriteSheet.numColumns - self.particle.spriteSheet.endColumn

    def draw(self, canvas, cam):

        self.particle.draw(canvas, cam)
        if SHOW_MONSTER_THOUGHTS:
            ratio = cam.ratioToCam()

            percentage = -self.life / self.totalLife
            simplegui_lib_draw.draw_rect(
                canvas,
                self.particle.pos.copy().add(Vector(
                    0, -100)).transformToCam(cam).getP(),
                Vector(100, 20).multiplyVector(Vector(
                    percentage, 0.2)).multiplyVector(ratio).getP(),
                1,
                'Red',
                fill_color='red')

        if SHOW_MONSTER_THOUGHTS:
            ratio = cam.ratioToCam()

            radius1 = int(self.attackRange * ratio.getX())
            radius2 = int(self.followDistance * ratio.getX())
            canvas.draw_circle(
                self.particle.pos.copy().transformToCam(cam).getP(), radius1,
                1, 'Red')
            canvas.draw_circle(
                self.particle.pos.copy().transformToCam(cam).getP(), radius2,
                1, 'Pink')
            simplegui_lib_draw.draw_rect(
                canvas,
                self.operationOrigin.copy().transformToCam(cam).subtract(
                    self.operationRange.copy().multiplyVector(ratio)).getP(),
                self.operationRange.copy().add(
                    self.operationRange).multiplyVector(ratio).getP(),
                1,
                'White',
                fill_color=None)

    def move(self, pos):
        self.particle.move(pos)

    def checkFireCooldown(self):
        if self.hasFired:
            self.fireCooldown += time.time() - self.currentTime
            if self.fireCooldown > 5:
                self.hasFired = False
                self.fireCooldown = 0

    def turn(self, angle):
        self.particle.angle += angle

    def setCorrectSpriteState(self):
        if self.particle.pos.getX() - self.particle.nextPos.getX(
        ) < 0 and not self.spriteState == 2:
            self.setSpriteState(2)
        elif self.particle.pos.getX() - self.particle.nextPos.getX(
        ) > 0 and not self.spriteState == 1:
            self.setSpriteState(1)

    def encode(self):
        data = {
            'spriteState': self.spriteState,
            'clickPosition': {
                'x': self.clickPosition.x,
                'y': self.clickPosition.y
            },
            'hasFired': self.hasFired,
            'idObject': self.idObject,
            'idClass': self.idClass,
            'pos': {
                'x': self.particle.pos.x,
                'y': self.particle.pos.y
            },
            'vel': {
                'x': self.particle.vel.x,
                'y': self.particle.vel.y
            },
            'maxVel': self.particle.maxVel,
            'angle': self.particle.angle,
            'radius': self.particle.radius,
            'spriteKey': self.particle.spriteKey,
            'currentTime': self.currentTime,
            'nextPos': {
                'x': self.particle.nextPos.x,
                'y': self.particle.nextPos.y
            },
            'nextPosTime': self.particle.nextPosTime,
            'fps': self.particle.spriteSheet.fps,
            'remove': self.remove,
            'updateSprite': self.particle.updateSprite,
            'maxRange': self.particle.maxRange,
            'numColumns': self.particle.spriteSheet.numColumns,
            'numRows': self.particle.spriteSheet.numRows,
            'startColumn': self.particle.spriteSheet.startColumn,
            'startRow': self.particle.spriteSheet.startRow,
            'endRow': self.particle.spriteSheet.endRow,
            'endColumn': self.particle.spriteSheet.endColumn,
            'magic': self.magic,
            'melee': self.melee,
            'range': self.range,
            'life': self.life,
            'tier': self.tier,
            'aBack': self.aBack,
            'external': True,
            'totalLife': self.totalLife,
            'operationOrigin': {
                'x': self.operationOrigin.x,
                'y': self.operationOrigin.y
            },
            'operationRange': {
                'x': self.operationRange.x,
                'y': self.operationRange.y
            },
            'attackRange': self.attackRange,
            'followDistance': self.followDistance
        }
        b = json.dumps(data)
        return data