Prev: BDBB Up: Map Next: BF41
BE01: Add Object Entity
Used by the routine at DrawObject.
Called by DrawObject for an object that can be an entity. Checks that the object is in view and, if it is of kind 02 03 or 04 makes an entity for it in the pool of four entities at B670. The entity is of type 0C for kind 02 and type 10 for the other two. Any object that is in view is also recorded with RecordObject.
Input
IX The object, which is three bytes
HL The screen column of the object
Check that the object is not off the left or right edge of the view.
AddObjectEntity BE01 BIT 7,H Jump to AddObjectEntity_RightEdge if bit 7 of the high byte of the screen column is set.
BE03 JR NZ,AddObjectEntity_RightEdge
BE05 LD BC,$0028 BC is 28 the margin on the left of the view.
BE08 ADD HL,BC Add the margin to the screen column.
BE09 RL H Rotate bit 7 of the high byte into the carry flag.
BE0B RET NC Return if the object is off the left edge of the view.
BE0C JR AddObjectEntity_Position Jump to AddObjectEntity_Position.
AddObjectEntity_RightEdge BE0E LD BC,$80E0 BC is 80E0 the right edge of the view.
BE11 SBC HL,BC Subtract it from the screen column.
BE13 RET NC Return if the object is off the right edge of the view.
Work out the coordinates of the object. The x coordinate is the column of the cell and the upper four bits of the first byte of the object. The y coordinate is the row of the cell and the lower four bits, multiplied by sixteen. They are kept on the stack for the entity.
AddObjectEntity_Position BE14 LD A,($5CBA) Fetch the row of the cell and keep it in D.
BE17 LD D,A
BE18 LD A,(IX+$00) Fetch the first byte of the object.
BE1B ADD A,A Multiply it by sixteen, to move the lower four bits to the top...
BE1C ADD A,A
BE1D ADD A,A
BE1E ADD A,A
BE1F LD E,A ...and keep the result in E, as the low byte of the y coordinate.
BE20 LD HL,($5CB8) Fetch the column of the cell into H.
BE23 LD A,(IX+$00) Fetch the first byte of the object.
BE26 AND %11110000 Keep the upper four bits.
BE28 LD L,A Keep them in L, as the low byte of the x coordinate.
BE29 PUSH DE Save the y coordinate.
BE2A PUSH HL Save the x coordinate.
Work out the screen row of the object. This is half of the difference between its x and y coordinates, plus the offset to the view, less the height of the object.
BE2B SRL D Halve the y coordinate.
BE2D RR E
BE2F SRL H Halve the x coordinate.
BE31 RR L
BE33 SBC HL,DE Subtract half of the y coordinate from half of the x coordinate.
BE35 LD DE,($C061) Fetch the offset to the view from the drawing code.
BE39 ADD HL,DE Add the offset.
BE3A LD A,(IX+$01) Fetch the second byte of the object.
BE3D AND %01110000 Keep the height in bits 4-6.
BE3F LD E,A Keep it in E.
BE40 LD A,(IX+$02) Fetch the third byte of the object.
BE43 ADD A,A Move bit 6, the top bit of the height, to bit 7...
BE44 AND %10000000 ...and keep it.
BE46 ADD A,E Add the other bits of the height to find the height of the object.
BE47 LD E,A Keep the low byte of the height in E.
BE48 LD D,$00 Clear D.
BE4A SBC HL,DE Subtract the height from the screen row.
Check that the object is not above or below the view.
BE4C BIT 7,H Jump to AddObjectEntity_TopMargin if bit 7 of the high byte of the screen row is not set.
BE4E JR Z,AddObjectEntity_TopMargin
BE50 LD DE,$80A4 DE is 80A4 the bottom of the view.
BE53 SBC HL,DE Subtract it from the screen row.
BE55 POP BC Restore the x coordinate.
BE56 POP DE Restore the y coordinate.
BE57 RET NC Return if the object is below the view.
BE58 JR AddObjectEntity_Pool Jump to AddObjectEntity_Pool.
AddObjectEntity_TopMargin BE5A LD DE,$0028 DE is 28 the margin at the top of the view.
BE5D ADD HL,DE Add the margin to the screen row.
BE5E RL H Rotate bit 7 of the high byte into the carry flag.
BE60 POP BC Restore the x coordinate.
BE61 POP DE Restore the y coordinate.
BE62 RET NC Return if the object is above the view.
Only objects of kind 02 03 and 04 that are not hidden have an entity. Record all of the others.
AddObjectEntity_Pool BE63 LD IY,$B670 Point IY at the pool of four entities for objects.
BE67 LD A,(IX+$02) Fetch the third byte of the object.
BE6A BIT 7,A Jump to RecordObject to record the object if it has one.
BE6C JP NZ,RecordObject
BE6F AND %00011111 Keep the kind of the object.
BE71 CP $03 Jump to AddObjectEntity_Search if it is kind 03.
BE73 JR Z,AddObjectEntity_Search
BE75 CP $04 Jump to AddObjectEntity_Search if it is kind 04.
BE77 JR Z,AddObjectEntity_Search
BE79 CP $02 Jump to RecordObject to record the object if it is not kind 02.
BE7B JP NZ,RecordObject
Search the pool for an entity that is not in use.
AddObjectEntity_Search BE7E LD A,$04 Set the number of entities left to try to 04.
BE80 EX DE,HL Put the y coordinate into HL.
BE81 LD DE,$0026 Set the size of an entity, 26.
AddObjectEntity_Next BE84 EX AF,AF' Save the number of entities left.
BE85 LD A,(IY+$00) Fetch the type of the entity.
BE88 AND A Test it.
BE89 JP NZ,AddObjectEntity_NextEntity Jump to AddObjectEntity_NextEntity if the entity is in use.
Make the entity. It starts as type 0C which is kind 02.
BE8C LD (IY+$02),B Store the high byte of the x coordinate in field 02 of the entity.
BE8F LD (IY+$03),C Store the low byte of the x coordinate in field 03.
BE92 LD (IY+$05),H Store the high byte of the y coordinate in field 05.
BE95 LD A,L Fetch the low byte of the y coordinate.
BE96 ADD A,$04 Add 04 to put the entity in the middle of the cell.
BE98 LD L,A Keep it in L.
BE99 LD (IY+$06),L Store it in field 06.
BE9C LD (IY+$1C),$00 Set the animation counter in field 1C to 00.
BEA0 LD (IY+$1D),$20 Set the first and last frames in field 1D to 20.
BEA4 LD (IY+$00),$0C Set the type of the entity to 0C.
BEA8 LD (IY+$0B),$0C Set the size in field 0B to 0C.
BEAC LD (IY+$0C),$0C Set the size in field 0C to 0C.
BEB0 LD (IY+$0D),$01 Set field 0D to 01.
BEB4 LD A,(IX+$02) Fetch the third byte of the object.
BEB7 AND %00011111 Keep the kind of the object.
BEB9 CP $02 Jump to AddObjectEntity_Fields if it is kind 02.
BEBB JR Z,AddObjectEntity_Fields
BEBD LD (IY+$00),$10 Change the type of the entity to 10.
BEC1 LD (IY+$1D),$00 Set the first and last frames in field 1D to 00.
BEC5 LD (IY+$0D),$0A Set field 0D to 0A.
BEC9 CP $03 Jump to AddObjectEntity_Fields if it is kind 03.
BECB JR Z,AddObjectEntity_Fields
BECD LD (IY+$1D),$11 Set the first and last frames in field 1D to 11 for kind 04.
BED1 LD (IY+$0D),$10 Set field 0D to 10.
Work out the z coordinate of the entity from the height of the object, less one.
AddObjectEntity_Fields BED5 LD A,(IX+$01) Fetch the second byte of the object.
BED8 AND %01110000 Keep the height in bits 4-6.
BEDA LD E,A Keep it in E.
BEDB LD (IY+$08),$01 Set the high byte of the z coordinate in field 08 to 01.
BEDF LD A,(IX+$02) Fetch the third byte of the object.
BEE2 ADD A,A Move bit 6, the top bit of the height, to bit 7...
BEE3 AND %10000000 ...and keep it.
BEE5 ADD A,E Add the other bits of the height.
BEE6 SUB $01 Take away one.
BEE8 LD (IY+$09),A Store it in field 09 as the low byte of the z coordinate.
BEEB JR NC,AddObjectEntity_SetFrame Jump to AddObjectEntity_SetFrame if it did not go below zero.
BEED DEC (IY+$08) Decrease the high byte of the z coordinate by one.
AddObjectEntity_SetFrame BEF0 LD A,(IY+$1D) Fetch the first frame of the entity from field 1D.
BEF3 AND %00001111 Keep the frame.
BEF5 OR %10000000 Set bit 7 to hide the entity until it is known to be in view.
BEF7 LD (IY+$01),A Store it as the frame of the entity in field 01.
Remember the address of the object in fields 1A and 1B of the entity.
BEFA PUSH IX Put the address of the object into DE, via the stack.
BEFC POP DE
BEFD LD (IY+$1A),D Store the high byte in field 1A.
BF00 LD (IY+$1B),E Store the low byte in field 1B.
Check that the new entity is in view, and free it again if it is not.
BF03 EXX Switch to the alternate registers, to keep the coordinates.
BF04 PUSH IX Save the address of the object.
BF06 PUSH IY Put the address of the new entity into IX, via the stack.
BF08 POP IX
BF0A LD (IX+$16),$00 Set the width of the sprite in field 16 to 00.
BF0E LD (IX+$17),$18 Set the height of the sprite in field 17 to 18.
BF12 LD BC,$0010 Set the margin to 10.
BF15 CALL TestEntityVisible Test whether the entity is in view.
BF18 JR NZ,AddObjectEntity_Visible Jump to AddObjectEntity_Visible if it is.
BF1A LD (IX+$00),$00 Set the type of the entity to 00 to free it.
BF1E LD (IX+$01),$80 Set the frame of the entity to 80 so that it is hidden.
BF22 POP IX Restore the address of the object.
BF24 JR AddObjectEntity_Done Jump to AddObjectEntity_Done.
The entity is in view. Work out where it is on the screen and which sprite it has, and mark the object as having an entity so that it is not made again.
AddObjectEntity_Visible BF26 CALL ProjectEntity Work out the screen position of the entity.
BF29 CALL SetEntitySize Fetch the sprite of the entity.
BF2C POP IX Restore the address of the object.
BF2E POP HL Take the return address off the stack.
BF2F POP DE Take the screen column of the object off the stack.
BF30 POP BC Take the second and third bytes of the object off the stack.
BF31 SET 7,B Set bit 7 of the third byte, to show that the object has an entity.
BF33 PUSH BC Put the bytes of the object back on the stack...
BF34 PUSH DE ...then the screen column...
BF35 PUSH HL ...and then the return address.
AddObjectEntity_Done BF36 EXX Switch back to the main registers.
BF37 JR AddObjectEntity_Record Jump to AddObjectEntity_Record to record the object.
AddObjectEntity_NextEntity BF39 ADD IY,DE Move on to the next entity of the pool.
BF3B EX AF,AF' Restore the number of entities left.
BF3C DEC A Decrease the number of entities left.
BF3D JP NZ,AddObjectEntity_Next Jump back to AddObjectEntity_Next until all four have been tried.
AddObjectEntity_Record BF40 EX DE,HL Put the y coordinate back into DE, and carry on into RecordObject to record the object.
Continue on to RecordObject.
Prev: BDBB Up: Map Next: BF41