A former undercover New York City detective in a gun-buy operation said he had no choice but to fire his weapon after the suspect robbed him and pointed a gun at his head.

"I just didn't want to die," he said Monday in Westchester County Court. "I needed to stop him. So I fired."

The robber was hit that August afternoon two years ago in Mount Vernon. But so was an innocent bystander, 61-year-old Felix Kumi, a school bus driver walking to a repair shop to pick up his car. Kumi was hit twice and died.

The ex-detective, referred to as Undercover 113, testified Monday at the murder trial of Alvin Smothers, the man who robbed him. He was led into court through a back door before testifying, and although he gave his name when he was sworn in, he is not being identified because of the sensitivity of his years of undercover work.

MOUNT VERNON: Man beats girl, 2, to death, injures her twin

MOUNT VERNON: Council, comptroller faulted for trying to block mayoral appointments

Smothers never fired his own weapon that day. The weapon he wielded was an inoperable BB gun, But he is charged with second-degree murder under the theory of felony murder, when someone not involved in the crime is killed either during the commission of a felony or while the perpetrator is fleeing.

Case law in New York has supported a wide range of convictions in felony murder cases when someone other than the defendant is the killer. Several of those instances involve police killing innocent bystanders.

Smothers opted for a non-jury trial before Westchester County Judge Barbara Zambelli. His defense is that he never aimed his gun at the undercover officer once out of the car and that he could not have foreseen Kumi's death once he was walking away from the robbery.

Prosecutors Perry Perrone and Christine O'Connor contend that the detective's perception that his life was in danger justified the shooting, and that someone robbing the buyer of illegal handguns should anticipate a violent reaction.

Undercover 113 was part of Operation Stadium, a probe by the NYPD's Firearm Investigations Unit into illegal guns in the 44th Precinct of the Bronx, near Yankee Stadium.

The investigation had already resulted in the purchase of 26 illegal guns through Jeff Aristy, a target of the probe. But the team was not satisfied yet, hoping to catch Aristy's supplier.

On Aug. 28, 2015, after getting a text from Aristy, the detective advised the team that two more guns were available — a Mac 10 machine pistol and a .38 caliber revolver — for $2,450. With backups following him, he drove to Featherbed Lane in the Bronx. He picked up Aristy, who directed him to Fulton Avenue in Mount Vernon where they would meet his cousin.

After Aristy got out to get a soda, he returned with Samuel Ruiz, who he identified as his cousin. Soon, Smothers approached and Ruiz and Aristy left to go with him. When Aristy and Smothers returned, Smothers directed Undercover 113 around several blocks until they were parked on the south side of Beekman, just west of Tecumseh Avenue.

The undercover expressed frustration that the gun-buy was "choppy," that his earlier buys from Aristy had been much quicker. But Smothers said he was worried about getting caught. Aristy also expressed hesitation because he had identified one of the backup cars as police. The undercover communicated through his wireless transmitter and the car moved. He also convinced Aristy that it was probably Mount Vernon police because that was a high-crime area.

Soon though, Smothers removed a gun from his bag and demanded money. The undercover officer turned over cash that he was going to use to buy the guns. After threaterning to "pop the (expletive) out of you" if the driver turned around, Smothers got out.

A moment later, Undercover 113 pulled out his own Glock 26 and got out of the car "to assess" the situation. As soon as he looked over the rooftop, he testified, he saw Smothers looking back at him.

Not only was Smothers pointing the gun at him, he said, but he believed Smothers had pulled the trigger as well.

"I thought his gun malfunctioned," he testified. "All he has to do is rack the slide. I wasn't going to wait for him to do so."

Undercover 113 fired 11 shots, emptying his magazine, as Smothers fled up Tecumseh.

On the video from the inside of the unmarked Cadillac the initial gunshots could be heard almost as soon as the detective got out.

The undercover then paused by a stone wall to load a second magazine. In that brief moment, he said, he observed Kumi for the first time — but did not realize then that two of his initial shots had struck him. Kumi was leaning on a car near the corner and let out a "faint scream," the undercover said.

"I thought he was traumatized from being in a shootout," he testified.

He continued to pursue Smothers, firing five shots, followed several seconds later by another five shots as Smothers ran down the driveway of 17 Tecumseh. By then, another member of the team had shown up and told the undercover to "get out of there" so he ran back to his car and took off.

Two backup officers grabbed Ruiz on the block and responding Mount Vernon officers captured Smothers. Aristy got away but was taken into custody later that night. He is serving a 10-year prison sentence for the earlier gun sales. Ruiz pleaded to criminal facilitation and cooperated with prosecutors.

Within months, Undercover 113's 10-year police career was over. The married father of three suffered from post traumatic stress disorder and retired on disability last year.

"I was going through a lot of flashbacks...A lot of depression," he said when Perrone asked him why he resigned. He said he became withdrawn from family and friends. "A lot of emotional detachment from life itself."

When defense lawyer Allan Focarile asked him if he had killed Kumi, he struggled momentarily for the right words to emphasize he was not ultimately responsible.

"I believe that, yes, my rounds did aid in his death," the undercover said. But he insisted that while he felt guilty about shooting Kumi, firing at Smothers was justified because he thought he was going to be killed. He said he could not retreat because he thought Aristy might have been part of the robbery and could have shot him.

While he said he was fearful for his own life and those of his team members, the witness conceded that in the ensuing moments, he never warned them that Smothers had a gun and had tried to shoot him.

Kumi's relatives were not in court Monday. This year, New York City reached a $1.1 million settlement with them after they announced plans to sue for $20 million.

Undercover 113 was the final prosecution witness. The trial resumes on Wednesday and could wrap up the same day.

Twitter: @jonbandler