-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.s
154 lines (131 loc) · 5.87 KB
/
main.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
; Rasterline debug
SHOW_PLAYROUTINE_TIME = 0
SHOW_CHARSETANIM_TIME = 0
SHOW_SPRITEIRQ_TIME = 0
SHOW_SCROLLWORK_TIME = 0
SHOW_SKIPPED_FRAME = 0
SHOW_FREE_TIME = 0
SHOW_DEBUG_VARS = 0
; Filename and resource chunk defines
F_MUSIC = $01
F_CHARSET = $02
F_LEVEL = $03
F_CHUNK = $04-1
C_COMMON = 1 ;Common sprites
C_PLAYER = 2 ;Player sprites
C_ENEMY = 3 ;Enemy sprites
C_SCRIPT0 = 4 ;First script (loadable code) file, with player + enemy movement / render code
C_FIRSTPURGEABLE = C_PLAYER ;First chunk that can be removed from memory. Put e.g. always resident spritefiles
;to lower indices
C_FIRSTSCRIPT = C_SCRIPT0 ;Remember to update this, otherwise sprite etc. resources will be relocated as code!
include memory.s
include loadsym.s
include ldepacksym.s
org loaderCodeEnd
EntryPoint: ldx #$ff ;Init stack pointer to top
txs
jsr InitAll ;Call to disposable init code
lda #5 ;Adjust text margin for the health display
sta textLeftMargin
lda #1
jsr PlaySong ;Play music. Note: some music (even a silence tune)
;needs to be initialized to get sound effect playback
;Also, loading music trashes the zone scrolling buffer,
;so must load music before changing the zone / beginning gameplay
lda #0 ;Setup level / zone we're in
jsr ChangeLevel
lda #0
jsr ChangeZone
ldx #ACTI_PLAYER ;Create player actor. Player movement code is in script00.s
lda #ACT_PLAYER ;to demonstrate runtime code loading / relocation
sta actT,x
lda #28
sta actXH,x
lda #$40
sta actXL,x
lda #15
sta actYH,x
lda #$00
sta actD,x
jsr InitActor ;Init health & actor flags
jsr CenterPlayer ;Center scrolling on player & redraw
jsr SavePlayerState ;Make an "in-memory checkpoint" for restarting
jsr UpdateFrame ;Show screen just to demonstrate loading while screen is on
jsr RedrawHUD
ldy #C_COMMON
jsr LoadResource ;Preload the common sprites
MainLoop: jsr ScrollLogic
jsr DrawActors
jsr UpdateFrame
jsr GetControls
jsr ScrollLogic
jsr UpdateActors
jsr UpdateFrame
jsr UpdateLevelObjects
jsr RedrawHUD
jsr CheckRestart
jmp MainLoop
RedrawHUD: lda actHp+ACTI_PLAYER ;Redraw health to left side of scorepanel if changed
cmp displayedHealth
beq RH_SameHealth
sta displayedHealth
jsr ConvertToBCD8
ldx #0
lda #94 ;Health symbol
jsr PrintPanelChar
jsr Print3BCDDigits ;Health value
RH_SameHealth: lda ammo ;Redraw ammo to right side of scorepanel if changed
cmp displayedAmmo
beq RH_SameAmmo
sta displayedAmmo
jsr ConvertToBCD8
ldx #34
lda #95 ;Ammo symbol
jsr PrintPanelChar
jsr Print3BCDDigits ;Ammo value
RH_SameAmmo: rts
CheckRestart: lda actT+ACTI_PLAYER ;Check if player actor vanished (destroyed)
bne CR_NoRestart
sta scrollSX ;Stop scrolling
sta scrollSY
dec restartDelay ;Two second delay (50 mainloops or 100 frames)
bne CR_NoRestart
lda #50
sta restartDelay
jsr RestoreCheckpoint ;Restore player & world state from last entered area & start again
lda actHp+ACTI_PLAYER
cmp #50 ;Ensure at least half health after restart
bcs CR_HealthOK
lda #50
sta actHp+ACTI_PLAYER
CR_HealthOK:
CR_NoRestart: rts
Print3BCDDigits:lda zpDestHi
jsr PrintBCDDigit
lda zpDestLo
jmp PrintBCDDigits
displayedHealth:dc.b $ff
displayedAmmo: dc.b $ff
restartDelay: dc.b 50
randomAreaStart:
include raster.s ;Include rest of the engine code
include sound.s
include input.s
include screen.s
include sprite.s
include math.s
include file.s
include panel.s
include actor.s
include physics.s
include ai.s
include level.s
randomAreaEnd:
include playroutinedata.s ;Include datas
include sounddata.s
include actordata.s
include miscdata.s
include aligneddata.s
; Dynamic allocation area begin
fileAreaStart:
include init.s ;Disposable init code